QLayout 类

QLayout 类是几何体管理器的基类。 更多...

头: #include <QLayout>
CMake: find_package(Qt6 COMPONENTS Widgets REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承: QObject and QLayoutItem
继承者: QBoxLayout , QFormLayout , QGridLayout ,和 QStackedLayout

公共类型

enum SizeConstraint { SetDefaultConstraint, SetFixedSize, SetMinimumSize, SetMaximumSize, SetMinAndMaxSize, SetNoConstraint }

特性

公共函数

  QLayout (QWidget * parent = nullptr)
bool activate ()
virtual void addItem (QLayoutItem * item ) = 0
void addWidget (QWidget * w )
QMargins contentsMargins () const
QRect contentsRect () const
virtual int count () const = 0
void getContentsMargins (int * left , int * top , int * right , int * bottom ) const
virtual int indexOf (const QWidget * widget ) const
virtual int indexOf (const QLayoutItem * layoutItem ) const
bool isEnabled () const
virtual QLayoutItem * itemAt (int index ) const = 0
QWidget * menuBar () const
QWidget * parentWidget () const
void removeItem (QLayoutItem * item )
void removeWidget (QWidget * widget )
virtual QLayoutItem * replaceWidget (QWidget * from , QWidget * to , Qt::FindChildOptions options = Qt::FindChildrenRecursively)
bool setAlignment (QWidget * w , Qt::Alignment alignment )
bool setAlignment (QLayout * l , Qt::Alignment alignment )
void setContentsMargins (int left , int top , int right , int bottom )
void setContentsMargins (const QMargins & margins )
void setEnabled (bool enable )
void setMenuBar (QWidget * widget )
void setSizeConstraint (QLayout::SizeConstraint)
virtual void setSpacing (int)
QLayout::SizeConstraint sizeConstraint () const
virtual int spacing () const
virtual QLayoutItem * takeAt (int index ) = 0
void unsetContentsMargins ()
void update ()

重实现公共函数

virtual QSizePolicy::ControlTypes controlTypes () const override
virtual Qt::Orientations expandingDirections () const override
virtual QRect geometry () const override
virtual void invalidate () override
virtual bool isEmpty () const override
virtual QLayout * layout () override
virtual QSize maximumSize () const override
virtual QSize minimumSize () const override
virtual void setGeometry (const QRect & r ) override

静态公共成员

QSize closestAcceptableSize (const QWidget * widget , const QSize & size )

保护函数

void addChildLayout (QLayout * l )
void addChildWidget (QWidget * w )
QRect alignmentRect (const QRect & r ) const

重实现保护函数

virtual void childEvent (QChildEvent * e ) override

详细描述

此抽象基类继承通过具体类 QBoxLayout , QGridLayout , QFormLayout ,和 QStackedLayout .

对于用户,QLayout 子类或 QMainWindow 很少需要用到由 QLayout 提供的基本功能,譬如 setSizeConstraint () 或 setMenuBar ()。见 布局管理 了解更多信息。

要制作自己的布局管理器,实现函数 addItem (), sizeHint (), setGeometry (), itemAt () 和 takeAt ()。还应实现 minimumSize () 以确保布局不会重置大小为 0 (若空间太小)。要支持高度从属其宽度的子级,实现 hasHeightForWidth () 和 heightForWidth ()。见 边框布局 and 流式布局 范例了解实现自定义布局管理器的更多有关信息。

几何体管理停止,当布局管理器被删除时。

另请参阅 QLayoutItem , 布局管理 , 基本布局范例 , 边框布局范例 ,和 流式布局范例 .

成员类型文档编制

enum QLayout:: SizeConstraint

可能的值包括:

常量 描述
QLayout::SetDefaultConstraint 0 将主 Widget 的最小大小设为 minimumSize (),除非小部件已经拥有最小大小。
QLayout::SetFixedSize 3 将主 Widget 的大小设为 sizeHint ();它根本无法重置大小。
QLayout::SetMinimumSize 2 将主 Widget 的最小大小设为 minimumSize ();它无法更小。
QLayout::SetMaximumSize 4 将主 Widget 的最大大小设为 maximumSize ();它无法更大。
QLayout::SetMinAndMaxSize 5 将主 Widget 的最小大小设为 minimumSize () 并将其最大大小设为 maximumSize ().
QLayout::SetNoConstraint 1 Widget 不受约束。

另请参阅 setSizeConstraint ().

特性文档编制

sizeConstraint : SizeConstraint

此特性保持布局的重置尺寸模式

默认模式为 SetDefaultConstraint .

访问函数:

QLayout::SizeConstraint sizeConstraint () const
void setSizeConstraint (QLayout::SizeConstraint)

spacing : int

此特性保持布局内 Widget 之间的间距

若值未明确设置,布局间距从父级布局或父级 Widget 样式设置继承。

For QGridLayout and QFormLayout ,它是可能的设置不同水平和垂直间距使用 setHorizontalSpacing() and setVerticalSpacing() 。在此情况下,spacing() 返回 -1。

访问函数:

virtual int spacing () const
virtual void setSpacing (int)

另请参阅 contentsRect (), getContentsMargins (), QStyle::layoutSpacing (),和 QStyle::pixelMetric ().

成员函数文档编制

QLayout:: QLayout ( QWidget * parent = nullptr)

构造新的顶层 QLayout,采用父级 parent .

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

parent is nullptr ,那么必须将此布局插入另一布局,或将其设为 Widget 布局使用 QWidget::setLayout ().

另请参阅 QWidget::setLayout ().

bool QLayout:: activate ()

重做布局为 parentWidget () 若有必要。

一般应该不需要调用此,因为在最合适的时候会自动调用它。返回 true 若布局有重做。

另请参阅 update () 和 QWidget::updateGeometry ().

[protected] void QLayout:: addChildLayout ( QLayout * l )

此函数被调用从 addLayout() or insertLayout() 函数在子类中添加布局 l 作为子布局。

需要直接调用它的唯一情景,是实现支持嵌套布局的自定义布局。

另请参阅 QBoxLayout::addLayout (), QBoxLayout::insertLayout (),和 QGridLayout::addLayout ().

[protected] void QLayout:: addChildWidget ( QWidget * w )

此函数被调用从 addWidget() 函数在子类中添加 w 作为布局管理 Widget。

w 已经被布局管理,此函数将给出警告并移除 w 从该布局。因此,必须调用此函数先于添加 w 到布局的数据结构。

[pure virtual] void QLayout:: addItem ( QLayoutItem * item )

在子类中实现以添加 item 。如何添加它特定于每个子类。

This function is not usually called in application code. To add a widget to a layout, use the addWidget () function; to add a child layout, use the addLayout() function provided by the relevant QLayout 子类。

注意: 所有权对于 item is transferred to the layout, and it's the layout's responsibility to delete it.

另请参阅 addWidget (), QBoxLayout::addLayout (),和 QGridLayout::addLayout ().

void QLayout:: addWidget ( QWidget * w )

添加 Widget w 到此布局以特定布局方式。此函数使用 addItem ().

[protected] QRect QLayout:: alignmentRect (const QRect & r ) const

Returns the rectangle that should be covered when the geometry of this layout is set to r , provided that this layout supports setAlignment ().

结果派生自 sizeHint () 和 expanding()。它从不大于 r .

[override virtual protected] void QLayout:: childEvent ( QChildEvent * e )

重实现: QObject::childEvent (QChildEvent *event).

[static] QSize QLayout:: closestAcceptableSize (const QWidget * widget , const QSize & size )

返回大小满足所有大小约束在 widget ,包括 heightForWidth () and that is as close as possible to size .

QMargins QLayout:: contentsMargins () const

返回围绕布局所使用的边距。

默认情况下, QLayout 使用由样式提供的值。在大多数平台,所有方向边距为 11 像素。

注意: Getter 函数对于特性 contentsMargins。

另请参阅 setContentsMargins ().

QRect QLayout:: contentsRect () const

返回布局的 geometry () 矩形,但要考虑内容边距。

另请参阅 setContentsMargins () 和 getContentsMargins ().

[override virtual] QSizePolicy::ControlTypes QLayout:: controlTypes () const

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

[pure virtual] int QLayout:: count () const

必须在子类中实现以返回布局项数。

另请参阅 itemAt ().

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

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

Returns whether this layout can make use of more space than sizeHint ()。值 Qt::Vertical or Qt::Horizontal 意味着只想按 1 维增长,而 Qt::Vertical | Qt::Horizontal 意味着想要按 2 维增长。

默认实现返回 Qt::Horizontal | Qt::Vertical . Subclasses reimplement it to return a meaningful value based on their child widgets's 大小策略 .

另请参阅 sizeHint ().

[override virtual] QRect QLayout:: geometry () const

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

另请参阅 setGeometry ().

void QLayout:: getContentsMargins ( int * left , int * top , int * right , int * bottom ) const

For each of left , top , right and bottom that is not nullptr , stores the size of the margin named in the location the pointer refers to.

默认情况下, QLayout 使用由样式提供的值。在大多数平台,所有方向边距为 11 像素。

另请参阅 setContentsMargins (), QStyle::pixelMetric (), PM_LayoutLeftMargin , PM_LayoutTopMargin , PM_LayoutRightMargin ,和 PM_LayoutBottomMargin .

[virtual] int QLayout:: indexOf (const QWidget * widget ) const

搜索小部件 widget 在此布局 (不包括子级布局)。

返回索引对于 widget ,或 -1 若 widget 找不到。

默认实现遍历所有项,使用 itemAt ().

[virtual, since 5.12] int QLayout:: indexOf (const QLayoutItem * layoutItem ) const

搜索布局项 layoutItem 在此布局 (不包括子级布局)。

返回索引对于 layoutItem ,或 -1 若 layoutItem 找不到。

该函数在 Qt 5.12 引入。

[override virtual] void QLayout:: invalidate ()

重实现: QLayoutItem::invalidate ().

[override virtual] bool QLayout:: isEmpty () const

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

bool QLayout:: isEnabled () const

返回 true 若布局被启用;否则返回 false .

另请参阅 setEnabled ().

[pure virtual] QLayoutItem *QLayout:: itemAt ( int index ) const

必须在子类中实现以返回布局项在 index 。若没有这样的项,函数必须返回 nullptr 。项从 0 开始连续编号。若项被删除,将重新编号其它项。

此函数可用于迭代布局。以下代码将为 Widget 布局结构中的每个布局项绘制矩形。

static void paintLayout(QPainter *painter, QLayoutItem *item)
{
    QLayout *layout = item->layout();
    if (layout) {
        for (int i = 0; i < layout->count(); ++i)
            paintLayout(painter, layout->itemAt(i));
    }
    painter->drawRect(item->geometry());
}
void MyWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    if (layout())
        paintLayout(&painter, layout());
}
					

另请参阅 count () 和 takeAt ().

[override virtual] QLayout *QLayout:: layout ()

重实现: QLayoutItem::layout ().

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

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

Returns the maximum size of this layout. This is the largest size that the layout can have while still respecting the specifications.

返回值不包括要求空间由 QWidget::setContentsMargins () 或 menuBar ().

默认实现允许不受限制重置大小。

QWidget *QLayout:: menuBar () const

返回为此布局设置的菜单栏,或 nullptr 若未设置菜单栏。

另请参阅 setMenuBar ().

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

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

Returns the minimum size of this layout. This is the smallest size that the layout can have while still respecting the specifications.

返回值不包括要求空间由 QWidget::setContentsMargins () 或 menuBar ().

默认实现允许不受限制重置大小。

QWidget *QLayout:: parentWidget () const

返回此布局的父级 Widget,或 nullptr 若此布局未安装在任何 Widget 中。

若布局是子布局,此函数返回父级布局的父级 Widget。

另请参阅 parent ().

void QLayout:: removeItem ( QLayoutItem * item )

移除布局项 item 从布局。删除项是调用者的责任。

预告 item 可以是布局 (由于 QLayout 继承 QLayoutItem ).

另请参阅 removeWidget () 和 addItem ().

void QLayout:: removeWidget ( QWidget * widget )

移除小部件 widget 从布局。在调用此之后,调用者负责为 Widget 赋予合理几何体,或将 Widget 放回布局,或明确隐藏它若有必要。

注意: 所有权对于 widget 仍然与添加时相同。

另请参阅 removeItem (), QWidget::setGeometry (),和 addWidget ().

[virtual, since 5.2] QLayoutItem *QLayout:: replaceWidget ( QWidget * from , QWidget * to , Qt::FindChildOptions options = Qt::FindChildrenRecursively)

搜索小部件 from 并将它替换为小部件 to 若找到。返回布局项包含小部件 from 当成功时。否则 nullptr 被返回。若 options 包含 Qt::FindChildrenRecursively (默认),搜索子布局为履行替换。任何其它标志在 options 被忽略。

注意,返回项因此可能不属于此布局,而是属于子布局。

返回的布局项不再由布局所拥有,且应该被删除或插入另一布局。小部件 from 不再由布局进行管理,且可能需要删除或隐藏。父级对于小部件 from 保持不变。

此函数适于内置 Qt 布局,但可能不适于自定义布局。

该函数在 Qt 5.2 引入。

另请参阅 indexOf ().

bool QLayout:: setAlignment ( QWidget * w , Qt::Alignment alignment )

设置对齐方式为小部件 w to alignment 和返回 true 若 w 在此布局中被找到 (不包括子级布局);否则返回 false .

bool QLayout:: setAlignment ( QLayout * l , Qt::Alignment alignment )

这是重载函数。

设置对齐方式为布局 l to alignment 并返回 true if l 在此布局中被找到 (不包括子级布局);否则返回 false .

void QLayout:: setContentsMargins ( int left , int top , int right , int bottom )

设置 left , top , right ,和 bottom 边距以用于布局周围。

默认情况下, QLayout 使用由样式提供的值。在大多数平台,所有方向边距为 11 像素。

注意: Setter 函数对于特性 contentsMargins .

另请参阅 contentsMargins (), getContentsMargins (), QStyle::pixelMetric (), PM_LayoutLeftMargin , PM_LayoutTopMargin , PM_LayoutRightMargin ,和 PM_LayoutBottomMargin .

void QLayout:: setContentsMargins (const QMargins & margins )

设置 margins 以围绕布局使用。

默认情况下, QLayout 使用由样式提供的值。在大多数平台,所有方向边距为 11 像素。

注意: Setter 函数对于特性 contentsMargins .

另请参阅 contentsMargins ().

void QLayout:: setEnabled ( bool enable )

启用此布局,若 enable 为 true,否则禁用它。

启用布局会根据变化动态调节;禁用布局就像它不存在。

默认情况下,所有布局是启用的。

另请参阅 isEnabled ().

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

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

另请参阅 geometry ().

void QLayout:: setMenuBar ( QWidget * widget )

告诉几何体管理器放置菜单栏 widget 在顶部对于 parentWidget (),超出 QWidget::contentsMargins ()。所有子级 Widget 放在菜单栏下边缘下方。

另请参阅 menuBar ().

[pure virtual] QLayoutItem *QLayout:: takeAt ( int index )

必须在子类中实现以移除布局项,在 index 从布局,并返回项。若没有这种项,函数必须什么都不做,并返回 0。项从 0 起被连续编号。若项被移除,其它项将被重新编号。

下列代码片段展示从布局,安全移除所有项的办法:

QLayoutItem *child;
while ((child = layout->takeAt(0)) != nullptr) {
    ...
    delete child->widget(); // delete the widget
    delete child;   // delete the layout item
}
					

另请参阅 itemAt () 和 count ().

[since 6.1] void QLayout:: unsetContentsMargins ()

取消围绕布局的任何用户定义边距设置。布局将使用由样式提供的默认值。

注意: Resetter 函数对于特性 contentsMargins .

该函数在 Qt 6.1 引入。

另请参阅 setContentsMargins ().

void QLayout:: update ()

更新布局为 parentWidget ().

通常,不需要调用此,因为它会被自动调用 (在最合适时间)。

另请参阅 activate () 和 invalidate ().