QLayout 类

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

头: #include <QLayout>
qmake: QT += widgets
继承: QObject and QLayoutItem
继承者: QBoxLayout , QFormLayout , QGridLayout ,和 QStackedLayout

公共类型

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

特性

公共函数

QLayout (QWidget * parent )
QLayout ()
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 (QWidget * widget ) const
int indexOf (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 )
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 )
void setSpacing ( int )
QLayout::SizeConstraint sizeConstraint () const
int spacing () const
virtual QLayoutItem * takeAt (int index ) = 0
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 )
const QMetaObject staticMetaObject

保护函数

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

重实现保护函数

virtual void childEvent (QChildEvent * e ) override

额外继承成员

详细描述

The QLayout 类是几何体管理器的基类。

此抽象基类继承通过具体类 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。

访问函数:

int spacing () const
void setSpacing ( int )

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

成员函数文档编制

QLayout:: QLayout ( QWidget * parent )

构造新的顶层 QLayout ,采用父级 parent . parent may not be a nullptr .

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

QLayout:: QLayout ()

构造新的子级 QLayout .

必须将此布局插入另一布局,在几何体管理将工作之前。

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

[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 像素。

该函数在 Qt 4.6 引入。

另请参阅 setContentsMargins ().

QRect QLayout:: contentsRect () const

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

该函数在 Qt 4.3 引入。

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

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

重实现自 QLayoutItem::controlTypes ().

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

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

另请参阅 itemAt ().

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

重实现自 QLayoutItem::expandingDirections ().

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

另请参阅 setGeometry ().

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

Extracts the left, top, right, and bottom margins used around the layout, and assigns them to * left , * top , * right , and * bottom (unless they are null pointers).

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

该函数在 Qt 4.3 引入。

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

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

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

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

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

int QLayout:: indexOf ( 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 ().

bool QLayout:: isEnabled () const

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

另请参阅 setEnabled ().

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

必须在子类中实现以返回布局项在 index 。若没有这种项,函数必须返回 0。项从 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 ().

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

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

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

另请参阅 setMenuBar ().

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

重实现自 QLayoutItem::minimumSize ().

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,或 0 若此布局未安装在任何 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 ().

QLayoutItem *QLayout:: replaceWidget ( QWidget * from , QWidget * to , Qt::FindChildOptions options = Qt::FindChildrenRecursively)

搜索小部件 from 并将它替换为小部件 to 若找到。返回布局项包含小部件 from 当成功时。否则 0 被返回。若 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 像素。

该函数在 Qt 4.3 引入。

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

void QLayout:: setContentsMargins (const QMargins & margins )

设置 margins 以围绕布局使用。

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

该函数在 Qt 4.6 引入。

另请参阅 contentsMargins ().

void QLayout:: setEnabled ( bool enable )

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

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

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

另请参阅 isEnabled ().

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

重实现自 QLayoutItem::setGeometry ().

另请参阅 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)) != 0) {
    ...
    delete child;
}
					

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

void QLayout:: update ()

更新布局为 parentWidget ().

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

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