QSizePolicy 類

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

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

公共類型

enum ControlType { DefaultType, ButtonBox, CheckBox, ComboBox, ..., ToolButton }
flags ControlTypes
enum Policy { Fixed, Minimum, Maximum, Preferred, ..., 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

靜態公共成員

const QMetaObject staticMetaObject
uint qHash (QSizePolicy key , uint seed = ...)
QDataStream & operator<< (QDataStream & stream , const QSizePolicy & policy )
QDataStream & operator>> (QDataStream & stream , QSizePolicy & policy )

詳細描述

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

Widget 的大小策略是其自願以各種方式重置大小的錶達,和影響小部件如何被對待通過 布局引擎 . 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 特性。

QSizePolicy contains two independent 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 作為 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 實例。

該枚舉在 Qt 4.3 引入或被修改。

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 .

成員函數文檔編製

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

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 帶換行)。

該函數在 Qt 4.3 引入。

另請參閱 setHorizontalStretch (), setVerticalStretch (),和 controlType ().

QSizePolicy::ControlType QSizePolicy:: controlType () const

返迴應用此大小策略的 Widget 的關聯控件類型。

該函數在 Qt 4.3 引入。

另請參閱 setControlType ().

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

bool QSizePolicy:: hasHeightForWidth () const

返迴 true 若 Widget 的首選高度取決於其寬度;否則返迴 false .

另請參閱 setHeightForWidth ().

bool QSizePolicy:: hasWidthForHeight () const

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

另請參閱 setWidthForHeight ().

QSizePolicy::Policy QSizePolicy:: horizontalPolicy () const

Returns the horizontal component of the size policy.

另請參閱 setHorizontalPolicy (), verticalPolicy (),和 horizontalStretch ().

int QSizePolicy:: horizontalStretch () const

Returns the horizontal stretch factor of the size policy.

另請參閱 setHorizontalStretch (), verticalStretch (),和 horizontalPolicy ().

bool QSizePolicy:: retainSizeWhenHidden () const

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

該函數在 Qt 5.2 引入。

另請參閱 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.

該函數在 Qt 4.3 引入。

另請參閱 controlType () 和 QStyle::layoutSpacing ().

void QSizePolicy:: setHeightForWidth ( bool dependent )

將 Widget 首選高度是否從屬其寬度的確定標誌設為 dependent .

另請參閱 hasHeightForWidth () 和 setWidthForHeight ().

void QSizePolicy:: setHorizontalPolicy ( QSizePolicy::Policy policy )

Sets the horizontal component to the given policy .

另請參閱 horizontalPolicy (), setVerticalPolicy (),和 setHorizontalStretch ().

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

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.

該函數在 Qt 5.2 引入。

另請參閱 retainSizeWhenHidden ().

void QSizePolicy:: setVerticalPolicy ( QSizePolicy::Policy policy )

Sets the vertical component to the given policy .

另請參閱 verticalPolicy (), setHorizontalPolicy (),和 setVerticalStretch ().

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

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

void QSizePolicy:: transpose ()

Swaps the horizontal and vertical policies and stretches.

另請參閱 transposed ().

QSizePolicy QSizePolicy:: transposed () const

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

該函數在 Qt 5.9 引入。

另請參閱 transpose ().

QSizePolicy::Policy QSizePolicy:: verticalPolicy () const

返迴大小策略的垂直組件。

另請參閱 setVerticalPolicy (), horizontalPolicy (),和 verticalStretch ().

int QSizePolicy:: verticalStretch () const

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

另請參閱 setVerticalStretch (), horizontalStretch (),和 verticalPolicy ().

QVariant QSizePolicy:: operator QVariant () const

返迴 QVariant storing this QSizePolicy .

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

返迴 true 若此策略不同於 other ;否則返迴 false .

另請參閱 operator== ().

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

返迴 true 若此策略等於 other ;否則返迴 false .

另請參閱 operator!= ().

相關非成員

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

返迴哈希值為 key ,使用 seed 做計算種子。

該函數在 Qt 5.6 引入。

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

Writes the size policy 到數據流 stream .

該函數在 Qt 4.2 引入。

另請參閱 QDataStream 運算符格式 .

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

Reads the size policy 從數據流 stream .

該函數在 Qt 4.2 引入。

另請參閱 QDataStream 運算符格式 .