|  | Home · All Classes · Modules

# QBoxLayout Class Reference
[QtGui module]

The QBoxLayout class lines up child widgets horizontally or vertically. More...

Inherits QLayout.

Inherited by QHBoxLayout and QVBoxLayout.

### Types

- enum Direction { LeftToRight, RightToLeft, TopToBottom, BottomToTop, Down, Up }

### Methods

- __init__ (self, Direction, QWidget parent = None)
- addItem (self, QLayoutItem)
- addLayout (self, QLayout, int stretch = 0)
- addSpacerItem (self, QSpacerItem)
- addSpacing (self, int)
- addStretch (self, int stretch = 0)
- addStrut (self, int)
- addWidget (self, QWidget, int stretch = 0, Qt.Alignment alignment = 0)
- int count (self)
- Direction direction (self)
- Qt.Orientations expandingDirections (self)
- bool hasHeightForWidth (self)
- int heightForWidth (self, int)
- insertItem (self, int, QLayoutItem)
- insertLayout (self, int, QLayout, int stretch = 0)
- insertSpacerItem (self, int, QSpacerItem)
- insertSpacing (self, int, int)
- insertStretch (self, int, int stretch = 0)
- insertWidget (self, int, QWidget, int stretch = 0, Qt.Alignment alignment = 0)
- invalidate (self)
- QLayoutItem itemAt (self, int)
- QSize maximumSize (self)
- int minimumHeightForWidth (self, int)
- QSize minimumSize (self)
- setDirection (self, Direction)
- setGeometry (self, QRect)
- setSpacing (self, int)
- setStretch (self, int, int)
- bool setStretchFactor (self, QWidget, int)
- bool setStretchFactor (self, QLayout, int)
- QSize sizeHint (self)
- int spacing (self)
- int stretch (self, int)
- QLayoutItem takeAt (self, int)

## Detailed Description

The QBoxLayout class lines up child widgets horizontally or vertically.

QBoxLayout takes the space it gets (from its parent layout or from the parentWidget()), divides it up into a row of boxes, and makes each managed widget fill one box.

If the QBoxLayout's orientation is Qt.Horizontal the boxes are placed in a row, with suitable sizes. Each widget (or other box) will get at least its minimum size and at most its maximum size. Any excess space is shared according to the stretch factors (more about that below).

If the QBoxLayout's orientation is Qt.Vertical, the boxes are placed in a column, again with suitable sizes.

The easiest way to create a QBoxLayout is to use one of the convenience classes, e.g. QHBoxLayout (for Qt.Horizontal boxes) or QVBoxLayout (for Qt.Vertical boxes). You can also use the QBoxLayout constructor directly, specifying its direction as LeftToRight, RightToLeft, TopToBottom, or BottomToTop.

If the QBoxLayout is not the top-level layout (i.e. it is not managing all of the widget's area and children), you must add it to its parent layout before you can do anything with it. The normal way to add a layout is by calling parentLayout->addLayout().

Once you have done this, you can add boxes to the QBoxLayout using one of four functions:

- addWidget() to add a widget to the QBoxLayout and set the widget's stretch factor. (The stretch factor is along the row of boxes.)
- addSpacing() to create an empty box; this is one of the functions you use to create nice and spacious dialogs. See below for ways to set margins.
- addStretch() to create an empty, stretchable box.
- addLayout() to add a box containing another QLayout to the row and set that layout's stretch factor.

Use insertWidget(), insertSpacing(), insertStretch() or insertLayout() to insert a box at a specified position in the layout.

QBoxLayout also includes two margin widths:

- setContentsMargins() sets the width of the outer border on each side of the widget. This is the width of the reserved space along each of the QBoxLayout's four sides.
- setSpacing() sets the width between neighboring boxes. (You can use addSpacing() to get more space at a particular spot.)

The margin default is provided by the style. The default margin most Qt styles specify is 9 for child widgets and 11 for windows. The spacing defaults to the same as the margin width for a top-level layout, or to the same as the parent layout.

To remove a widget from a layout, call removeWidget(). Calling QWidget.hide() on a widget also effectively removes the widget from the layout until QWidget.show() is called.

You will almost always want to use QVBoxLayout and QHBoxLayout rather than QBoxLayout because of their convenient constructors.

See also QGridLayout, QStackedLayout, and Layout Management.

## Type Documentation

### QBoxLayout.Direction

This type is used to determine the direction of a box layout.

Constant | Value | Description
--- | --- | ---
`QBoxLayout.LeftToRight` | `0` | Horizontal from left to right.
`QBoxLayout.RightToLeft` | `1` | Horizontal from right to left.
`QBoxLayout.TopToBottom` | `2` | Vertical from top to bottom.
`QBoxLayout.BottomToTop` | `3` | Vertical from bottom to top.

## Method Documentation

### QBoxLayout.__init__ (self, Direction, QWidget parent = None)

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

Constructs a new QBoxLayout with direction dir and parent widget parent.

See also direction().

### QBoxLayout.addItem (self, QLayoutItem)

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

Reimplemented from QLayout.addItem().

### QBoxLayout.addLayout (self, QLayout, int stretch = 0)

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

Adds layout to the end of the box, with serial stretch factor stretch.

See also insertLayout(), addItem(), and addWidget().

### QBoxLayout.addSpacerItem (self, QSpacerItem)

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

Adds spacerItem to the end of this box layout.

This function was introduced in Qt 4.4.

See also addSpacing() and addStretch().

### QBoxLayout.addSpacing (self, int)

Adds a non-stretchable space (a QSpacerItem) with size size to the end of this box layout. QBoxLayout provides default margin and spacing. This function adds additional space.

See also insertSpacing(), addItem(), and QSpacerItem.

### QBoxLayout.addStretch (self, int stretch = 0)

Adds a stretchable space (a QSpacerItem) with zero minimum size and stretch factor stretch to the end of this box layout.

See also insertStretch(), addItem(), and QSpacerItem.

### QBoxLayout.addStrut (self, int)

Limits the perpendicular dimension of the box (e.g. height if the box is LeftToRight) to a minimum of size. Other constraints may increase the limit.

See also addItem().

### QBoxLayout.addWidget (self, QWidget, int stretch = 0, Qt.Alignment alignment = 0)

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

Adds widget to the end of this box layout, with a stretch factor of stretch and alignment alignment.

The stretch factor applies only in the direction of the QBoxLayout, and is relative to the other boxes and widgets in this QBoxLayout. Widgets and boxes with higher stretch factors grow more.

If the stretch factor is 0 and nothing else in the QBoxLayout has a stretch factor greater than zero, the space is distributed according to the QWidget:sizePolicy() of each widget that's involved.

The alignment is specified by alignment. The default alignment is 0, which means that the widget fills the entire cell.

See also insertWidget(), addItem(), addLayout(), addStretch(), addSpacing(), and addStrut().

### int QBoxLayout.count (self)

Reimplemented from QLayout.count().

### Direction QBoxLayout.direction (self)

Returns the direction of the box. addWidget() and addSpacing() work in this direction; the stretch stretches in this direction.

See also setDirection(), QBoxLayout.Direction, addWidget(), and addSpacing().

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

Reimplemented from QLayoutItem.expandingDirections().

### bool QBoxLayout.hasHeightForWidth (self)

Reimplemented from QLayoutItem.hasHeightForWidth().

### int QBoxLayout.heightForWidth (self, int)

Reimplemented from QLayoutItem.heightForWidth().

### QBoxLayout.insertItem (self, int, QLayoutItem)

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

Inserts item into this box layout at position index. If index is negative, the item is added at the end.

See also addItem(), insertWidget(), insertLayout(), insertStretch(), and insertSpacing().

### QBoxLayout.insertLayout (self, int, QLayout, int stretch = 0)

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

Inserts layout at position index, with stretch factor stretch. If index is negative, the layout is added at the end.

layout becomes a child of the box layout.

See also addLayout() and insertItem().

### QBoxLayout.insertSpacerItem (self, int, QSpacerItem)

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

Inserts spacerItem at position index, with zero minimum size and stretch factor. If index is negative the space is added at the end.

This function was introduced in Qt 4.4.

See also addSpacerItem(), insertStretch(), and insertSpacing().

### QBoxLayout.insertSpacing (self, int, int)

Inserts a non-stretchable space (a QSpacerItem) at position index, with size size. If index is negative the space is added at the end.

The box layout has default margin and spacing. This function adds additional space.

See also addSpacing(), insertItem(), and QSpacerItem.

### QBoxLayout.insertStretch (self, int, int stretch = 0)

Inserts a stretchable space (a QSpacerItem) at position index, with zero minimum size and stretch factor stretch. If index is negative the space is added at the end.

See also addStretch(), insertItem(), and QSpacerItem.

### QBoxLayout.insertWidget (self, int, QWidget, int stretch = 0, Qt.Alignment alignment = 0)

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

Inserts widget at position index, with stretch factor stretch and alignment alignment. If index is negative, the widget is added at the end.

The stretch factor applies only in the direction of the QBoxLayout, and is relative to the other boxes and widgets in this QBoxLayout. Widgets and boxes with higher stretch factors grow more.

If the stretch factor is 0 and nothing else in the QBoxLayout has a stretch factor greater than zero, the space is distributed according to the QWidget:sizePolicy() of each widget that's involved.

The alignment is specified by alignment. The default alignment is 0, which means that the widget fills the entire cell.

See also addWidget() and insertItem().

### QBoxLayout.invalidate (self)

Reimplemented from QLayoutItem.invalidate().

Resets cached information.

### QLayoutItem QBoxLayout.itemAt (self, int)

Reimplemented from QLayout.itemAt().

### QSize QBoxLayout.maximumSize (self)

Reimplemented from QLayoutItem.maximumSize().

### int QBoxLayout.minimumHeightForWidth (self, int)

Reimplemented from QLayoutItem.minimumHeightForWidth().

### QSize QBoxLayout.minimumSize (self)

Reimplemented from QLayoutItem.minimumSize().

### QBoxLayout.setDirection (self, Direction)

Sets the direction of this layout to direction.

See also direction().

### QBoxLayout.setGeometry (self, QRect)

Reimplemented from QLayoutItem.setGeometry().

### QBoxLayout.setSpacing (self, int)

Reimplements QLayout.setSpacing(). Sets the spacing property to spacing.

See also QLayout.setSpacing() and spacing().

### QBoxLayout.setStretch (self, int, int)

Sets the stretch factor at position index. to stretch.

This function was introduced in Qt 4.5.

See also stretch().

### bool QBoxLayout.setStretchFactor (self, QWidget, int)

Sets the stretch factor for widget to stretch and returns true if widget is found in this layout (not including child layouts); otherwise returns false.

See also setAlignment().

### bool QBoxLayout.setStretchFactor (self, QLayout, int)

This is an overloaded function.

Sets the stretch factor for the layout layout to stretch and returns true if layout is found in this layout (not including child layouts); otherwise returns false.

### QSize QBoxLayout.sizeHint (self)

Reimplemented from QLayoutItem.sizeHint().

### int QBoxLayout.spacing (self)

Reimplements QLayout.spacing(). If the spacing property is valid, that value is returned. Otherwise, a value for the spacing property is computed and returned. Since layout spacing in a widget is style dependent, if the parent is a widget, it queries the style for the (horizontal or vertical) spacing of the layout. Otherwise, the parent is a layout, and it queries the parent layout for the spacing().

See also QLayout.spacing() and setSpacing().

### int QBoxLayout.stretch (self, int)

Returns the stretch factor at position index.

This function was introduced in Qt 4.5.

See also setStretch().

### QLayoutItem QBoxLayout.takeAt (self, int)

The QLayoutItem result

Reimplemented from QLayout.takeAt().