QSizePolicy 类

QSizePolicy 类是描述水平 垂直尺寸重置策略的布局属性。 更多...

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

公共类型

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

公共函数

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
size_t qHash (QSizePolicy key , size_t seed = 0)
QDataStream & operator<< (QDataStream & stream , const QSizePolicy & policy )
QDataStream & operator>> (QDataStream & stream , QSizePolicy & policy )

详细描述

Widget 的大小策略是其自愿以各种方式重置大小的表达,和影响小部件如何被对待通过 布局引擎 。每个 Widget (当布置时) 返回描述它首选的水平和垂直重置大小策略的 QSizePolicy。特定 Widget 可以改变此通过更改其 QWidget::sizePolicy 特性。

QSizePolicy 包含 2 独立 QSizePolicy::Policy 值和 2 拉伸因子;一个描述 Widget 的水平大小策略,而另一描述其垂直大小策略。它还包含指示其首选大小高度和宽度是否相关的标志。

The horizontal and vertical policies can be set in the constructor, and altered using the setHorizontalPolicy () 和 setVerticalPolicy () functions. The stretch factors can be set using the setHorizontalStretch () 和 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 () 函数。

The current size policies and stretch factors be retrieved using the horizontalPolicy (), verticalPolicy (), horizontalStretch () 和 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.

使用 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 对象。

另请参阅 QSize , QWidget::sizeHint (), QWidget::sizePolicy ,和 QLayoutItem::sizeHint ().

成员类型文档编制

enum QSizePolicy:: ControlType
flags QSizePolicy:: ControlTypes

此枚举指定 Widget 的不同类型,就布局交互而言:

常量 描述
QSizePolicy::DefaultType 0x00000001 默认类型,当未指定时。
QSizePolicy::ButtonBox 0x00000002 A QDialogButtonBox 实例。
QSizePolicy::CheckBox 0x00000004 A QCheckBox 实例。
QSizePolicy::ComboBox 0x00000008 A QComboBox 实例。
QSizePolicy::Frame 0x00000010 A QFrame 实例。
QSizePolicy::GroupBox 0x00000020 A QGroupBox 实例。
QSizePolicy::Label 0x00000040 A QLabel 实例。
QSizePolicy::Line 0x00000080 A QFrame 实例与 QFrame::HLine or QFrame::VLine .
QSizePolicy::LineEdit 0x00000100 A QLineEdit 实例。
QSizePolicy::PushButton 0x00000200 A QPushButton 实例。
QSizePolicy::RadioButton 0x00000400 A QRadioButton 实例。
QSizePolicy::Slider 0x00000800 A QAbstractSlider 实例。
QSizePolicy::SpinBox 0x00001000 A QAbstractSpinBox 实例。
QSizePolicy::TabWidget 0x00002000 A QTabWidget 实例。
QSizePolicy::ToolButton 0x00004000 A QToolButton 实例。

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

另请参阅 setControlType () 和 controlType ().

enum QSizePolicy:: Policy

此枚举描述使用的各种每维度大小调整类型当构造 QSizePolicy .

常量 描述
QSizePolicy::Fixed 0 The QWidget::sizeHint () 是唯一可接受的替代,因此 Widget 可以从不增长或收缩 (如:按钮的垂直方向)。
QSizePolicy::Minimum GrowFlag sizeHint() 最小,且足够。Widget 可以扩展,但变大没有好处 (如:按钮的水平方向)。它不可以小于由 sizeHint() 提供的大小。
QSizePolicy::Maximum ShrinkFlag sizeHint() 最大。Widget 可以任意收缩且不会受到损害,若其它小部件需要空间 (如:分隔线)。它不可以大于由 sizeHint() 提供的大小。
QSizePolicy::Preferred GrowFlag | ShrinkFlag sizeHint() 最佳,但 Widget 可以收缩且仍然有用。小部件可以展开,但是大于 sizeHint() 没有好处 (默认 QWidget 策略)。
QSizePolicy::Expanding GrowFlag | ShrinkFlag | ExpandFlag sizeHint() 是合理大小,但 Widget 可以收缩且仍然有用。小部件可以利用额外空间,因此它应获得尽可能多的空间 (如:水平滑块的水平方向)。
QSizePolicy::MinimumExpanding GrowFlag | ExpandFlag sizeHint() 为最小,且足够。Widget 可以利用额外空间,因此应获得尽可能多的空间 (如:水平滑块的水平方向)。
QSizePolicy::Ignored ShrinkFlag | GrowFlag | IgnoreFlag sizeHint() 被忽略。Widget 将获得尽可能多的空间。

另请参阅 PolicyFlag , setHorizontalPolicy (),和 setVerticalPolicy ().

enum QSizePolicy:: PolicyFlag

组合这些标志在一起以形成各种 Policy 值:

常量 描述
QSizePolicy::GrowFlag 1 Widget 可以增长到超出其大小提示 (若有必要)。
QSizePolicy::ExpandFlag 2 Widget 应获得尽可能多的空间。
QSizePolicy::ShrinkFlag 4 Widget 可以收缩到其大小提示以内 (若有必要)。
QSizePolicy::IgnoreFlag 8 Widget 的大小提示被忽略。小部件将获得尽可能多的空间。

另请参阅 Policy .

成员函数文档编制

[constexpr noexcept] QSizePolicy:: QSizePolicy ()

构造 QSizePolicy 对象采用 Fixed as its horizontal and vertical policies.

The policies can be altered using the setHorizontalPolicy () 和 setVerticalPolicy () 函数。使用 setHeightForWidth () function if the preferred height of the widget is dependent on the width of the widget (for example, a QLabel 带换行)。

另请参阅 setHorizontalStretch () 和 setVerticalStretch ().

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

构造 QSizePolicy 对象采用给定 horizontal and vertical 策略,并指定控件 type .

使用 setHeightForWidth () if the preferred height of the widget is dependent on the width of the widget (for example, a QLabel 带换行)。

另请参阅 setHorizontalStretch (), setVerticalStretch (),和 controlType ().

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

返回应用此大小策略的 Widget 的关联控件类型。

另请参阅 setControlType ().

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

Returns whether a widget can make use of more space than the QWidget::sizeHint () 函数指示。

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.

另请参阅 horizontalPolicy () 和 verticalPolicy ().

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

返回 true 若 Widget 的首选高度取决于其宽度;否则返回 false .

另请参阅 setHeightForWidth ().

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

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

另请参阅 setWidthForHeight ().

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

Returns the horizontal component of the size policy.

另请参阅 setHorizontalPolicy (), verticalPolicy (),和 horizontalStretch ().

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

Returns the horizontal stretch factor of the size policy.

另请参阅 setHorizontalStretch (), verticalStretch (),和 horizontalPolicy ().

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

Returns whether the layout should retain the widget's size when it is hidden. This is false 在默认情况下。

另请参阅 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.

另请参阅 controlType () 和 QStyle::layoutSpacing ().

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

将 Widget 首选高度是否从属其宽度的确定标志设为 dependent .

另请参阅 hasHeightForWidth () 和 setWidthForHeight ().

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

Sets the horizontal component to the given policy .

另请参阅 horizontalPolicy (), setVerticalPolicy (),和 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.

另请参阅 horizontalStretch (), setVerticalStretch (),和 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.

另请参阅 retainSizeWhenHidden ().

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

Sets the vertical component to the given policy .

另请参阅 verticalPolicy (), setHorizontalPolicy (),和 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.

另请参阅 verticalStretch (), setHorizontalStretch (),和 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.

另请参阅 hasWidthForHeight () 和 setHeightForWidth ().

[constexpr noexcept] void QSizePolicy:: transpose ()

Swaps the horizontal and vertical policies and stretches.

另请参阅 transposed ().

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

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

另请参阅 transpose ().

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

返回大小策略的垂直组件。

另请参阅 setVerticalPolicy (), horizontalPolicy (),和 verticalStretch ().

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

返回大小策略的垂直拉伸因子。

另请参阅 setVerticalStretch (), horizontalStretch (),和 verticalPolicy ().

QVariant QSizePolicy:: operator QVariant () const

返回 QVariant storing this QSizePolicy .

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

返回 true 若此策略不同于 other ;否则返回 false .

另请参阅 operator== ().

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

返回 true 若此策略等于 other ;否则返回 false .

另请参阅 operator!= ().

相关非成员

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

返回哈希值为 key ,使用 seed 做计算种子。

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

Writes the size policy 到数据流 stream .

另请参阅 QDataStream 运算符格式 .

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

Reads the size policy 从数据流 stream .

另请参阅 QDataStream 运算符格式 .