QtBase  v6.3.1
Classes | Public Types | Public Member Functions | Friends | Related Functions | List of all members
QSizePolicy Class Reference

The QSizePolicy class is a layout attribute describing horizontal and vertical resizing policy. More...

#include <qsizepolicy.h>

Collaboration diagram for QSizePolicy:
Collaboration graph
[legend]

Public Types

enum  PolicyFlag { GrowFlag = 1 , ExpandFlag = 2 , ShrinkFlag = 4 , IgnoreFlag = 8 }
 
enum  Policy {
  Fixed = 0 , Minimum = GrowFlag , Maximum = ShrinkFlag , Preferred = GrowFlag | ShrinkFlag ,
  MinimumExpanding = GrowFlag | ExpandFlag , Expanding = GrowFlag | ShrinkFlag | ExpandFlag , Ignored = ShrinkFlag | GrowFlag | IgnoreFlag
}
 
enum  ControlType {
  DefaultType = 0x00000001 , ButtonBox = 0x00000002 , CheckBox = 0x00000004 , ComboBox = 0x00000008 ,
  Frame = 0x00000010 , GroupBox = 0x00000020 , Label = 0x00000040 , Line = 0x00000080 ,
  LineEdit = 0x00000100 , PushButton = 0x00000200 , RadioButton = 0x00000400 , Slider = 0x00000800 ,
  SpinBox = 0x00001000 , TabWidget = 0x00002000 , ToolButton = 0x00004000
}
 

Public Member Functions

constexpr QSizePolicy () noexcept
 
constexpr QSizePolicy (Policy horizontal, Policy vertical, ControlType type=DefaultType) noexcept
 
constexpr Policy horizontalPolicy () const noexcept
 
constexpr Policy verticalPolicy () const noexcept
 
ControlType controlType () const noexcept
 
constexpr void setHorizontalPolicy (Policy d) noexcept
 
constexpr void setVerticalPolicy (Policy d) noexcept
 
void setControlType (ControlType type) noexcept
 
constexpr Qt::Orientations expandingDirections () const noexcept
 
constexpr void setHeightForWidth (bool b) noexcept
 
constexpr bool hasHeightForWidth () const noexcept
 
constexpr void setWidthForHeight (bool b) noexcept
 
constexpr bool hasWidthForHeight () const noexcept
 
constexpr bool operator== (const QSizePolicy &s) const noexcept
 
constexpr bool operator!= (const QSizePolicy &s) const noexcept
 
 operator QVariant () const
 
constexpr int horizontalStretch () const noexcept
 
constexpr int verticalStretch () const noexcept
 
constexpr void setHorizontalStretch (int stretchFactor)
 
constexpr void setVerticalStretch (int stretchFactor)
 
constexpr bool retainSizeWhenHidden () const noexcept
 
constexpr void setRetainSizeWhenHidden (bool retainSize) noexcept
 
constexpr void transpose () noexcept
 
constexpr QSizePolicy transposed () const noexcept
 

Friends

Q_DECL_CONST_FUNCTION size_t qHash (QSizePolicy key, size_t seed=0) noexcept
 
Q_WIDGETS_EXPORT QDataStreamoperator<< (QDataStream &, const QSizePolicy &)
 
Q_WIDGETS_EXPORT QDataStreamoperator>> (QDataStream &, QSizePolicy &)
 

Related Functions

(Note that these are not member functions.)

QDataStreamoperator<< (QDataStream &stream, const QSizePolicy &policy)
 
QDataStreamoperator>> (QDataStream &stream, QSizePolicy &policy)
 

Detailed Description

The QSizePolicy class is a layout attribute describing horizontal and vertical resizing policy.

\inmodule QtWidgets

The size policy of a widget is an expression of its willingness to be resized in various ways, and affects how the widget is treated by the \l{Layout Management}{layout engine}. Each widget returns a QSizePolicy that describes the horizontal and vertical resizing policy it prefers when being laid out. You can change this for a specific widget by changing its QWidget::sizePolicy property.

QSizePolicy contains two independent QSizePolicy::Policy values and two stretch factors; one describes the widgets's horizontal size policy, and the other describes its vertical size policy. It also contains a flag to indicate whether the height and width of its preferred size are related.

The horizontal and vertical policies can be set in the constructor, and altered using the setHorizontalPolicy() and setVerticalPolicy() functions. The stretch factors can be set using the setHorizontalStretch() and setVerticalStretch() functions. The flag indicating whether the widget's \l{QWidget::sizeHint()}{sizeHint()} is width-dependent (such as a menu bar or a word-wrapping label) can be set using the setHeightForWidth() function.

The current size policies and stretch factors be retrieved using the horizontalPolicy(), verticalPolicy(), horizontalStretch() and verticalStretch() functions. Alternatively, use the transpose() function to swap the horizontal and vertical policies and stretches. The hasHeightForWidth() function returns the current status of the flag indicating the size hint dependencies.

Use the expandingDirections() function to determine whether the associated widget can make use of more space than its \l{QWidget::sizeHint()}{sizeHint()} function indicates, as well as find out in which directions it can expand.

Finally, the QSizePolicy class provides operators comparing this size policy to a given policy, as well as a QVariant operator storing this QSizePolicy as a QVariant object.

See also
QSize, QWidget::sizeHint(), QWidget::sizePolicy, QLayoutItem::sizeHint()

Definition at line 53 of file qsizepolicy.h.

Member Enumeration Documentation

◆ ControlType

Since
4.3

This enum specifies the different types of widgets in terms of layout interaction:

\value DefaultType The default type, when none is specified. \value ButtonBox A QDialogButtonBox instance. \value CheckBox A QCheckBox instance. \value ComboBox A QComboBox instance. \value Frame A QFrame instance. \value GroupBox A QGroupBox instance. \value Label A QLabel instance. \value Line A QFrame instance with QFrame::HLine or QFrame::VLine. \value LineEdit A QLineEdit instance. \value PushButton A QPushButton instance. \value RadioButton A QRadioButton instance. \value Slider A QAbstractSlider instance. \value SpinBox A QAbstractSpinBox instance. \value TabWidget A QTabWidget instance. \value ToolButton A QToolButton instance.

See also
setControlType(), controlType()
Enumerator
DefaultType 
ButtonBox 
CheckBox 
ComboBox 
Frame 
GroupBox 
Label 
Line 
LineEdit 
PushButton 
RadioButton 
Slider 
SpinBox 
TabWidget 
ToolButton 

Definition at line 76 of file qsizepolicy.h.

◆ Policy

This enum describes the various per-dimension sizing types used when constructing a QSizePolicy.

\value Fixed The QWidget::sizeHint() is the only acceptable alternative, so the widget can never grow or shrink (e.g. the vertical direction of a push button).

\value Minimum The sizeHint() is minimal, and sufficient. The widget can be expanded, but there is no advantage to it being larger (e.g. the horizontal direction of a push button). It cannot be smaller than the size provided by sizeHint().

\value Maximum The sizeHint() is a maximum. The widget can be shrunk any amount without detriment if other widgets need the space (e.g. a separator line). It cannot be larger than the size provided by sizeHint().

\value Preferred The sizeHint() is best, but the widget can be shrunk and still be useful. The widget can be expanded, but there is no advantage to it being larger than sizeHint() (the default QWidget policy).

\value Expanding The sizeHint() is a sensible size, but the widget can be shrunk and still be useful. The widget can make use of extra space, so it should get as much space as possible (e.g. the horizontal direction of a horizontal slider).

\value MinimumExpanding The sizeHint() is minimal, and sufficient. The widget can make use of extra space, so it should get as much space as possible (e.g. the horizontal direction of a horizontal slider).

\value Ignored The sizeHint() is ignored. The widget will get as much space as possible.

See also
PolicyFlag, setHorizontalPolicy(), setVerticalPolicy()
Enumerator
Fixed 
Minimum 
Maximum 
Preferred 
MinimumExpanding 
Expanding 
Ignored 

Definition at line 65 of file qsizepolicy.h.

◆ PolicyFlag

These flags are combined together to form the various \l{Policy} values:

\value GrowFlag The widget can grow beyond its size hint if necessary. \value ExpandFlag The widget should get as much space as possible. \value ShrinkFlag The widget can shrink below its size hint if necessary. \value IgnoreFlag The widget's size hint is ignored. The widget will get as much space as possible.

See also
Policy
Enumerator
GrowFlag 
ExpandFlag 
ShrinkFlag 
IgnoreFlag 

Definition at line 58 of file qsizepolicy.h.

Constructor & Destructor Documentation

◆ QSizePolicy() [1/2]

QSizePolicy::QSizePolicy ( )
inlineconstexprnoexcept

Constructs a QSizePolicy object with \l Fixed as its horizontal and vertical policies.

The policies can be altered using the setHorizontalPolicy() and setVerticalPolicy() functions. Use the setHeightForWidth() function if the preferred height of the widget is dependent on the width of the widget (for example, a QLabel with line wrapping).

See also
setHorizontalStretch(), setVerticalStretch()

Definition at line 96 of file qsizepolicy.h.

◆ QSizePolicy() [2/2]

QSizePolicy::QSizePolicy ( Policy  horizontal,
Policy  vertical,
ControlType  type = DefaultType 
)
inlineconstexprnoexcept
Since
4.3

Constructs a QSizePolicy object with the given horizontal and vertical policies, and the specified control type.

Use setHeightForWidth() if the preferred height of the widget is dependent on the width of the widget (for example, a QLabel with line wrapping).

See also
setHorizontalStretch(), setVerticalStretch(), controlType()

Definition at line 98 of file qsizepolicy.h.

Member Function Documentation

◆ controlType()

QSizePolicy::ControlType QSizePolicy::controlType ( ) const
noexcept
Since
4.3

Returns the control type associated with the widget for which this size policy applies.

Definition at line 235 of file qsizepolicy.cpp.

Here is the caller graph for this function:

◆ expandingDirections()

Qt::Orientations QSizePolicy::expandingDirections ( ) const
inlineconstexprnoexcept

Returns whether a widget can make use of more space than the QWidget::sizeHint() function indicates.

A value of Qt::Horizontal or Qt::Vertical means that the widget can grow horizontally or vertically (i.e., the horizontal or vertical policy is \l Expanding or \l MinimumExpanding), whereas Qt::Horizontal | Qt::Vertical means that it can grow in both dimensions.

See also
horizontalPolicy(), verticalPolicy()

Definition at line 111 of file qsizepolicy.h.

Here is the caller graph for this function:

◆ hasHeightForWidth()

bool QSizePolicy::hasHeightForWidth ( ) const
inlineconstexprnoexcept

Returns true if the widget's preferred height depends on its width; otherwise returns false.

See also
setHeightForWidth()

Definition at line 117 of file qsizepolicy.h.

Here is the caller graph for this function:

◆ hasWidthForHeight()

bool QSizePolicy::hasWidthForHeight ( ) const
inlineconstexprnoexcept

Returns true if the widget's width depends on its height; otherwise returns false.

See also
setWidthForHeight()

Definition at line 119 of file qsizepolicy.h.

◆ horizontalPolicy()

QSizePolicy::Policy QSizePolicy::horizontalPolicy ( ) const
inlineconstexprnoexcept

Returns the horizontal component of the size policy.

See also
setHorizontalPolicy(), verticalPolicy(), horizontalStretch()

Definition at line 102 of file qsizepolicy.h.

Here is the caller graph for this function:

◆ horizontalStretch()

int QSizePolicy::horizontalStretch ( ) const
inlineconstexprnoexcept

Returns the horizontal stretch factor of the size policy.

See also
setHorizontalStretch(), verticalStretch(), horizontalPolicy()

Definition at line 128 of file qsizepolicy.h.

Here is the caller graph for this function:

◆ operator QVariant()

QSizePolicy::operator QVariant ( ) const

Returns a QVariant storing this QSizePolicy.

Definition at line 439 of file qsizepolicy.cpp.

Here is the call graph for this function:

◆ operator!=()

bool QSizePolicy::operator!= ( const QSizePolicy other) const
inlineconstexprnoexcept

Returns true if this policy is different from other; otherwise returns false.

See also
operator==()

Definition at line 122 of file qsizepolicy.h.

◆ operator==()

bool QSizePolicy::operator== ( const QSizePolicy other) const
inlineconstexprnoexcept

Returns true if this policy is equal to other; otherwise returns false.

See also
operator!=()

Definition at line 121 of file qsizepolicy.h.

◆ retainSizeWhenHidden()

void QSizePolicy::retainSizeWhenHidden ( ) const
inlineconstexprnoexcept
Since
5.2

Returns whether the layout should retain the widget's size when it is hidden. This is false by default.

See also
setRetainSizeWhenHidden()

Definition at line 133 of file qsizepolicy.h.

Here is the caller graph for this function:

◆ setControlType()

void QSizePolicy::setControlType ( ControlType  type)
noexcept
Since
4.3

Sets the control type associated with the widget for which this size policy applies to type.

The control type specifies the type of the widget for which this size policy applies. It is used by some styles, notably QMacStyle, to insert proper spacing between widgets. For example, the \macos Aqua guidelines specify that push buttons should be separated by 12 pixels, whereas vertically stacked radio buttons only require 6 pixels.

See also
QStyle::layoutSpacing()

Definition at line 256 of file qsizepolicy.cpp.

◆ setHeightForWidth()

void QSizePolicy::setHeightForWidth ( bool  dependent)
inlineconstexprnoexcept

Sets the flag determining whether the widget's preferred height depends on its width, to dependent.

See also
hasHeightForWidth(), setWidthForHeight()

Definition at line 116 of file qsizepolicy.h.

Here is the caller graph for this function:

◆ setHorizontalPolicy()

void QSizePolicy::setHorizontalPolicy ( Policy  policy)
inlineconstexprnoexcept

Sets the horizontal component to the given policy.

See also
horizontalPolicy(), setVerticalPolicy(), setHorizontalStretch()

Definition at line 106 of file qsizepolicy.h.

Here is the caller graph for this function:

◆ setHorizontalStretch()

void QSizePolicy::setHorizontalStretch ( int  stretchFactor)
inlineconstexpr

Sets the horizontal stretch factor of the size policy to the given stretchFactor. stretchFactor must be in the range [0,255].

When two widgets are adjacent to each other in a horizontal layout, setting the horizontal stretch factor of the widget on the left to 2 and the factor of widget on the right to 1 will ensure that the widget on the left will always be twice the size of the one on the right.

See also
horizontalStretch(), setVerticalStretch(), setHorizontalPolicy()

Definition at line 130 of file qsizepolicy.h.

Here is the caller graph for this function:

◆ setRetainSizeWhenHidden()

void QSizePolicy::setRetainSizeWhenHidden ( bool  retainSize)
inlineconstexprnoexcept
Since
5.2

Sets whether a layout should retain the widget's size when it is hidden. If retainSize is true, the layout will not be changed by hiding the widget.

See also
retainSizeWhenHidden()

Definition at line 134 of file qsizepolicy.h.

Here is the caller graph for this function:

◆ setVerticalPolicy()

void QSizePolicy::setVerticalPolicy ( Policy  policy)
inlineconstexprnoexcept

Sets the vertical component to the given policy.

See also
verticalPolicy(), setHorizontalPolicy(), setVerticalStretch()

Definition at line 107 of file qsizepolicy.h.

Here is the caller graph for this function:

◆ setVerticalStretch()

void QSizePolicy::setVerticalStretch ( int  stretchFactor)
inlineconstexpr

Sets the vertical stretch factor of the size policy to the given stretchFactor. stretchFactor must be in the range [0,255].

When two widgets are adjacent to each other in a vertical layout, setting the vertical stretch factor of the widget on the top to 2 and the factor of widget on the bottom to 1 will ensure that the widget on the top will always be twice the size of the one on the bottom.

See also
verticalStretch(), setHorizontalStretch(), setVerticalPolicy()

Definition at line 131 of file qsizepolicy.h.

Here is the caller graph for this function:

◆ setWidthForHeight()

void QSizePolicy::setWidthForHeight ( bool  dependent)
inlineconstexprnoexcept

Sets the flag determining whether the widget's width depends on its height, to dependent.

This is only supported for QGraphicsLayout's subclasses. It is not possible to have a layout with both height-for-width and width-for-height constraints at the same time.

See also
hasWidthForHeight(), setHeightForWidth()

Definition at line 118 of file qsizepolicy.h.

◆ transpose()

void QSizePolicy::transpose ( )
inlineconstexprnoexcept

Swaps the horizontal and vertical policies and stretches.

See also
transposed()

Definition at line 136 of file qsizepolicy.h.

◆ transposed()

QSizePolicy QSizePolicy::transposed ( ) const
inlineconstexprnoexcept
Since
5.9

Returns a size policy object with the horizontal and vertical policies and stretches swapped.

See also
transpose()

Definition at line 137 of file qsizepolicy.h.

◆ verticalPolicy()

QSizePolicy::Policy QSizePolicy::verticalPolicy ( ) const
inlineconstexprnoexcept

Returns the vertical component of the size policy.

See also
setVerticalPolicy(), horizontalPolicy(), verticalStretch()

Definition at line 103 of file qsizepolicy.h.

Here is the caller graph for this function:

◆ verticalStretch()

int QSizePolicy::verticalStretch ( ) const
inlineconstexprnoexcept

Returns the vertical stretch factor of the size policy.

See also
setVerticalStretch(), horizontalStretch(), verticalPolicy()

Definition at line 129 of file qsizepolicy.h.

Here is the caller graph for this function:

Friends And Related Function Documentation

◆ operator<< [1/2]

QDataStream & operator<< ( QDataStream stream,
const QSizePolicy policy 
)
friend
Since
4.2

Writes the size policy to the data stream stream.

See also
{Serializing Qt Data Types}{Format of the QDataStream operators}

Definition at line 454 of file qsizepolicy.cpp.

◆ operator<<() [2/2]

QDataStream & operator<< ( QDataStream stream,
const QSizePolicy policy 
)
related
Since
4.2

Writes the size policy to the data stream stream.

See also
{Serializing Qt Data Types}{Format of the QDataStream operators}

Definition at line 454 of file qsizepolicy.cpp.

◆ operator>> [1/2]

QDataStream & operator>> ( QDataStream stream,
QSizePolicy policy 
)
friend
Since
4.2

Reads the size policy from the data stream stream.

See also
{Serializing Qt Data Types}{Format of the QDataStream operators}

Definition at line 478 of file qsizepolicy.cpp.

◆ operator>>() [2/2]

QDataStream & operator>> ( QDataStream stream,
QSizePolicy policy 
)
related
Since
4.2

Reads the size policy from the data stream stream.

See also
{Serializing Qt Data Types}{Format of the QDataStream operators}

Definition at line 478 of file qsizepolicy.cpp.

◆ qHash

size_t QSizePolicy::qHash ( QSizePolicy  key,
size_t  seed = 0 
)
friend
Since
5.6

Returns the hash value for key, using seed to seed the calculation.

Definition at line 124 of file qsizepolicy.h.

Member Data Documentation

◆ bits

Bits QSizePolicy::bits

Definition at line 193 of file qsizepolicy.h.

◆ data

quint32 QSizePolicy::data

Definition at line 194 of file qsizepolicy.h.


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