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 , 布局管理 , 基本布局范例 , 边框布局范例 ,和 流式布局范例 .
可能的值包括:
| 常量 | 值 | 描述 | 
|---|---|---|
| 
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 ().
此特性保持布局的重置尺寸模式
默认模式为 SetDefaultConstraint .
访问函数:
| QLayout::SizeConstraint | sizeConstraint () const | 
| void | setSizeConstraint ( QLayout::SizeConstraint ) | 
此特性保持布局内 Widget 之间的间距
若值未明确设置,布局间距从父级布局或父级 Widget 样式设置继承。
For QGridLayout and QFormLayout ,它是可能的设置不同水平和垂直间距使用 setHorizontalSpacing() and setVerticalSpacing() 。在此情况下,spacing() 返回 -1。
访问函数:
| int | spacing () const | 
| void | setSpacing ( int ) | 
另请参阅 contentsRect (), getContentsMargins (), QStyle::layoutSpacing (),和 QStyle::pixelMetric ().
						构造新的顶层
						
							QLayout
						
						,采用父级
						
							parent
						
						.
						
							parent
						
						may not be a
						
nullptr
						
						.
					
将直接把布局设为顶层布局对于 parent 。Widget 只可以有一个顶层布局。返回它通过 QWidget::layout ().
构造新的子级 QLayout .
必须将此布局插入另一布局,在几何体管理将工作之前。
重做布局为 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 。如何添加它特定于每个子类。
通常,不会在应用程序代码中调用此函数。要将 Widget 添加到布局,使用 addWidget () 函数;要添加子级布局,使用 addLayout() 函数提供通过相关 QLayout 子类。
注意: 所有权对于 item 会被转移给布局,而删除它是布局的责任。
另请参阅 addWidget (), QBoxLayout::addLayout (),和 QGridLayout::addLayout ().
添加 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 () 且尽可能接近 size .
返回围绕布局所使用的边距。
默认情况下, QLayout 使用由样式提供的值。在大多数平台,所有方向边距为 11 像素。
该函数在 Qt 4.6 引入。
另请参阅 setContentsMargins ().
返回布局的 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 ().
返回此布局是否可以利用更多的空间相比 sizeHint ()。值 Qt::Vertical or Qt::Horizontal 意味着只想按 1 维增长,而 Qt::Vertical | Qt::Horizontal 意味着想要按 2 维增长。
默认实现返回 Qt::Horizontal | Qt::Vertical 。子类重实现它,以返回有意义值基于其子级 Widget 的 大小策略 .
另请参阅 sizeHint ().
[override virtual]
						
						
							
								QRect
							
						
						QLayout::
						
							geometry
						
						() const
						
					重实现自 QLayoutItem::geometry ().
另请参阅 setGeometry ().
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 .
[虚拟]
						
						
							int
						
						QLayout::
						
							indexOf
						
						(
						
							
								QWidget
							
						
						*
						
							widget
						
						) const
						
					搜索小部件 widget 在此布局 (不包括子级布局)。
返回索引对于 widget ,或 -1 若 widget 找不到。
默认实现遍历所有项,使用 itemAt ()
搜索布局项 layoutItem 在此布局 (不包括子级布局)。
返回索引对于 layoutItem ,或 -1 若 layoutItem 找不到。
该函数在 Qt 5.12 引入。
[override virtual]
						
						
							void
						
						QLayout::
						
							invalidate
						
						()
						
					重实现自 QLayoutItem::invalidate ().
[override virtual]
						
						
							bool
						
						QLayout::
						
							isEmpty
						
						() const
						
					重实现自 QLayoutItem::isEmpty ().
						返回
						
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()); }
[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 ().
默认实现允许不受限制重置大小。
返回此布局的父级 Widget,或 0 若此布局未安装在任何 Widget 中。
若布局是子布局,此函数返回父级布局的父级 Widget。
另请参阅 parent ().
移除布局项 item 从布局。删除项是调用者的责任。
预告 item 可以是布局 (由于 QLayout 继承 QLayoutItem ).
另请参阅 removeWidget () 和 addItem ().
移除小部件 widget 从布局。在调用此之后,调用者负责为 Widget 赋予合理几何体,或将 Widget 放回布局,或明确隐藏它若有必要。
注意: 所有权对于 widget 仍然与添加时相同。
另请参阅 removeItem (), QWidget::setGeometry (),和 addWidget ().
						搜索小部件
						
							from
						
						并将它替换为小部件
						
							to
						
						若找到。返回布局项包含小部件
						
							from
						
						当成功时。否则
						
0
						
						被返回。若
						
							options
						
						包含
						
Qt::FindChildrenRecursively
						
						(默认),搜索子布局为履行替换。任何其它标志在
						
							options
						
						被忽略。
					
注意,返回项因此可能不属于此布局,而是属于子布局。
返回的布局项不再由布局所拥有,且应该被删除或插入另一布局。小部件 from 不再由布局进行管理,且可能需要删除或隐藏。父级对于小部件 from 保持不变。
此函数适于内置 Qt 布局,但可能不适于自定义布局。
该函数在 Qt 5.2 引入。
另请参阅 indexOf ().
						设置对齐方式为小部件
						
							w
						
						to
						
							alignment
						
						和返回 true 若
						
							w
						
						在此布局中被找到 (不包括子级布局);否则返回
						
false
						
						.
					
这是重载函数。
						设置对齐方式为布局
						
							l
						
						to
						
							alignment
						
						并返回
						
true
						
						if
						
							l
						
						在此布局中被找到 (不包括子级布局);否则返回
						
false
						
						.
					
设置 left , top , right ,和 bottom 边距以用于布局周围。
默认情况下, QLayout 使用由样式提供的值。在大多数平台,所有方向边距为 11 像素。
该函数在 Qt 4.3 引入。
另请参阅 contentsMargins (), getContentsMargins (), QStyle::pixelMetric (), PM_LayoutLeftMargin , PM_LayoutTopMargin , PM_LayoutRightMargin ,和 PM_LayoutBottomMargin .
设置 margins 以围绕布局使用。
默认情况下, QLayout 使用由样式提供的值。在大多数平台,所有方向边距为 11 像素。
该函数在 Qt 4.6 引入。
另请参阅 contentsMargins ().
启用此布局,若 enable 为 true,否则禁用它。
启用布局会根据变化动态调节;禁用布局就像它不存在。
默认情况下,所有布局是启用的。
另请参阅 isEnabled ().
[override virtual]
						
						
							void
						
						QLayout::
						
							setGeometry
						
						(const
						
							
								QRect
							
						
						&
						
							r
						
						)
						
					重实现自 QLayoutItem::setGeometry ().
另请参阅 geometry ().
告诉几何体管理器放置菜单栏 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; }
更新布局为 parentWidget ().
通常,不需要调用此,因为它会被自动调用 (在最合适时间)。
另请参阅 activate () 和 invalidate ().