QBoxLayout 类

QBoxLayout 类水平或垂直排列子级 Widget。 更多...

头: #include <QBoxLayout>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承: QLayout
继承者:

QHBoxLayout and QVBoxLayout

公共类型

enum Direction { LeftToRight, RightToLeft, TopToBottom, BottomToTop }

公共函数

QBoxLayout (QBoxLayout::Direction dir , QWidget * parent = nullptr)
virtual ~QBoxLayout ()
void addLayout (QLayout * layout , int stretch = 0)
void addSpacerItem (QSpacerItem * spacerItem )
void addSpacing (int size )
void addStretch (int stretch = 0)
void addStrut (int size )
void addWidget (QWidget * widget , int stretch = 0, Qt::Alignment alignment = Qt::Alignment())
QBoxLayout::Direction direction () const
void insertItem (int index , QLayoutItem * item )
void insertLayout (int index , QLayout * layout , int stretch = 0)
void insertSpacerItem (int index , QSpacerItem * spacerItem )
void insertSpacing (int index , int size )
void insertStretch (int index , int stretch = 0)
void insertWidget (int index , QWidget * widget , int stretch = 0, Qt::Alignment alignment = Qt::Alignment())
void setDirection (QBoxLayout::Direction direction )
void setStretch (int index , int stretch )
bool setStretchFactor (QWidget * widget , int stretch )
bool setStretchFactor (QLayout * layout , int stretch )
int stretch (int index ) const

重实现公共函数

virtual void addItem (QLayoutItem * item ) override
virtual int count () const override
virtual Qt::Orientations expandingDirections () const override
virtual bool hasHeightForWidth () const override
virtual int heightForWidth (int w ) const override
virtual void invalidate () override
virtual QLayoutItem * itemAt (int index ) const override
virtual QSize maximumSize () const override
virtual int minimumHeightForWidth (int w ) const override
virtual QSize minimumSize () const override
virtual void setGeometry (const QRect & r ) override
virtual void setSpacing (int spacing ) override
virtual QSize sizeHint () const override
virtual int spacing () const override
virtual QLayoutItem * takeAt (int index ) override

详细描述

QBoxLayout 占用它获得的空间 (从其父级布局或从 parentWidget ()),将其分成行框,并使每个被管理的 Widget 填充一个框。

Five buttons in horizontal layout

若 QBoxLayout 的取向为 Qt::Horizontal 框按合适尺寸排成一行。每个 Widget (或其它框) 将至少获得其最小尺寸和最多获得其最大尺寸。任何多余空间根据拉伸因子共享 (详见下文)。

Five buttons in vertical layout

若 QBoxLayout 的取向为 Qt::Vertical ,框将再次采用合适尺寸排成一列。

最轻松方式创建 QBoxLayout 是使用某个方便类,如 QHBoxLayout (对于 Qt::Horizontal 框) 或 QVBoxLayout (对于 Qt::Vertical 框)。也可以直接使用 QBoxLayout 构造函数,指定其方向为 LeftToRight , RightToLeft , TopToBottom ,或 BottomToTop .

若 QBoxLayout 不是顶层布局 (即:它不管理 Widget 的所有区域和子级),必须将它添加到其父级布局,在采用它做任何事情之前。添加布局的正常方式是通过调用 parentLayout-> addLayout ().

一旦这样做,就可以将框添加到 QBoxLayout 使用下列 4 函数之一:

  • addWidget () 以将 Widget 添加到 QVBoxLayout 并设置 Widget 的拉伸因子 (拉伸因子沿着框的行)。
  • addSpacing () 以创建空框;这是用来创建漂亮且宽敞对话框的函数之一。有关设置边距的方式,见下文。
  • addStretch () 以创建空的可拉伸框。
  • addLayout () 以添加框包含另一 QLayout 到行并设置该布局的拉伸因子。

使用 insertWidget (), insertSpacing (), insertStretch () 或 insertLayout () 以在布局中指定位置插入框。

QBoxLayout 还包括 2 种边距宽度:

  • setContentsMargins () 设置 Widget 每侧外边框的宽度。这是预留空间的宽度沿着 QBoxLayout 的 4 边。
  • setSpacing () 设置相邻框之间的宽度。(可以使用 addSpacing () 以在特定位置获得更多空间)

边距默认由样式提供。大多数 Qt 样式指定的默认边距对于 Widget 为 9,对于窗口为 11。间距默认与顶层布局边距宽度相同,或与父布局相同。

要从布局移除 Widget,调用 removeWidget ()。调用 QWidget::hide () 在 Widget 还会从布局高效移除小部件,直到 QWidget::show () 被调用。

几乎总是会想要使用 QVBoxLayout and QHBoxLayout 而不是 QBoxLayout 因为它们的方便构造函数。

另请参阅 QGridLayout , QStackedLayout ,和 布局管理 .

成员类型文档编制

enum QBoxLayout:: Direction

此类型用于确定框布局的方向。

常量 描述
QBoxLayout::LeftToRight 0 水平从左到右。
QBoxLayout::RightToLeft 1 水平从右到左。
QBoxLayout::TopToBottom 2 垂直从上到下。
QBoxLayout::BottomToTop 3 垂直从下到上。

成员函数文档编制

[explicit] QBoxLayout:: QBoxLayout ( QBoxLayout::Direction dir , QWidget * parent = nullptr)

构造新 QBoxLayout 采用方向 dir 和父级小部件 parent .

将直接把布局设为顶层布局对于 parent 。Widget 只可以有一个顶层布局。返回它通过 QWidget::layout ().

另请参阅 direction () 和 QWidget::setLayout ().

[virtual noexcept] QBoxLayout:: ~QBoxLayout ()

销毁此框布局。

不销毁布局 Widget。

[override virtual] void QBoxLayout:: addItem ( QLayoutItem * item )

重实现: QLayout::addItem (QLayoutItem *item).

void QBoxLayout:: addLayout ( QLayout * layout , int stretch = 0)

添加 layout 到方框末尾,采用连续拉伸因子 stretch .

layout 变为框布局的子级。

另请参阅 insertLayout (), addItem (),和 addWidget ().

void QBoxLayout:: addSpacerItem ( QSpacerItem * spacerItem )

添加 spacerItem 到此方框布局的末尾。

所有权对于 spacerItem is passed to this layout.

另请参阅 addSpacing () 和 addStretch ().

void QBoxLayout:: addSpacing ( int size )

添加不可拉伸空间 ( QSpacerItem ) 采用尺寸 size 到此方框布局的末尾。 QBoxLayout 提供默认边距和间距。此函数添加额外空间。

另请参阅 insertSpacing (), addItem (),和 QSpacerItem .

void QBoxLayout:: addStretch ( int stretch = 0)

添加可拉伸空间 ( QSpacerItem ) 采用 0 最小尺寸和拉伸因子 stretch 到此方框布局的末尾。

另请参阅 insertStretch (), addItem (),和 QSpacerItem .

void QBoxLayout:: addStrut ( int size )

限制框的垂直尺度 (如:高度若框是 LeftToRight ) 到最小 size 。其它约束可能递增限制。

另请参阅 addItem ().

void QBoxLayout:: addWidget ( QWidget * widget , int stretch = 0, Qt::Alignment alignment = Qt::Alignment())

添加 widget 到此框布局的末尾,采用拉伸因子 stretch 和对齐 alignment .

拉伸因子仅适用于 direction QBoxLayout ,且相对于其它框与 Widget 在此 QBoxLayout 。具有更高拉伸因子的 Widget 与框增长更多。

若拉伸因子为 0 且什么都没有除非 QBoxLayout 拥有大于 0 的拉伸因子,空间分布将根据 QWidget :sizePolicy() 对于涉及的每个 Widget。

对齐方式的指定是通过 alignment 。默认对齐为 0,意味着 Widget 填充整个单元格。

widget becomes a child of the QLayout::parentWidget ().

另请参阅 insertWidget (), addItem (), addLayout (), addStretch (), addSpacing (),和 addStrut ().

[override virtual] int QBoxLayout:: count () const

重实现: QLayout::count () const.

QBoxLayout::Direction QBoxLayout:: direction () const

返回框的方向。 addWidget () 和 addSpacing () 工作于此方向;拉伸将在此方向延伸。

另请参阅 setDirection (), QBoxLayout::Direction , addWidget (),和 addSpacing ().

[override virtual] Qt::Orientations QBoxLayout:: expandingDirections () const

重实现: QLayout::expandingDirections () const.

[override virtual] bool QBoxLayout:: hasHeightForWidth () const

重实现: QLayoutItem::hasHeightForWidth () const.

[override virtual] int QBoxLayout:: heightForWidth ( int w ) const

重实现: QLayoutItem::heightForWidth (int) const.

void QBoxLayout:: insertItem ( int index , QLayoutItem * item )

插入 item 到此框布局在位置 index . Index must be either negative or within the range 0 to count (), inclusive. If index is negative or count (), the item is added at the end.

所有权对于 item is passed to this layout.

另请参阅 addItem (), insertWidget (), insertLayout (), insertStretch (),和 insertSpacing ().

void QBoxLayout:: insertLayout ( int index , QLayout * layout , int stretch = 0)

插入 layout 在位置 index ,采用拉伸因子 stretch 。若 index 为负,布局被添加在末尾。

layout 变为框布局的子级。

另请参阅 addLayout () 和 insertItem ().

void QBoxLayout:: insertSpacerItem ( int index , QSpacerItem * spacerItem )

插入 spacerItem 在位置 index ,采用 0 最小尺寸和拉伸因子。若 index 为负,空间被添加在末尾。

所有权对于 spacerItem is passed to this layout.

另请参阅 addSpacerItem (), insertStretch (),和 insertSpacing ().

void QBoxLayout:: insertSpacing ( int index , int size )

插入不可拉伸空间 ( QSpacerItem ) 在位置 index ,采用尺寸 size 。若 index 为负,空间被添加在末尾。

框布局具有默认的边距和间距。此函数添加额外空间。

另请参阅 addSpacing (), insertItem (),和 QSpacerItem .

void QBoxLayout:: insertStretch ( int index , int stretch = 0)

插入可拉伸空间 ( QSpacerItem ) 在位置 index ,采用 0 最小尺寸和拉伸因子 stretch 。若 index 为负,空间被添加在末尾。

另请参阅 addStretch (), insertItem (),和 QSpacerItem .

void QBoxLayout:: insertWidget ( int index , QWidget * widget , int stretch = 0, Qt::Alignment alignment = Qt::Alignment())

插入 widget 在位置 index ,采用拉伸因子 stretch 和对齐 alignment 。若 index 为负,Widget 被添加到末尾。

拉伸因子仅适用于 direction QBoxLayout ,且相对于其它框与 Widget 在此 QBoxLayout 。具有更高拉伸因子的 Widget 与框增长更多。

若拉伸因子为 0 且什么都没有除非 QBoxLayout 拥有大于 0 的拉伸因子,空间分布将根据 QWidget :sizePolicy() 对于涉及的每个 Widget。

对齐方式的指定是通过 alignment 。默认对齐为 0,意味着 Widget 填充整个单元格。

widget becomes a child of the QLayout::parentWidget ().

另请参阅 addWidget () 和 insertItem ().

[override virtual] void QBoxLayout:: invalidate ()

重实现: QLayout::invalidate ().

重置缓存信息。

[override virtual] QLayoutItem *QBoxLayout:: itemAt ( int index ) const

重实现: QLayout::itemAt (int index) const.

[override virtual] QSize QBoxLayout:: maximumSize () const

重实现: QLayout::maximumSize () const.

[override virtual] int QBoxLayout:: minimumHeightForWidth ( int w ) const

重实现: QLayoutItem::minimumHeightForWidth (int w) const.

[override virtual] QSize QBoxLayout:: minimumSize () const

重实现: QLayout::minimumSize () const.

void QBoxLayout:: setDirection ( QBoxLayout::Direction direction )

将此布局的方向设为 direction .

另请参阅 direction ().

[override virtual] void QBoxLayout:: setGeometry (const QRect & r )

重实现: QLayout::setGeometry (const QRect &r).

[override virtual] void QBoxLayout:: setSpacing ( int spacing )

重实现访问函数为特性: QLayout::spacing .

重实现 QLayout::setSpacing ()。把间距特性设为 spacing .

另请参阅 QLayout::setSpacing () 和 spacing ().

void QBoxLayout:: setStretch ( int index , int stretch )

设置拉伸因子在位置 index 。到 stretch .

另请参阅 stretch ().

bool QBoxLayout:: setStretchFactor ( QWidget * widget , int stretch )

设置拉伸因子为 widget to stretch 和返回 true 若 widget 在此布局中被找到 (不包括子级布局);否则返回 false .

另请参阅 setAlignment ().

bool QBoxLayout:: setStretchFactor ( QLayout * layout , int stretch )

设置拉伸因子为布局 layout to stretch 并返回 true if layout 在此布局中被找到 (不包括子级布局);否则返回 false .

这是重载函数。

[override virtual] QSize QBoxLayout:: sizeHint () const

重实现: QLayoutItem::sizeHint () const.

[override virtual] int QBoxLayout:: spacing () const

重实现访问函数为特性: QLayout::spacing .

重实现 QLayout::spacing ()。若间距特性有效,返回该值。否则,计算并返回间距特性的值。由于 Widget 布局间距从属样式,因此,若父级是 Widget,它会查询样式为布局 (水平或垂直) 间距。否则,若父级是布局,它会查询父级布局为 spacing()。

另请参阅 QLayout::spacing () 和 setSpacing ().

int QBoxLayout:: stretch ( int index ) const

返回拉伸因子在位置 index .

另请参阅 setStretch ().

[override virtual] QLayoutItem *QBoxLayout:: takeAt ( int index )

重实现: QLayout::takeAt (int index).