
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQLayout class is the base class of geometry managers.More...
| Header: | #include <QLayout> |
| Inherits: | QObject andQLayoutItem |
| Inherited By: |
| 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 |
| bool | isEnabled() const |
| virtual QLayoutItem * | itemAt(int index) const = 0 |
| QWidget * | menuBar() const |
| QWidget * | parentWidget() const |
| void | removeItem(QLayoutItem * item) |
| void | removeWidget(QWidget * widget) |
| bool | setAlignment(QWidget * w, Qt::Alignment alignment) |
| void | setAlignment(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 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) |
| 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) |
TheQLayout class is the base class of geometry managers.
This is an abstract base class inherited by the concrete classesQBoxLayout,QGridLayout,QFormLayout, andQStackedLayout.
For users ofQLayout subclasses or ofQMainWindow there is seldom any need to use the basic functions provided byQLayout, such assetSizeConstraint() orsetMenuBar(). SeeLayout Management for more information.
To make your own layout manager, implement the functionsaddItem(),sizeHint(),setGeometry(),itemAt() andtakeAt(). You should also implementminimumSize() to ensure your layout isn't resized to zero size if there is too little space. To support children whose heights depend on their widths, implementhasHeightForWidth() andheightForWidth(). See theBorder Layout andFlow Layout examples for more information about implementing custom layout managers.
Geometry management stops when the layout manager is deleted.
See alsoQLayoutItem,Layout Management,Basic Layouts Example,Border Layout Example, andFlow Layout Example.
The possible values are:
| Constant | Value | Description |
|---|---|---|
QLayout::SetDefaultConstraint | 0 | The main widget's minimum size is set tominimumSize(), unless the widget already has a minimum size. |
QLayout::SetFixedSize | 3 | The main widget's size is set tosizeHint(); it cannot be resized at all. |
QLayout::SetMinimumSize | 2 | The main widget's minimum size is set tominimumSize(); it cannot be smaller. |
QLayout::SetMaximumSize | 4 | The main widget's maximum size is set tomaximumSize(); it cannot be larger. |
QLayout::SetMinAndMaxSize | 5 | The main widget's minimum size is set tominimumSize() and its maximum size is set tomaximumSize(). |
QLayout::SetNoConstraint | 1 | The widget is not constrained. |
See alsosetSizeConstraint().
This property holds the resize mode of the layout.
The default mode isSetDefaultConstraint.
Access functions:
| SizeConstraint | sizeConstraint() const |
| void | setSizeConstraint(SizeConstraint) |
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.
ForQGridLayout andQFormLayout, it is possible to set different horizontal and vertical spacings usingsetHorizontalSpacing() andsetVerticalSpacing(). In that case, spacing() returns -1.
Access functions:
| int | spacing() const |
| void | setSpacing(int) |
See alsocontentsRect(),getContentsMargins(),QStyle::layoutSpacing(), andQStyle::pixelMetric().
Constructs a new top-levelQLayout, with parentparent.parent may not be 0.
There can be only one top-level layout for a widget. It is returned byQWidget::layout().
Constructs a new childQLayout.
This layout has to be inserted into another layout before geometry management will work.
Redoes the layout forparentWidget() 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.
See alsoupdate() andQWidget::updateGeometry().
[protected]void QLayout::addChildLayout(QLayout * l)This function is called fromaddLayout() orinsertLayout() functions in subclasses to add layoutl 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.
See alsoQBoxLayout::addLayout(),QBoxLayout::insertLayout(), andQGridLayout::addLayout().
[protected]void QLayout::addChildWidget(QWidget * w)This function is called fromaddWidget() functions in subclasses to addw as a managed widget of a layout.
Ifw is already managed by a layout, this function will give a warning and removew from that layout. This function must therefore be called before addingw to the layout's data structure.
[pure virtual]void QLayout::addItem(QLayoutItem * item)Implemented in subclasses to add anitem. 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 theaddWidget() function; to add a child layout, use the addLayout() function provided by the relevantQLayout subclass.
Note: The ownership ofitem is transferred to the layout, and it's the layout's responsibility to delete it.
See alsoaddWidget(),QBoxLayout::addLayout(), andQGridLayout::addLayout().
Adds widgetw to this layout in a manner specific to the layout. This function usesaddItem().
[protected]QRect QLayout::alignmentRect(constQRect & r) constReturns the rectangle that should be covered when the geometry of this layout is set tor, provided that this layout supportssetAlignment().
The result is derived fromsizeHint() and expanding(). It is never larger thanr.
[virtual protected]void QLayout::childEvent(QChildEvent * e)Reimplemented fromQObject::childEvent().
[static]QSize QLayout::closestAcceptableSize(constQWidget * widget, constQSize & size)Returns a size that satisfies all size constraints onwidget, includingheightForWidth() and that is as close as possible tosize.
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.
See alsosetContentsMargins().
Returns the layout'sgeometry() rectangle, but taking into account the contents margins.
This function was introduced in Qt 4.3.
See alsosetContentsMargins() andgetContentsMargins().
[pure virtual]int QLayout::count() constMust be implemented in subclasses to return the number of items in the layout.
See alsoitemAt().
[virtual]Qt::Orientations QLayout::expandingDirections() constReimplemented fromQLayoutItem::expandingDirections().
Returns whether this layout can make use of more space thansizeHint(). A value ofQt::Vertical orQt::Horizontal means that it wants to grow in only one dimension, whereasQt::Vertical |Qt::Horizontal means that it wants to grow in both dimensions.
The default implementation returnsQt::Horizontal |Qt::Vertical. Subclasses reimplement it to return a meaningful value based on their child widgets'ssize policies.
See alsosizeHint().
[virtual]QRect QLayout::geometry() constReimplemented fromQLayoutItem::geometry().
See alsosetGeometry().
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.
See alsosetContentsMargins(),QStyle::pixelMetric(),PM_LayoutLeftMargin,PM_LayoutTopMargin,PM_LayoutRightMargin, andPM_LayoutBottomMargin.
[virtual]int QLayout::indexOf(QWidget * widget) constSearches for widgetwidget in this layout (not including child layouts).
Returns the index ofwidget, or -1 ifwidget is not found.
The default implementation iterates over all items usingitemAt()
[virtual]void QLayout::invalidate()Reimplemented fromQLayoutItem::invalidate().
[virtual]bool QLayout::isEmpty() constReimplemented fromQLayoutItem::isEmpty().
Returns true if the layout is enabled; otherwise returns false.
See alsosetEnabled().
[pure virtual]QLayoutItem * QLayout::itemAt(int index) constMust be implemented in subclasses to return the layout item atindex. 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.
staticvoid 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()Reimplemented fromQLayoutItem::layout().
[virtual]QSize QLayout::maximumSize() constReimplemented fromQLayoutItem::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 byQWidget::setContentsMargins() ormenuBar().
The default implementation allows unlimited resizing.
Returns the menu bar set for this layout, or 0 if no menu bar is set.
See alsosetMenuBar().
[virtual]QSize QLayout::minimumSize() constReimplemented fromQLayoutItem::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 byQWidget::setContentsMargins() ormenuBar().
The default implementation allows unlimited resizing.
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.
See alsoparent().
Removes the layout itemitem from the layout. It is the caller's responsibility to delete the item.
Notice thatitem can be a layout (sinceQLayout inheritsQLayoutItem).
See alsoremoveWidget() andaddItem().
Removes the widgetwidget 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 ofwidget remains the same as when it was added.
See alsoremoveItem(),QWidget::setGeometry(), andaddWidget().
Sets the alignment for widgetw toalignment and returns true ifw is found in this layout (not including child layouts); otherwise returns false.
Sets the alignment of this item toalignment.
See alsoQLayoutItem::setAlignment().
This is an overloaded function.
Sets the alignment for the layoutl toalignment and returns true ifl is found in this layout (not including child layouts); otherwise returns false.
Sets theleft,top,right, andbottom 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.
See alsocontentsMargins(),getContentsMargins(),QStyle::pixelMetric(),PM_LayoutLeftMargin,PM_LayoutTopMargin,PM_LayoutRightMargin, andPM_LayoutBottomMargin.
Sets themargins 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.
See alsocontentsMargins().
Enables this layout ifenable 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.
See alsoisEnabled().
[virtual]void QLayout::setGeometry(constQRect & r)Reimplemented fromQLayoutItem::setGeometry().
See alsogeometry().
Tells the geometry manager to place the menu barwidget at the top ofparentWidget(), outsideQWidget::contentsMargins(). All child widgets are placed below the bottom edge of the menu bar.
See alsomenuBar().
[pure virtual]QLayoutItem * QLayout::takeAt(int index)Must be implemented in subclasses to remove the layout item atindex 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;}
Updates the layout forparentWidget().
You should generally not need to call this because it is automatically called at the most appropriate times.
See alsoactivate() andinvalidate().
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.