QSize Class | Qt Core | Qt 6.11.1
Description: The QSize class defines the size of a two-dimensional object using integer point precision.

- Qt 6.11
- Qt Core
- C++ Classes
- QSize
- Qt 6.11.1 Reference Documentation

# QSize Class

The QSize class defines the size of a two-dimensional object using integer point precision. More...

Header: | `#include <QSize>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Core)` `target_link_libraries(mytarget PRIVATE Qt6::Core)`
qmake: | `QT += core`

- List of all members, including inherited members
- QSize is part of Painting Classes.

## Public Functions

| QSize()
| QSize(int width, int height)
QSize | boundedTo(const QSize &otherSize) const
QSize | expandedTo(const QSize &otherSize) const
QSize | grownBy(QMargins margins) const
int | height() const
bool | isEmpty() const
bool | isNull() const
bool | isValid() const
int & | rheight()
int & | rwidth()
void | scale(int width, int height, Qt::AspectRatioMode mode)
void | scale(const QSize &size, Qt::AspectRatioMode mode)
QSize | scaled(int width, int height, Qt::AspectRatioMode mode) const
QSize | scaled(const QSize &s, Qt::AspectRatioMode mode) const
void | setHeight(int height)
void | setWidth(int width)
QSize | shrunkBy(QMargins margins) const
CGSize | toCGSize() const
`(since 6.4)` QSizeF | toSizeF() const
void | transpose()
QSize | transposed() const
int | width() const
QSize & | operator*=(qreal factor)
QSize & | operator+=(const QSize &size)
QSize & | operator-=(const QSize &size)
QSize & | operator/=(qreal divisor)

## Related Non-Members

bool | operator!=(const QSize &lhs, const QSize &rhs)
QSize | operator*(const QSize &size, qreal factor)
QSize | operator*(qreal factor, const QSize &size)
QSize | operator+(const QSize &s1, const QSize &s2)
QSize | operator-(const QSize &s1, const QSize &s2)
QSize | operator/(const QSize &size, qreal divisor)
QDataStream & | operator<<(QDataStream &stream, const QSize &size)
bool | operator==(const QSize &lhs, const QSize &rhs)
QDataStream & | operator>>(QDataStream &stream, QSize &size)

## Detailed Description

A size is specified by a width() and a height(). It can be set in the constructor and changed using the setWidth(), setHeight(), or scale() functions, or using arithmetic operators. A size can also be manipulated directly by retrieving references to the width and height using the rwidth() and rheight() functions. Finally, the width and height can be swapped using the transpose() function.

The isValid() function determines if a size is valid (a valid size has both width and height greater than or equal to zero). The isEmpty() function returns `true` if either of the width and height is less than, or equal to, zero, while the isNull() function returns `true` only if both the width and the height is zero.

Use the expandedTo() function to retrieve a size which holds the maximum height and width of this size and a given size. Similarly, the boundedTo() function returns a size which holds the minimum height and width of this size and a given size.

QSize objects can be streamed as well as compared.

See also QSizeF, QPoint, and QRect.

## Member Function Documentation

### `[constexpr noexcept]` QSize::QSize()

Constructs a size with an invalid width and height (i.e., isValid() returns `false`).

See also isValid().

### `[constexpr noexcept]` QSize::QSize(int width, int height)

Constructs a size with the given width and height.

See also setWidth() and setHeight().

### `[constexpr noexcept]` QSize QSize::boundedTo(const QSize &otherSize) const

Returns a size holding the minimum width and height of this size and the given otherSize.

See also expandedTo() and scale().

### `[constexpr noexcept]` QSize QSize::expandedTo(const QSize &otherSize) const

Returns a size holding the maximum width and height of this size and the given otherSize.

See also boundedTo() and scale().

### `[constexpr noexcept]` QSize QSize::grownBy(QMargins margins) const

Returns the size that results from growing this size by margins.

See also shrunkBy().

### `[constexpr noexcept]` int QSize::height() const

Returns the height.

See also width() and setHeight().

### `[constexpr noexcept]` bool QSize::isEmpty() const

Returns `true` if either of the width and height is less than or equal to 0; otherwise returns `false`.

See also isNull() and isValid().

### `[constexpr noexcept]` bool QSize::isNull() const

Returns `true` if both the width and height is 0; otherwise returns false.

See also isValid() and isEmpty().

### `[constexpr noexcept]` bool QSize::isValid() const

Returns `true` if both the width and height is equal to or greater than 0; otherwise returns `false`.

See also isNull() and isEmpty().

### `[constexpr noexcept]` int &QSize::rheight()

Returns a reference to the height.

Using a reference makes it possible to manipulate the height directly. For example:

     QSize size(100, 10);
     size.rheight() += 5;

     // size becomes (100,15)

See also rwidth() and setHeight().

### `[constexpr noexcept]` int &QSize::rwidth()

Returns a reference to the width.

Using a reference makes it possible to manipulate the width directly. For example:

     QSize size(100, 10);
     size.rwidth() += 20;

     // size becomes (120,10)

See also rheight() and setWidth().

### `[noexcept]` void QSize::scale(int width, int height, Qt::AspectRatioMode mode)

Scales the size to a rectangle with the given width and height, according to the specified mode:

- If mode is Qt::IgnoreAspectRatio, the size is set to (width, height).
- If mode is Qt::KeepAspectRatio, the current size is scaled to a rectangle as large as possible inside (width, height), preserving the aspect ratio.
- If mode is Qt::KeepAspectRatioByExpanding, the current size is scaled to a rectangle as small as possible outside (width, height), preserving the aspect ratio.

Example:

     QSize t1(10, 12);
     t1.scale(60, 60, Qt::IgnoreAspectRatio);
     // t1 is (60, 60)

     QSize t2(10, 12);
     t2.scale(60, 60, Qt::KeepAspectRatio);
     // t2 is (50, 60)

     QSize t3(10, 12);
     t3.scale(60, 60, Qt::KeepAspectRatioByExpanding);
     // t3 is (60, 72)

See also setWidth(), setHeight(), and scaled().

### `[noexcept]` void QSize::scale(const QSize &size, Qt::AspectRatioMode mode)

Scales the size to a rectangle with the given size, according to the specified mode.

This is an overloaded function.

### `[noexcept]` QSize QSize::scaled(int width, int height, Qt::AspectRatioMode mode) const

Return a size scaled to a rectangle with the given width and height, according to the specified mode.

See also scale().

### `[noexcept]` QSize QSize::scaled(const QSize &s, Qt::AspectRatioMode mode) const

Return a size scaled to a rectangle with the given size s, according to the specified mode.

This is an overloaded function.

### `[constexpr noexcept]` void QSize::setHeight(int height)

Sets the height to the given height.

See also rheight(), height(), and setWidth().

### `[constexpr noexcept]` void QSize::setWidth(int width)

Sets the width to the given width.

See also rwidth(), width(), and setHeight().

### `[constexpr noexcept]` QSize QSize::shrunkBy(QMargins margins) const

Returns the size that results from shrinking this size by margins.

See also grownBy().

### `[noexcept]` CGSize QSize::toCGSize() const

Creates a CGSize from a QSize.

See also QSizeF::fromCGSize().

### `[constexpr noexcept, since 6.4]` QSizeF QSize::toSizeF() const

Returns this size as a size with floating point accuracy.

This function was introduced in Qt 6.4.

See also QSizeF::toSize().

### `[noexcept]` void QSize::transpose()

Swaps the width and height values.

See also setWidth(), setHeight(), and transposed().

### `[constexpr noexcept]` QSize QSize::transposed() const

Returns a QSize with width and height swapped.

See also transpose().

### `[constexpr noexcept]` int QSize::width() const

Returns the width.

See also height() and setWidth().

### `[constexpr noexcept]` QSize &QSize::operator*=(qreal factor)

Multiplies both the width and height by the given factor, and returns a reference to the size.

Note that the result is rounded to the nearest integer.

See also scale().

### `[constexpr noexcept]` QSize &QSize::operator+=(const QSize &size)

Adds the given size to this size, and returns a reference to this size. For example:

     QSize s( 3, 7);
     QSize r(-1, 4);
     s += r;

     // s becomes (2,11)

### `[constexpr noexcept]` QSize &QSize::operator-=(const QSize &size)

Subtracts the given size from this size, and returns a reference to this size. For example:

     QSize s( 3, 7);
     QSize r(-1, 4);
     s -= r;

     // s becomes (4,3)

### QSize &QSize::operator/=(qreal divisor)

Divides both the width and height by the given divisor, and returns a reference to the size.

Note that the result is rounded to the nearest integer.

See also QSize::scale().

## Related Non-Members

### `[constexpr noexcept]` bool operator!=(const QSize &lhs, const QSize &rhs)

Returns `true` if lhs and rhs are different; otherwise returns `false`.

### `[constexpr noexcept]` QSize operator*(const QSize &size, qreal factor)

Multiplies the given size by the given factor, and returns the result rounded to the nearest integer.

See also QSize::scale().

### `[constexpr noexcept]` QSize operator*(qreal factor, const QSize &size)

Multiplies the given size by the given factor, and returns the result rounded to the nearest integer.

This is an overloaded function.

### `[constexpr noexcept]` QSize operator+(const QSize &s1, const QSize &s2)

Returns the sum of s1 and s2; each component is added separately.

### `[constexpr noexcept]` QSize operator-(const QSize &s1, const QSize &s2)

Returns s2 subtracted from s1; each component is subtracted separately.

### QSize operator/(const QSize &size, qreal divisor)

Divides the given size by the given divisor, and returns the result rounded to the nearest integer.

See also QSize::scale().

### QDataStream &operator<<(QDataStream &stream, const QSize &size)

Writes the given size to the given stream, and returns a reference to the stream.

See also Serializing Qt Data Types.

### `[constexpr noexcept]` bool operator==(const QSize &lhs, const QSize &rhs)

Returns `true` if lhs and rhs are equal; otherwise returns `false`.

### QDataStream &operator>>(QDataStream &stream, QSize &size)

Reads a size from the given stream into the given size, and returns a reference to the stream.

See also Serializing Qt Data Types.