QSizePolicy Class

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

Header: #include <QSizePolicy>
qmake: QT += widgets

Public Types

enum ControlType { DefaultType, ButtonBox, CheckBox, ComboBox, ..., ToolButton }
flags ControlTypes
enum Policy { Fixed, Minimum, Maximum, Preferred, ..., 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 b)
void setHorizontalPolicy(QSizePolicy::Policy d)
void setHorizontalStretch(int stretchFactor)
void setRetainSizeWhenHidden(bool retainSize)
void setVerticalPolicy(QSizePolicy::Policy d)
void setVerticalStretch(int stretchFactor)
void setWidthForHeight(bool b)
void transpose()
QSizePolicy transposed() const
QSizePolicy::Policy verticalPolicy() const
int verticalStretch() const
QVariant operator QVariant() const
bool operator!=(const QSizePolicy &s) const
bool operator==(const QSizePolicy &s) const
uint qHash(QSizePolicy key, uint seed = ...)
QDataStream &operator<<(QDataStream &stream, const QSizePolicy &policy)
QDataStream &operator>>(QDataStream &stream, QSizePolicy &policy)

Detailed Description

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

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:

ConstantValueDescription
QSizePolicy::DefaultType0x00000001The default type, when none is specified.
QSizePolicy::ButtonBox0x00000002A QDialogButtonBox instance.
QSizePolicy::CheckBox0x00000004A QCheckBox instance.
QSizePolicy::ComboBox0x00000008A QComboBox instance.
QSizePolicy::Frame0x00000010A QFrame instance.
QSizePolicy::GroupBox0x00000020A QGroupBox instance.
QSizePolicy::Label0x00000040A QLabel instance.
QSizePolicy::Line0x00000080A QFrame instance with QFrame::HLine or QFrame::VLine.
QSizePolicy::LineEdit0x00000100A QLineEdit instance.
QSizePolicy::PushButton0x00000200A QPushButton instance.
QSizePolicy::RadioButton0x00000400A QRadioButton instance.
QSizePolicy::Slider0x00000800A QAbstractSlider instance.
QSizePolicy::SpinBox0x00001000A QAbstractSpinBox instance.
QSizePolicy::TabWidget0x00002000A QTabWidget instance.
QSizePolicy::ToolButton0x00004000A QToolButton instance.

This enum was introduced or modified in Qt 4.3.

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.

ConstantValueDescription
QSizePolicy::Fixed0The 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::MinimumGrowFlagThe 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::MaximumShrinkFlagThe 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::PreferredGrowFlag | ShrinkFlagThe 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::ExpandingGrowFlag | ShrinkFlag | ExpandFlagThe 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::MinimumExpandingGrowFlag | ExpandFlagThe 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::IgnoredShrinkFlag | GrowFlag | IgnoreFlagThe 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:

ConstantValueDescription
QSizePolicy::GrowFlag1The widget can grow beyond its size hint if necessary.
QSizePolicy::ExpandFlag2The widget should get as much space as possible.
QSizePolicy::ShrinkFlag4The widget can shrink below its size hint if necessary.
QSizePolicy::IgnoreFlag8The widget's size hint is ignored. The widget will get as much space as possible.

See also Policy.

Member Function Documentation

QSizePolicy::QSizePolicy()

Default constructs an instance of QSizePolicy.

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

Default constructs an instance of QSizePolicy.

QSizePolicy::ControlType QSizePolicy::controlType() const

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

This function was introduced in Qt 4.3.

See also setControlType().

Qt::Orientations QSizePolicy::expandingDirections() const

bool QSizePolicy::hasHeightForWidth() const

bool QSizePolicy::hasWidthForHeight() const

QSizePolicy::Policy QSizePolicy::horizontalPolicy() const

See also setHorizontalPolicy().

int QSizePolicy::horizontalStretch() const

See also setHorizontalStretch().

bool QSizePolicy::retainSizeWhenHidden() const

See also setRetainSizeWhenHidden().

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.

This function was introduced in Qt 4.3.

See also controlType() and QStyle::layoutSpacing().

void QSizePolicy::setHeightForWidth(bool b)

See also hasHeightForWidth().

void QSizePolicy::setHorizontalPolicy(QSizePolicy::Policy d)

See also horizontalPolicy().

void QSizePolicy::setHorizontalStretch(int stretchFactor)

See also horizontalStretch().

void QSizePolicy::setRetainSizeWhenHidden(bool retainSize)

See also retainSizeWhenHidden().

void QSizePolicy::setVerticalPolicy(QSizePolicy::Policy d)

See also verticalPolicy().

void QSizePolicy::setVerticalStretch(int stretchFactor)

See also verticalStretch().

void QSizePolicy::setWidthForHeight(bool b)

See also hasWidthForHeight().

void QSizePolicy::transpose()

QSizePolicy QSizePolicy::transposed() const

QSizePolicy::Policy QSizePolicy::verticalPolicy() const

See also setVerticalPolicy().

int QSizePolicy::verticalStretch() const

See also setVerticalStretch().

QVariant QSizePolicy::operator QVariant() const

Returns a QVariant storing this QSizePolicy.

bool QSizePolicy::operator!=(const QSizePolicy &s) const

bool QSizePolicy::operator==(const QSizePolicy &s) const

Related Non-Members

uint qHash(QSizePolicy key, uint seed = ...)

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

This function was introduced in Qt 5.6.

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

Writes the size policy to the data stream stream.

This function was introduced in Qt 4.2.

See also Format of the QDataStream operators.

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

Reads the size policy from the data stream stream.

This function was introduced in Qt 4.2.

See also Format of the QDataStream operators.