QBoxLayout Class
The QBoxLayout class lines up child widgets horizontally or vertically. More...
| Header: | #include <QBoxLayout> |
| qmake: | QT += widgets |
| Inherits: | QLayout |
| Inherited By: |
Public Types
| enum | Direction { LeftToRight, RightToLeft, TopToBottom, BottomToTop } |
Detailed Description
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.
Member Type Documentation
enum 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. |