QBoxLayout 类

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

头: #include <QBoxLayout>
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 setSpacing (int spacing )
void setStretch (int index , int stretch )
bool setStretchFactor (QWidget * widget , int stretch )
bool setStretchFactor (QLayout * layout , int stretch )
int spacing () const
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 QSize sizeHint () const override
virtual QLayoutItem * takeAt (int index ) override

详细描述

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

Horizontal box layout with five child widgets

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

Vertical box layout with five child widgets

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

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

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

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

  • addWidget () 以将 Widget 添加到 QVBoxLayout 并设置小部件的拉伸因子 (拉伸因子沿着框的行)。
  • 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 垂直从下到上。

成员函数文档编制

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

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

另请参阅 direction ().

[virtual] QBoxLayout:: ~QBoxLayout ()

销毁此框布局。

布局的 Widget 不被销毁。

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

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

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

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

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

void QBoxLayout:: addSpacerItem ( QSpacerItem * spacerItem )

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

该函数在 Qt 4.4 引入。

另请参阅 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 填充整个单元格。

另请参阅 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 为负,项被添加在末尾。

另请参阅 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 为负,空间被添加在末尾。

该函数在 Qt 4.4 引入。

另请参阅 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 填充整个单元格。

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

void QBoxLayout:: setSpacing ( int spacing )

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

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

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

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

该函数在 Qt 4.5 引入。

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

int QBoxLayout:: spacing () const

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

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

int QBoxLayout:: stretch ( int index ) const

返回拉伸因子在位置 index .

该函数在 Qt 4.5 引入。

另请参阅 setStretch ().

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

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