QLayout 類是幾何體管理器的基類。 更多...
| 頭: | #include <QLayout> |
| qmake: | QT += widgets |
| 繼承: | QObject and QLayoutItem |
| 繼承者: |
| enum | SizeConstraint { SetDefaultConstraint, SetFixedSize, SetMinimumSize, SetMaximumSize, SetMinAndMaxSize, SetNoConstraint } |
| QLayout () | |
| QLayout (QWidget * parent ) | |
| 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 ) |
| 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 , 布局管理 , 基本布局範例 , 邊框布局範例 ,和 流式布局範例 .
可能的值包括:
| 常量 | 值 | 描述 |
|---|---|---|
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。
必須將此布局插入另一布局,在幾何體管理將工作前。
構造新的頂層 QLayout,采用父級
parent
.
parent
可能不為
nullptr
.
將直接把布局設為頂層布局對於 parent 。Widget 隻可以有一個頂層布局。返迴它通過 QWidget::layout ().
重做布局為 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
返迴應覆蓋的矩形,當此布局的幾何體被設為 r ,假如此布局支持 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 () 且盡可能接近 size .
返迴圍繞布局所使用的邊距。
默認情況下, QLayout 使用由樣式提供的值。在大多數平颱,所有方嚮邊距為 11 像素。
該函數在 Qt 4.6 引入。
另請參閱 setContentsMargins ().
返迴布局的 geometry () 矩形,但要考慮內容邊距。
該函數在 Qt 4.3 引入。
另請參閱 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.
返迴此布局是否可以利用更多的空間相比 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 () const.
另請參閱 setGeometry ().
對於每個
left
,
top
,
right
and
bottom
不是
nullptr
,存儲指針引用位置命名的邊距大小。
默認情況下, 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 () 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()); }
[override virtual]
QLayout
*QLayout::
layout
()
重實現: QLayoutItem::layout ().
[override virtual]
QSize
QLayout::
maximumSize
() const
重實現: QLayoutItem::maximumSize () const.
返迴此布局的最大大小。這是布局可以擁有的最大大小,同時仍遵守規範。
返迴值不包括要求空間由 QWidget::setContentsMargins () 或 menuBar ().
默認實現允許不受限製重置大小。
返迴為此布局設置的菜單欄,或
nullptr
若未設置菜單欄。
另請參閱 setMenuBar ().
[override virtual]
QSize
QLayout::
minimumSize
() const
重實現: QLayoutItem::minimumSize () const.
返迴此布局的最小大小。這是布局可以擁有的最小大小,同時仍遵守規範。
返迴值不包括要求空間由 QWidget::setContentsMargins () 或 menuBar ().
默認實現允許不受限製重置大小。
返迴此布局的父級 Widget,或
nullptr
若此布局未安裝在任何 Widget 中。
若布局是子布局,此函數返迴父級布局的父級 Widget。
另請參閱 parent ().
移除布局項 item 從布局。刪除項是調用者的責任。
預告 item 可以是布局 (由於 QLayout 繼承 QLayoutItem ).
另請參閱 removeWidget () 和 addItem ().
移除小部件 widget 從布局。在調用此之後,調用者負責為 Widget 賦予閤理幾何體,或將 Widget 放迴布局,或明確隱藏它若有必要。
注意: 所有權對於 widget 仍然與添加時相同。
另請參閱 removeItem (), QWidget::setGeometry (),和 addWidget ().
搜索小部件
from
並將它替換為小部件
to
若找到。返迴布局項包含小部件
from
當成功時。否則
nullptr
被返迴。若
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 (const QRect &r).
另請參閱 geometry ().
告訴幾何體管理器放置菜單欄 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 }
更新布局為 parentWidget ().
通常,不需要調用此,因為它會被自動調用 (在最閤適時間)。
另請參閱 activate () 和 invalidate ().