本页内容

QBoxLayout Class

QBoxLayout 类可将子控件水平或垂直排列。更多内容...

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

QHBoxLayout 以及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() 获取),将其划分为一行框,并让每个受管理的控件填满一个框。

五个按钮呈水平排列

如果 QBoxLayout 的方向为Qt::Horizontal ,则这些框将按适当的大小排列成一行。每个小部件(或其他框)将至少获得其最小大小,至多获得其最大大小。任何多余的空间将根据拉伸因子进行分配(下文将对此进行详细说明)。

五个按钮,垂直排列

如果 QBoxLayout 的方向为 `Qt::Vertical`,则这些框将按列排列,同样具有适当的大小。

创建 QBoxLayout 最简单的方法是使用其中一个便捷类,例如QHBoxLayout (用于Qt::Horizontal 布局框)或QVBoxLayout (用于Qt::Vertical 布局框)。您也可以直接使用 QBoxLayout 构造函数,将其方向指定为LeftToRight 、RightToLeft 、TopToBottom 或BottomToTop 。

如果 QBoxLayout 不是顶级布局(即它并未管理小部件的全部区域及其子项),则必须先将其添加到父布局中,才能对其进行任何操作。添加布局的常规方法是调用 parentLayout->addLayout()。

完成此操作后,您可以使用以下四种函数之一向 QBoxLayout 添加框:

  • addWidget():将小部件添加到 QBoxLayout 并设置该小部件的拉伸因子。(拉伸因子沿框的行方向作用。)
  • addSpacing() 用于创建一个空框;这是用于创建美观且宽敞的对话框的函数之一。有关设置边距的方法,请参见下文。
  • addStretch() 用于创建一个空的、可拉伸的框。
  • addLayout() 用于向行中添加一个包含另一个QLayout 的框,并设置该布局的拉伸因子。

使用insertWidget()、insertSpacing()、insertStretch() 或insertLayout() 在布局的指定位置插入一个框。

QBoxLayout 还包含两种边距宽度:

  • setContentsMargins() 设置小部件各边的外边框宽度。这是沿 QBoxLayout 四边预留空间的宽度。
  • setSpacing() 用于设置相邻框之间的间距。(您可以使用addSpacing() 在特定位置获得更多空间。)

边距的默认值由样式提供。大多数 Qt 样式指定的默认边距为:子控件为 9,窗口为 11。间距默认与顶级布局的边距宽度相同,或与父布局的边距宽度相同。

要将小部件从布局中移除,请调用removeWidget()。对小部件调用QWidget::hide()也会将其从布局中有效移除,直到调用QWidget::show()为止。

由于QVBoxLayout 和QHBoxLayout 提供了便捷的构造函数,因此您几乎总是会优先使用它们,而不是 QBoxLayout。

另请参阅 QGridLayout 、QStackedLayout 以及《布局管理》。

成员类型文档

enum QBoxLayout::Direction

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

常量值描述
QBoxLayout::LeftToRight0从左到右的水平方向。
QBoxLayout::RightToLeft1从右向左的水平方向。
QBoxLayout::TopToBottom2从上到下水平方向。
QBoxLayout::BottomToTop3从下往上垂直排列。

成员函数文档

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

创建一个新的 QBoxLayout,其方向设置为dir ,父控件为parent 。

该布局将直接被设置为parent 的顶级布局。一个控件只能有一个顶级布局。该布局由QWidget::layout() 返回。

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

[virtual noexcept] QBoxLayout::~QBoxLayout()

销毁此盒子布局。

该布局中的控件不会被销毁。

[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 的所有权将传递给此布局。

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

void QBoxLayout::addSpacing(int size)

在该框布局的末尾添加一个不可拉伸的间隔(QSpacerItem ),其大小为size 。QBoxLayout 提供默认的边距和间距。此函数用于添加额外的间隔。

另请参阅 insertSpacing()、addItem() 和QSpacerItem 。

void QBoxLayout::addStretch(int stretch = 0)

在该盒子布局的末尾添加一个可伸缩区域(QSpacerItem ),其最小尺寸为零,伸缩因子为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 。

拉伸因子仅适用于QBoxLayout 中的direction ,且相对于该QBoxLayout 中的其他框和控件而言。拉伸因子较高的控件和框会扩展得更多。

如果拉伸因子为 0,且QBoxLayout 中没有其他元素的拉伸因子大于零,则空间将根据每个相关小部件的QWidget::sizePolicy() 进行分配。

对齐方式由alignment 指定。默认对齐值为 0,这意味着小部件将填满整个单元格。

widget 成为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 。索引必须为负数,或位于0到count()的范围内(包含两端)。如果index 为负数或count(),则该项目将被添加到末尾。

item 的所有权将传递给此布局。

另请参阅 addItem()、insertWidget()、insertLayout()、insertStretch() 以及insertSpacing()。

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

在位置index 处插入layout ,拉伸因子为stretch 。如果index 为负值,则该布局将被添加到末尾。

layout 成为盒式布局的子元素。

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

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

在位置index 处插入spacerItem ,最小尺寸和拉伸因子均为零。如果index 为负值,则该空间将添加在末尾。

spacerItem 的所有权将传递给此布局。

另请参阅 addSpacerItem()、insertStretch() 和insertSpacing()。

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

在位置index 处插入一个不可拉伸的空格(QSpacerItem ),大小为size 。如果index 为负值,则该空格将添加在末尾。

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

另请参阅 addSpacing()、insertItem() 和QSpacerItem 。

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

在位置index 处插入一个可伸缩空格(QSpacerItem ),其最小大小为零,伸缩因子为stretch 。如果index 为负值,则该空格将添加在末尾。

另请参阅 addStretch()、insertItem() 和QSpacerItem 。

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

在位置index 处插入widget ,伸缩因子为stretch ,对齐方式为alignment 。如果index 为负值,则将该控件添加到末尾。

拉伸因子仅适用于QBoxLayout 的direction ,且相对于该QBoxLayout 中的其他框和控件而言。拉伸因子较高的控件和框会扩展得更多。

如果拉伸因子为 0,且该QBoxLayout 中没有其他元素的拉伸因子大于零,则空间将根据每个相关小部件的QWidget::sizePolicy()进行分配。

对齐方式由alignment 指定。默认对齐值为 0,这意味着小部件将填满整个单元格。

widget 成为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 属性设置为spacing 。

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

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

将位置index 处的拉伸因子设置为stretch 。

另请参阅 stretch()。

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

将widget 的拉伸因子设置为stretch ,如果在此布局中(不包括子布局)找到了widget ,则返回true;否则返回false 。

另请参阅 setAlignment()。

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

将布局layout 的拉伸因子设置为stretch ,并在该布局(不包括子布局)中找到layout 时返回true ;否则返回false 。

这是一个重载函数。

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

重新实现了:QLayoutItem::sizeHint() const。

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

重新实现了属性QLayout::spacing 的访问函数。

重新实现了QLayout::spacing() 方法。如果 spacing 属性有效,则返回该值。 否则,将计算并返回 spacing 属性的值。由于小部件中的布局间距取决于样式,如果父元素是小部件,则会查询样式以获取布局的(水平或垂直)间距;否则,父元素是布局,则会查询父布局的 spacing() 属性。

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

int QBoxLayout::stretch(int index) const

返回位置index 处的拉伸因子。

另请参阅 setStretch()。

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

重写了:QLayout::takeAt(int index)。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.