Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QSize Class Reference

\inmodule QtCore More...

#include <qsize.h>

+ Inheritance diagram for QSize:
+ Collaboration diagram for QSize:

Public Member Functions

constexpr QSize () noexcept
 Constructs a size with an invalid width and height (i.e., isValid() returns false).
 
constexpr QSize (int w, int h) noexcept
 Constructs a size with the given width and height.
 
constexpr bool isNull () const noexcept
 Returns true if both the width and height is 0; otherwise returns false.
 
constexpr bool isEmpty () const noexcept
 Returns true if either of the width and height is less than or equal to 0; otherwise returns false.
 
constexpr bool isValid () const noexcept
 Returns true if both the width and height is equal to or greater than 0; otherwise returns false.
 
constexpr int width () const noexcept
 Returns the width.
 
constexpr int height () const noexcept
 Returns the height.
 
constexpr void setWidth (int w) noexcept
 Sets the width to the given width.
 
constexpr void setHeight (int h) noexcept
 Sets the height to the given height.
 
void transpose () noexcept
 Swaps the width and height values.
 
constexpr QSize transposed () const noexcept
 
void scale (int w, int h, Qt::AspectRatioMode mode) noexcept
 Scales the size to a rectangle with the given width and height, according to the specified mode:
 
void scale (const QSize &s, Qt::AspectRatioMode mode) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Scales the size to a rectangle with the given size, according to the specified mode.
 
QSize scaled (int w, int h, Qt::AspectRatioMode mode) const noexcept
 
QSize scaled (const QSize &s, Qt::AspectRatioMode mode) const noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
constexpr QSize expandedTo (const QSize &) const noexcept
 Returns a size holding the maximum width and height of this size and the given otherSize.
 
constexpr QSize boundedTo (const QSize &) const noexcept
 Returns a size holding the minimum width and height of this size and the given otherSize.
 
constexpr QSize grownBy (QMargins m) const noexcept
 
constexpr QSize shrunkBy (QMargins m) const noexcept
 
constexpr int & rwidth () noexcept
 Returns a reference to the width.
 
constexpr int & rheight () noexcept
 Returns a reference to the height.
 
constexpr QSizeoperator+= (const QSize &) noexcept
 Adds the given size to this size, and returns a reference to this size.
 
constexpr QSizeoperator-= (const QSize &) noexcept
 Subtracts the given size from this size, and returns a reference to this size.
 
constexpr QSizeoperator*= (qreal c) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Multiplies both the width and height by the given factor, and returns a reference to the size.
 
QSizeoperator/= (qreal c)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Divides both the width and height by the given divisor, and returns a reference to the size.
 
constexpr QSizeF toSizeF () const noexcept
 

Friends

constexpr bool comparesEqual (const QSize &s1, const QSize &s2) noexcept
 
constexpr QSize operator+ (const QSize &s1, const QSize &s2) noexcept
 Returns the sum of s1 and s2; each component is added separately.
 
constexpr QSize operator- (const QSize &s1, const QSize &s2) noexcept
 Returns s2 subtracted from s1; each component is subtracted separately.
 
constexpr QSize operator* (const QSize &s, qreal c) noexcept
 Multiplies the given size by the given factor, and returns the result rounded to the nearest integer.
 
constexpr QSize operator* (qreal c, const QSize &s) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Multiplies the given size by the given factor, and returns the result rounded to the nearest integer.
 
QSize operator/ (const QSize &s, qreal c)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Divides the given size by the given divisor, and returns the result rounded to the nearest integer.
 
constexpr size_t qHash (const QSize &s, size_t seed=0)
 
template<std::size_t I, typename S, std::enable_if_t<(I< 2), bool > = true, std::enable_if_t< std::is_same_v< q20::remove_cvref_t< S >, QSize >, bool > = true>
constexpr decltype(auto) get (S &&s) noexcept
 

Related Symbols

(Note that these are not member symbols.)

QDataStreamoperator<< (QDataStream &stream, const QSize &size)
 Writes the given size to the given stream, and returns a reference to the stream.
 
QDataStreamoperator>> (QDataStream &stream, QSize &size)
 Reads a size from the given stream into the given size, and returns a reference to the stream.
 

Detailed Description

\inmodule QtCore

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

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, QRect

Definition at line 25 of file qsize.h.

Constructor & Destructor Documentation

◆ QSize() [1/2]

QSize::QSize ( )
inlineconstexprnoexcept

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

See also
isValid()

Definition at line 127 of file qsize.h.

◆ QSize() [2/2]

QSize::QSize ( int width,
int height )
inlineconstexprnoexcept

Constructs a size with the given width and height.

See also
setWidth(), setHeight()

Definition at line 129 of file qsize.h.

Member Function Documentation

◆ boundedTo()

QSize QSize::boundedTo ( const QSize & otherSize) const
inlinenodiscardconstexprnoexcept

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

See also
expandedTo(), scale()

Definition at line 207 of file qsize.h.

◆ expandedTo()

QSize QSize::expandedTo ( const QSize & otherSize) const
inlinenodiscardconstexprnoexcept

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

See also
boundedTo(), scale()

Definition at line 202 of file qsize.h.

◆ grownBy()

QSize QSize::grownBy ( QMargins m) const
inlinenodiscardconstexprnoexcept

Definition at line 50 of file qsize.h.

◆ height()

int QSize::height ( ) const
inlineconstexprnoexcept

Returns the height.

See also
width(), setHeight()

Definition at line 143 of file qsize.h.

◆ isEmpty()

bool QSize::isEmpty ( ) const
inlineconstexprnoexcept

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

See also
isNull(), isValid()

Definition at line 134 of file qsize.h.

◆ isNull()

bool QSize::isNull ( ) const
inlineconstexprnoexcept

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

See also
isValid(), isEmpty()

Definition at line 131 of file qsize.h.

◆ isValid()

bool QSize::isValid ( ) const
inlineconstexprnoexcept

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

See also
isNull(), isEmpty()

Definition at line 137 of file qsize.h.

◆ operator*=()

QSize & QSize::operator*= ( qreal factor)
inlineconstexprnoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.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()

Definition at line 184 of file qsize.h.

◆ operator+=()

QSize & QSize::operator+= ( const QSize & size)
inlineconstexprnoexcept

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)

Definition at line 170 of file qsize.h.

◆ operator-=()

QSize & QSize::operator-= ( const QSize & size)
inlineconstexprnoexcept

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)

Definition at line 177 of file qsize.h.

◆ operator/=()

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.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()

Definition at line 194 of file qsize.h.

◆ rheight()

int & QSize::rheight ( )
inlineconstexprnoexcept

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(), setHeight()

Definition at line 167 of file qsize.h.

◆ rwidth()

int & QSize::rwidth ( )
inlineconstexprnoexcept

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(), setWidth()

Definition at line 164 of file qsize.h.

◆ scale() [1/2]

void QSize::scale ( const QSize & s,
Qt::AspectRatioMode mode )
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Scales the size to a rectangle with the given size, according to the specified mode.

Definition at line 158 of file qsize.h.

◆ scale() [2/2]

void QSize::scale ( int width,
int height,
Qt::AspectRatioMode mode )
inlinenoexcept

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

\list

  • 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. \endlist

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 is (60, 72)
See also
setWidth(), setHeight(), scaled()

Definition at line 155 of file qsize.h.

◆ scaled() [1/2]

QSize QSize::scaled ( const QSize & s,
Qt::AspectRatioMode mode ) const
nodiscardnoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
5.0

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

Definition at line 189 of file qsize.cpp.

◆ scaled() [2/2]

QSize QSize::scaled ( int width,
int height,
Qt::AspectRatioMode mode ) const
inlinenodiscardnoexcept
Since
5.0

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

See also
scale()

Definition at line 161 of file qsize.h.

◆ setHeight()

void QSize::setHeight ( int height)
inlineconstexprnoexcept

Sets the height to the given height.

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

Definition at line 149 of file qsize.h.

◆ setWidth()

void QSize::setWidth ( int width)
inlineconstexprnoexcept

Sets the width to the given width.

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

Definition at line 146 of file qsize.h.

◆ shrunkBy()

QSize QSize::shrunkBy ( QMargins m) const
inlinenodiscardconstexprnoexcept

Definition at line 52 of file qsize.h.

◆ toSizeF()

QSizeF QSize::toSizeF ( ) const
inlinenodiscardconstexprnoexcept
Since
6.4

Returns this size as a size with floating point accuracy.

See also
QSizeF::toSize()

Definition at line 416 of file qsize.h.

◆ transpose()

void QSize::transpose ( )
noexcept

Swaps the width and height values.

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

Definition at line 130 of file qsize.cpp.

◆ transposed()

QSize QSize::transposed ( ) const
inlinenodiscardconstexprnoexcept
Since
5.0

Returns a QSize with width and height swapped.

See also
transpose()

Definition at line 152 of file qsize.h.

◆ width()

int QSize::width ( ) const
inlineconstexprnoexcept

Returns the width.

See also
height(), setWidth()

Definition at line 140 of file qsize.h.

Friends And Related Symbol Documentation

◆ comparesEqual

bool comparesEqual ( const QSize & s1,
const QSize & s2 )
friend

Definition at line 64 of file qsize.h.

◆ get

template<std::size_t I, typename S, std::enable_if_t<(I< 2), bool > = true, std::enable_if_t< std::is_same_v< q20::remove_cvref_t< S >, QSize >, bool > = true>
decltype(auto) get ( S && s)
friend

Definition at line 103 of file qsize.h.

◆ operator* [1/2]

QSize operator* ( const QSize & size,
qreal factor )
friend

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

See also
QSize::scale()

Definition at line 71 of file qsize.h.

◆ operator* [2/2]

QSize operator* ( qreal c,
const QSize & s )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Multiplies the given size by the given factor, and returns the result rounded to the nearest integer.

Definition at line 73 of file qsize.h.

◆ operator+

QSize operator+ ( const QSize & s1,
const QSize & s2 )
friend

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

Definition at line 67 of file qsize.h.

◆ operator-

QSize operator- ( const QSize & s1,
const QSize & s2 )
friend

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

Definition at line 69 of file qsize.h.

◆ operator/

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Divides the given size by the given divisor, and returns the result rounded to the nearest integer.

See also
QSize::scale()

Definition at line 75 of file qsize.h.

◆ operator<<()

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

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

See also
{Serializing Qt Data Types}

◆ operator>>()

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

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

See also
{Serializing Qt Data Types}

◆ qHash

size_t qHash ( const QSize & s,
size_t seed = 0 )
friend

Definition at line 191 of file qsize.h.


The documentation for this class was generated from the following files: