- Qt 6.11
- Qt Widgets
- C++ Classes
- QSizePolicy
- Qt 6.11.0 Reference Documentation

# QSizePolicy Class

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

Header: | `#include <QSizePolicy>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Widgets)` `target_link_libraries(mytarget PRIVATE Qt6::Widgets)`
qmake: | `QT += widgets`

- List of all members, including inherited members

## Public Types

enum | ControlType { DefaultType, ButtonBox, CheckBox, ComboBox, Frame, …, ToolButton }
flags | ControlTypes
enum | Policy { Fixed, Minimum, Maximum, Preferred, Expanding, …, Ignored }
enum | PolicyFlag { GrowFlag, ExpandFlag, ShrinkFlag, IgnoreFlag }

## Public Functions

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

## Related Non-Members

size_t | qHash(QSizePolicy key, size_t seed = 0)
QDataStream & | operator<<(QDataStream &stream, const QSizePolicy &policy)
QDataStream & | operator>>(QDataStream &stream, QSizePolicy &policy)

## Detailed Description

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 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 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 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, and QLayoutItem::sizeHint().

## Member Type Documentation

### enum QSizePolicy::ControlType
flags QSizePolicy::ControlTypes

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

Constant | Value | Description
--- | --- | ---
`QSizePolicy::DefaultType` | `0x00000001` | The default type, when none is specified.
`QSizePolicy::ButtonBox` | `0x00000002` | A QDialogButtonBox instance.
`QSizePolicy::CheckBox` | `0x00000004` | A QCheckBox instance.
`QSizePolicy::ComboBox` | `0x00000008` | A QComboBox instance.
`QSizePolicy::Frame` | `0x00000010` | A QFrame instance.
`QSizePolicy::GroupBox` | `0x00000020` | A QGroupBox instance.
`QSizePolicy::Label` | `0x00000040` | A QLabel instance.
`QSizePolicy::Line` | `0x00000080` | A QFrame instance with QFrame::HLine or QFrame::VLine.
`QSizePolicy::LineEdit` | `0x00000100` | A QLineEdit instance.
`QSizePolicy::PushButton` | `0x00000200` | A QPushButton instance.
`QSizePolicy::RadioButton` | `0x00000400` | A QRadioButton instance.
`QSizePolicy::Slider` | `0x00000800` | A QAbstractSlider instance.
`QSizePolicy::SpinBox` | `0x00001000` | A QAbstractSpinBox instance.
`QSizePolicy::TabWidget` | `0x00002000` | A QTabWidget instance.
`QSizePolicy::ToolButton` | `0x00004000` | A QToolButton instance.

The ControlTypes type is a typedef for QFlags<ControlType>. It stores an OR combination of ControlType values.

See also setControlType() and controlType().

### enum QSizePolicy::Policy

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

Constant | Value | Description
--- | --- | ---
`QSizePolicy::Fixed` | `0` | 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).
`QSizePolicy::Minimum` | `GrowFlag` | 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().
`QSizePolicy::Maximum` | `ShrinkFlag` | 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().
`QSizePolicy::Preferred` | `GrowFlag | ShrinkFlag` | 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).
`QSizePolicy::Expanding` | `GrowFlag | ShrinkFlag | ExpandFlag` | 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).
`QSizePolicy::MinimumExpanding` | `GrowFlag | ExpandFlag` | 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).
`QSizePolicy::Ignored` | `ShrinkFlag | GrowFlag | IgnoreFlag` | The sizeHint() is ignored. The widget will get as much space as possible.

See also PolicyFlag, setHorizontalPolicy(), and setVerticalPolicy().

### enum QSizePolicy::PolicyFlag

These flags are combined together to form the various Policy values:

Constant | Value | Description
--- | --- | ---
`QSizePolicy::GrowFlag` | `1` | The widget can grow beyond its size hint if necessary.
`QSizePolicy::ExpandFlag` | `2` | The widget should get as much space as possible.
`QSizePolicy::ShrinkFlag` | `4` | The widget can shrink below its size hint if necessary.
`QSizePolicy::IgnoreFlag` | `8` | The widget's size hint is ignored. The widget will get as much space as possible.

See also Policy.

## Member Function Documentation

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

Constructs a QSizePolicy object with 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() and setVerticalStretch().

### `[constexpr noexcept]` QSizePolicy::QSizePolicy(QSizePolicy::Policy horizontal, QSizePolicy::Policy vertical, QSizePolicy::ControlType type = DefaultType)

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(), and controlType().

### `[noexcept]` QSizePolicy::ControlType QSizePolicy::controlType() const

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

See also setControlType().

### `[constexpr noexcept]` Qt::Orientations QSizePolicy::expandingDirections() const

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 Expanding or MinimumExpanding), whereas Qt::Horizontal | Qt::Vertical means that it can grow in both dimensions.

See also horizontalPolicy() and verticalPolicy().

### `[constexpr noexcept]` bool QSizePolicy::hasHeightForWidth() const

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

See also setHeightForWidth().

### `[constexpr noexcept]` bool QSizePolicy::hasWidthForHeight() const

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

See also setWidthForHeight().

### `[constexpr noexcept]` QSizePolicy::Policy QSizePolicy::horizontalPolicy() const

Returns the horizontal component of the size policy.

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

### `[constexpr noexcept]` int QSizePolicy::horizontalStretch() const

Returns the horizontal stretch factor of the size policy.

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

### `[constexpr noexcept]` bool QSizePolicy::retainSizeWhenHidden() const

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

See also setRetainSizeWhenHidden().

### `[noexcept]` void QSizePolicy::setControlType(QSizePolicy::ControlType type)

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 controlType() and QStyle::layoutSpacing().

### `[constexpr noexcept]` void QSizePolicy::setHeightForWidth(bool dependent)

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

See also hasHeightForWidth() and setWidthForHeight().

### `[constexpr noexcept]` void QSizePolicy::setHorizontalPolicy(QSizePolicy::Policy policy)

Sets the horizontal component to the given policy.

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

### `[constexpr]` void QSizePolicy::setHorizontalStretch(int stretchFactor)

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(), and setHorizontalPolicy().

### `[constexpr noexcept]` void QSizePolicy::setRetainSizeWhenHidden(bool retainSize)

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().

### `[constexpr noexcept]` void QSizePolicy::setVerticalPolicy(QSizePolicy::Policy policy)

Sets the vertical component to the given policy.

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

### `[constexpr]` void QSizePolicy::setVerticalStretch(int stretchFactor)

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(), and setVerticalPolicy().

### `[constexpr noexcept]` void QSizePolicy::setWidthForHeight(bool dependent)

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() and setHeightForWidth().

### `[constexpr noexcept]` void QSizePolicy::transpose()

Swaps the horizontal and vertical policies and stretches.

See also transposed().

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

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

See also transpose().

### `[constexpr noexcept]` QSizePolicy::Policy QSizePolicy::verticalPolicy() const

Returns the vertical component of the size policy.

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

### `[constexpr noexcept]` int QSizePolicy::verticalStretch() const

Returns the vertical stretch factor of the size policy.

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

### QVariant QSizePolicy::operator QVariant() const

Returns a QVariant storing this QSizePolicy.

### `[constexpr noexcept]` bool QSizePolicy::operator!=(const QSizePolicy &other) const

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

See also operator==().

### `[constexpr noexcept]` bool QSizePolicy::operator==(const QSizePolicy &other) const

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

See also operator!=().

## Related Non-Members

### `[noexcept]` size_t qHash(QSizePolicy key, size_t seed = 0)

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

### QDataStream &operator<<(QDataStream &stream, const QSizePolicy &policy)

Writes the size policy to the data stream stream.

See also Format of the QDataStream operators.

### QDataStream &operator>>(QDataStream &stream, QSizePolicy &policy)

Reads the size policy from the data stream stream.

See also Format of the QDataStream operators.