|  | Home · All Classes · Modules

# QLayout Class Reference
[QtGui module]

The QLayout class is the base class of geometry managers. More...

Inherits QObject and QLayoutItem.

Inherited by QBoxLayout, QFormLayout, QGridLayout and QStackedLayout.

### Types

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

### Methods

- __init__ (self, QWidget parent)
- __init__ (self)
- bool activate (self)
- addChildLayout (self, QLayout l)
- addChildWidget (self, QWidget w)
- addItem (self, QLayoutItem)
- addWidget (self, QWidget w)
- QRect alignmentRect (self, QRect)
- childEvent (self, QChildEvent e)
- QMargins contentsMargins (self)
- QRect contentsRect (self)
- int count (self)
- Qt.Orientations expandingDirections (self)
- QRect geometry (self)
- (int left, int top, int right, int bottom) getContentsMargins (self)
- int indexOf (self, QWidget)
- invalidate (self)
- bool isEmpty (self)
- bool isEnabled (self)
- QLayoutItem itemAt (self, int index)
- QLayout layout (self)
- int margin (self)
- QSize maximumSize (self)
- QWidget menuBar (self)
- QSize minimumSize (self)
- QWidget parentWidget (self)
- removeItem (self, QLayoutItem)
- removeWidget (self, QWidget w)
- bool setAlignment (self, QWidget w, Qt.Alignment alignment)
- bool setAlignment (self, QLayout l, Qt.Alignment alignment)
- setAlignment (self, Qt.Alignment alignment)
- setContentsMargins (self, int left, int top, int right, int bottom)
- setContentsMargins (self, QMargins margins)
- setEnabled (self, bool)
- setGeometry (self, QRect)
- setMargin (self, int)
- setMenuBar (self, QWidget w)
- setSizeConstraint (self, SizeConstraint)
- setSpacing (self, int)
- SizeConstraint sizeConstraint (self)
- int spacing (self)
- QLayoutItem takeAt (self, int index)
- int totalHeightForWidth (self, int w)
- QSize totalMaximumSize (self)
- QSize totalMinimumSize (self)
- QSize totalSizeHint (self)
- update (self)
- widgetEvent (self, QEvent)

### Static Methods

- QSize closestAcceptableSize (QWidget w, QSize s)

### Special Methods

- __len__ (self)

## Detailed Description

The QLayout class is the base class of geometry managers.

This is an abstract base class inherited by the concrete classes QBoxLayout, QGridLayout, QFormLayout, and QStackedLayout.

For users of QLayout subclasses or of QMainWindow there is seldom any need to use the basic functions provided by QLayout, such as setSizeConstraint() or setMenuBar(). See Layout Management for more information.

To make your own layout manager, implement the functions addItem(), sizeHint(), setGeometry(), itemAt() and takeAt(). You should also implement minimumSize() 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, implement hasHeightForWidth() and heightForWidth(). See the Border Layout and Flow Layout examples for more information about implementing custom layout managers.

Geometry management stops when the layout manager is deleted.

## Type Documentation

### 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.

See also setSizeConstraint().

## Method Documentation

### QLayout.__init__ (self, QWidget parent)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

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

Constructs a new child QLayout.

This layout has to be inserted into another layout before geometry management will work.

### bool QLayout.activate (self)

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.

See also update() and QWidget.updateGeometry().

### QLayout.addChildLayout (self, QLayout l)

The l argument has it's ownership transferred to Qt.

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.

See also QBoxLayout.addLayout(), QBoxLayout.insertLayout(), and QGridLayout.addLayout().

### QLayout.addChildWidget (self, QWidget w)

The w argument has it's ownership transferred to Qt.

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.

### QLayout.addItem (self, QLayoutItem)

This method is abstract and should be reimplemented in any sub-class.

The QLayoutItem argument has it's ownership transferred to Qt.

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.

See also addWidget(), QBoxLayout.addLayout(), and QGridLayout.addLayout().

### QLayout.addWidget (self, QWidget w)

The w argument has it's ownership transferred to Qt.

Adds widget w to this layout in a manner specific to the layout. This function uses addItem().

### QRect QLayout.alignmentRect (self, QRect)

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.

### QLayout.childEvent (self, QChildEvent e)

Reimplemented from QObject.childEvent().

### QSize QLayout.closestAcceptableSize (QWidget w, QSize s)

Returns a size that satisfies all size constraints on widget, including heightForWidth() and that is as close as possible to size.

### QMargins QLayout.contentsMargins (self)

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

### QRect QLayout.contentsRect (self)

Returns the layout's geometry() rectangle, but taking into account the contents margins.

This function was introduced in Qt 4.3.

See also setContentsMargins() and getContentsMargins().

### int QLayout.count (self)

This method is abstract and should be reimplemented in any sub-class.

Must be implemented in subclasses to return the number of items in the layout.

See also itemAt().

### Qt.Orientations QLayout.expandingDirections (self)

Reimplemented from 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.

See also sizeHint().

### QRect QLayout.geometry (self)

Reimplemented from QLayoutItem.geometry().

See also setGeometry().

### (int left, int top, int right, int bottom) QLayout.getContentsMargins (self)

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 also setContentsMargins(), QStyle.pixelMetric(), PM_LayoutLeftMargin, PM_LayoutTopMargin, PM_LayoutRightMargin, and PM_LayoutBottomMargin.

### int QLayout.indexOf (self, QWidget)

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

### QLayout.invalidate (self)

Reimplemented from QLayoutItem.invalidate().

### bool QLayout.isEmpty (self)

Reimplemented from QLayoutItem.isEmpty().

### bool QLayout.isEnabled (self)

Returns true if the layout is enabled; otherwise returns false.

See also setEnabled().

### QLayoutItem QLayout.itemAt (self, int index)

This method is abstract and should be reimplemented in any sub-class.

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());
     }

See also count() and takeAt().

### QLayout QLayout.layout (self)

Reimplemented from QLayoutItem.layout().

### int QLayout.margin (self)

### QSize QLayout.maximumSize (self)

Reimplemented from 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 (self)

Returns the menu bar set for this layout, or 0 if no menu bar is set.

See also setMenuBar().

### QSize QLayout.minimumSize (self)

Reimplemented from 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 (self)

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

### QLayout.removeItem (self, QLayoutItem)

The QLayoutItem argument

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

See also removeWidget() and addItem().

### QLayout.removeWidget (self, QWidget w)

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.

See also removeItem(), QWidget.setGeometry(), and addWidget().

### bool QLayout.setAlignment (self, 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 (self, QLayout l, Qt.Alignment alignment)

Sets the alignment of this item to alignment.

See also QLayoutItem.setAlignment().

### QLayout.setAlignment (self, 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.

### QLayout.setContentsMargins (self, 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.

See also contentsMargins(), getContentsMargins(), QStyle.pixelMetric(), PM_LayoutLeftMargin, PM_LayoutTopMargin, PM_LayoutRightMargin, and PM_LayoutBottomMargin.

### QLayout.setContentsMargins (self, 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.

See also contentsMargins().

### QLayout.setEnabled (self, bool)

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.

See also isEnabled().

### QLayout.setGeometry (self, QRect)

Reimplemented from QLayoutItem.setGeometry().

See also geometry().

### QLayout.setMargin (self, int)

### QLayout.setMenuBar (self, QWidget w)

The w argument has it's ownership transferred to Qt.

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.

See also menuBar().

### QLayout.setSizeConstraint (self, SizeConstraint)

### QLayout.setSpacing (self, int)

### SizeConstraint QLayout.sizeConstraint (self)

### int QLayout.spacing (self)

### QLayoutItem QLayout.takeAt (self, int index)

This method is abstract and should be reimplemented in any sub-class.

The QLayoutItem result

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;
     }

See also itemAt() and count().

### int QLayout.totalHeightForWidth (self, int w)

### QSize QLayout.totalMaximumSize (self)

### QSize QLayout.totalMinimumSize (self)

### QSize QLayout.totalSizeHint (self)

### QLayout.update (self)

Updates the layout for parentWidget().

You should generally not need to call this because it is automatically called at the most appropriate times.

See also activate() and invalidate().

### QLayout.widgetEvent (self, QEvent)

### QLayout.__len__ (self)