QLayout Class
头文件: | #include <QLayout> |
qmake: | QT += widgets |
基类: | QObject and QLayoutItem |
派生类: |
公有类型
enum | SizeConstraint { SetDefaultConstraint, SetFixedSize, SetMinimumSize, SetMaximumSize, SetMinAndMaxSize, SetNoConstraint } |
属性
- sizeConstraint : SizeConstraint
- spacing : int
- 1 个属性继承自 QObject
公有函数
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 |
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(SizeConstraint) |
void | setSpacing(int) |
SizeConstraint | sizeConstraint() const |
int | spacing() const |
virtual QLayoutItem * | takeAt(int index) = 0 |
void | update() |
重新实现的公有函数
virtual QSizePolicy::ControlTypes | controlTypes() const |
virtual Qt::Orientations | expandingDirections() const |
virtual QRect | geometry() const |
virtual void | invalidate() |
virtual bool | isEmpty() const |
virtual QLayout * | layout() |
virtual QSize | maximumSize() const |
virtual QSize | minimumSize() const |
virtual void | setGeometry(const QRect &r) |
- 32 个公有函数继承自 QObject
- 17 个公有函数继承自 QLayoutItem
静态公有成员
QSize | closestAcceptableSize(const QWidget *widget, const QSize &size) |
- 10 个静态公有成员继承自 QObject
受保护的函数
void | addChildLayout(QLayout *l) |
void | addChildWidget(QWidget *w) |
QRect | alignmentRect(const QRect &r) const |
重新实现的受保护函数
virtual void | childEvent(QChildEvent *e) |
- 9 个受保护的函数继承自 QObject
其他继承的成员
详细描述
QLayout 是几何管理器的基类.
QLayout是一个抽象类, 派生类是 QBoxLayout, QGridLayout, QFormLayout, QStackedLayout.
相较于 QLayout 子类或 QMainWindow, 你很少使用 QLayout的函数, 例如 setSizeConstraint() 或 setMenuBar(). 详见 布局管理.
创建自定义布局管理器, 你需要实现函数: addItem(), sizeHint(), setGeometry(), itemAt() 和 takeAt(). 你也应该实现 minimumSize() , 以确保空间太小时, 布局大小不会为0. 重新实现 hasHeightForWidth() 和 heightForWidth()支持高度随宽度变化. 有关自定义布局管理器, 详见示例 Border Layout 和 Flow Layout.
删除布局管理器后, 几何管理将停止.
参见 QLayoutItem, Layout Management, Basic Layouts Example, Border Layout Example, Flow Layout Example.
成员类型
enum QLayout::SizeConstraint
The possible values are:
Constant | Value | Description |
---|---|---|
QLayout::SetDefaultConstraint | 0 | The main widget's minimum size is set to minimumSize(), unless the widget already has a minimum size. |
QLayout::SetFixedSize | 3 | The main widget's size is set to sizeHint(); it cannot be resized at all. |
QLayout::SetMinimumSize | 2 | The main widget's minimum size is set to minimumSize(); it cannot be smaller. |
QLayout::SetMaximumSize | 4 | The main widget's maximum size is set to maximumSize(); it cannot be larger. |
QLayout::SetMinAndMaxSize | 5 | The main widget's minimum size is set to minimumSize() and its maximum size is set to maximumSize(). |
QLayout::SetNoConstraint | 1 | The widget is not constrained. |
参见 setSizeConstraint().
属性
sizeConstraint : SizeConstraint
This property holds the resize mode of the layout
The default mode is SetDefaultConstraint.
访问函数:
SizeConstraint | sizeConstraint() const |
void | setSizeConstraint(SizeConstraint) |
spacing : int
This property holds the spacing between widgets inside the layout
If no value is explicitly set, the layout's spacing is inherited from the parent layout, or from the style settings for the parent widget.
For QGridLayout and QFormLayout, it is possible to set different horizontal and vertical spacings using setHorizontalSpacing() and setVerticalSpacing(). In that case, spacing() returns -1.
访问函数:
int | spacing() const |
void | setSpacing(int) |
参见 contentsRect(), getContentsMargins(), QStyle::layoutSpacing(), and QStyle::pixelMetric().
成员函数
QLayout::QLayout(QWidget *parent)
Constructs a new top-level QLayout, with parent parent. parent may not be 0.
There can be only one top-level layout for a widget. It is returned by QWidget::layout().
QLayout::QLayout()
Constructs a new child QLayout.
This layout has to be inserted into another layout before geometry management will work.
bool QLayout::activate()
Redoes the layout for parentWidget() if necessary.
You should generally not need to call this because it is automatically called at the most appropriate times. It returns true if the layout was redone.
参见 update() and QWidget::updateGeometry().
[protected]
void QLayout::addChildLayout(QLayout *l)
This function is called from addLayout()
or insertLayout()
functions in subclasses to add layout l as a sub-layout.
The only scenario in which you need to call it directly is if you implement a custom layout that supports nested layouts.
参见 QBoxLayout::addLayout(), QBoxLayout::insertLayout(), and QGridLayout::addLayout().
[protected]
void QLayout::addChildWidget(QWidget *w)
This function is called from addWidget()
functions in subclasses to add w as a managed widget of a layout.
If w is already managed by a layout, this function will give a warning and remove w from that layout. This function must therefore be called before adding w to the layout's data structure.
[pure virtual]
void QLayout::addItem(QLayoutItem *item)
Implemented in subclasses to add an item. How it is added is specific to each subclass.
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 subclass.
Note: The ownership of item is transferred to the layout, and it's the layout's responsibility to delete it.
参见 addWidget(), QBoxLayout::addLayout(), and QGridLayout::addLayout().
void QLayout::addWidget(QWidget *w)
Adds widget w to this layout in a manner specific to the layout. This function uses 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().
The result is derived from sizeHint() and expanding(). It is never larger than r.
[virtual protected]
void QLayout::childEvent(QChildEvent *e)
重新实现 QObject::childEvent().
[static]
QSize QLayout::closestAcceptableSize(const QWidget *widget, const QSize &size)
Returns a size that satisfies all size constraints on widget, including heightForWidth() and that is as close as possible to size.
QMargins QLayout::contentsMargins() const
Returns the margins used around the layout.
By default, QLayout uses the values provided by the style. On most platforms, the margin is 11 pixels in all directions.
This function was introduced in Qt 4.6.
参见 setContentsMargins().
QRect QLayout::contentsRect() const
Returns the layout's geometry() rectangle, but taking into account the contents margins.
This function was introduced in Qt 4.3.
参见 setContentsMargins() and getContentsMargins().
[virtual]
QSizePolicy::ControlTypes QLayout::controlTypes() const
重新实现 QLayoutItem::controlTypes().
[pure virtual]
int QLayout::count() const
Must be implemented in subclasses to return the number of items in the layout.
参见 itemAt().
[virtual]
Qt::Orientations QLayout::expandingDirections() const
重新实现 QLayoutItem::expandingDirections().
Returns whether this layout can make use of more space than sizeHint(). A value of Qt::Vertical or Qt::Horizontal means that it wants to grow in only one dimension, whereas Qt::Vertical | Qt::Horizontal means that it wants to grow in both dimensions.
The default implementation returns Qt::Horizontal | Qt::Vertical. Subclasses reimplement it to return a meaningful value based on their child widgets's size policies.
参见 sizeHint().
[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).
By default, QLayout uses the values provided by the style. On most platforms, the margin is 11 pixels in all directions.
This function was introduced in Qt 4.3.
参见 setContentsMargins(), QStyle::pixelMetric(), PM_LayoutLeftMargin, PM_LayoutTopMargin, PM_LayoutRightMargin, and PM_LayoutBottomMargin.
[virtual]
int QLayout::indexOf(QWidget *widget) const
Searches for widget widget in this layout (not including child layouts).
Returns the index of widget, or -1 if widget is not found.
The default implementation iterates over all items using itemAt()
[virtual]
void QLayout::invalidate()
重新实现 QLayoutItem::invalidate().
[virtual]
bool QLayout::isEmpty() const
重新实现 QLayoutItem::isEmpty().
bool QLayout::isEnabled() const
Returns true
if the layout is enabled; otherwise returns false
.
参见 setEnabled().
[pure virtual]
QLayoutItem *QLayout::itemAt(int index) const
Must be implemented in subclasses to return the layout item at index. If there is no such item, the function must return 0. Items are numbered consecutively from 0. If an item is deleted, other items will be renumbered.
This function can be used to iterate over a layout. The following code will draw a rectangle for each layout item in the layout structure of the 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()); }
[virtual]
QLayout *QLayout::layout()
重新实现 QLayoutItem::layout().
[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.
The returned value doesn't include the space required by QWidget::setContentsMargins() or menuBar().
The default implementation allows unlimited resizing.
QWidget *QLayout::menuBar() const
Returns the menu bar set for this layout, or 0 if no menu bar is set.
参见 setMenuBar().
[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.
The returned value doesn't include the space required by QWidget::setContentsMargins() or menuBar().
The default implementation allows unlimited resizing.
QWidget *QLayout::parentWidget() const
Returns the parent widget of this layout, or 0 if this layout is not installed on any widget.
If the layout is a sub-layout, this function returns the parent widget of the parent layout.
参见 parent().
void QLayout::removeItem(QLayoutItem *item)
Removes the layout item item from the layout. It is the caller's responsibility to delete the item.
Notice that item can be a layout (since QLayout inherits QLayoutItem).
参见 removeWidget() and addItem().
void QLayout::removeWidget(QWidget *widget)
Removes the widget widget from the layout. After this call, it is the caller's responsibility to give the widget a reasonable geometry or to put the widget back into a layout.
Note: The ownership of widget remains the same as when it was added.
参见 removeItem(), QWidget::setGeometry(), and addWidget().
QLayoutItem *QLayout::replaceWidget(QWidget *from, QWidget *to, Qt::FindChildOptions options = Qt::FindChildrenRecursively)
Searches for widget from and replaces it with widget to if found. Returns the layout item that contains the widget from on success. Otherwise 0
is returned. If options contains Qt::FindChildrenRecursively
(the default), sub-layouts are searched for doing the replacement. Any other flag in options is ignored.
Notice that the returned item therefore might not belong to this layout, but to a sub-layout.
The returned layout item is no longer owned by the layout and should be either deleted or inserted to another layout. The widget from is no longer managed by the layout and may need to be deleted or hidden. The parent of widget from is left unchanged.
This function works for the built-in Qt layouts, but might not work for custom layouts.
This function was introduced in Qt 5.2.
参见 indexOf().
bool QLayout::setAlignment(QWidget *w, Qt::Alignment alignment)
Sets the alignment for widget w to alignment and returns true if w is found in this layout (not including child layouts); otherwise returns false
.
bool QLayout::setAlignment(QLayout *l, Qt::Alignment alignment)
This is an overloaded function.
Sets the alignment for the layout l to alignment and returns true
if l is found in this layout (not including child layouts); otherwise returns false
.
void QLayout::setContentsMargins(int left, int top, int right, int bottom)
Sets the left, top, right, and bottom margins to use around the layout.
By default, QLayout uses the values provided by the style. On most platforms, the margin is 11 pixels in all directions.
This function was introduced in Qt 4.3.
参见 contentsMargins(), getContentsMargins(), QStyle::pixelMetric(), PM_LayoutLeftMargin, PM_LayoutTopMargin, PM_LayoutRightMargin, and PM_LayoutBottomMargin.
void QLayout::setContentsMargins(const QMargins &margins)
Sets the margins to use around the layout.
By default, QLayout uses the values provided by the style. On most platforms, the margin is 11 pixels in all directions.
This function was introduced in Qt 4.6.
参见 contentsMargins().
void QLayout::setEnabled(bool enable)
Enables this layout if enable is true, otherwise disables it.
An enabled layout adjusts dynamically to changes; a disabled layout acts as if it did not exist.
By default all layouts are enabled.
参见 isEnabled().
[virtual]
void QLayout::setGeometry(const QRect &r)
重新实现 QLayoutItem::setGeometry().
参见 geometry().
void QLayout::setMenuBar(QWidget *widget)
Tells the geometry manager to place the menu bar widget at the top of parentWidget(), outside QWidget::contentsMargins(). All child widgets are placed below the bottom edge of the menu bar.
参见 menuBar().
[pure virtual]
QLayoutItem *QLayout::takeAt(int index)
Must be implemented in subclasses to remove the layout item at index from the layout, and return the item. If there is no such item, the function must do nothing and return 0. Items are numbered consecutively from 0. If an item is removed, other items will be renumbered.
The following code fragment shows a safe way to remove all items from a layout:
QLayoutItem *child; while ((child = layout->takeAt(0)) != 0) { ... delete child; }
void QLayout::update()
Updates the layout for parentWidget().
You should generally not need to call this because it is automatically called at the most appropriate times.
参见 activate() and invalidate().