Qt Toolkit - QBoxLayout Class

# QBoxLayout Class Reference

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

`#include <qlayout.h>`

Inherits QLayout.

Inherited by QHBoxLayout and QVBoxLayout.

List of all member functions.

## Public Members

- QBoxLayout ( QWidget * parent, Direction, int border=0, int autoBorder = -1, const char * name=0 )
- QBoxLayout ( Direction, int autoBorder = -1, const char * name=0 )
- ~QBoxLayout ()
- void addSpacing ( int size )
- void addStretch ( int stretch = 0 )
- void addWidget ( QWidget *, int stretch = 0, int alignment = AlignCenter )
- void addLayout ( QLayout * layout, int stretch = 0 )
- Direction direction () const
- void addStrut ( int )

## Protected Members

- virtual QChain* mainVerticalChain ()
- virtual QChain* mainHorizontalChain ()
- virtual void initGM ()

## 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 mainWindget()), divides it up into a row of boxes and makes each managed widget fill one box.

If the QBoxLayout is `Horizontal,` the boxes are beside each other, with suitable sizes. Each widget (or other box) will get at least its minimum sizes and at most its maximum size, and any excess space is shared according to the stretch factors (more about that below).

If the QBoxLayout is `Vertical,` the boxes are above and below each other, again with suitable sizes.

The easiest way to create a QBoxLayout is to use one of the convenience classes QHBoxLayout (for `Horizontal` boxes) or QVBoxLayout (for `Vertical` boxes). You can also use the QBoxLayout constuctor directly, specifying its direction as `LeftToRight, Down, RightToLeft` or `Up.`

If the QBoxLayout is not the top-level layout (ie. 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 that, 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 if 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.

Finally, if the layout is a top-level one, you activate() it.

QBoxLayout also includes two margin widths: The border width and the inter-box width. The border width is the width of the reserved space along each of the QBoxLayout's four sides. The intra-widget width is the width of the automatically allocated spacing between neighbouring boxes. (You can use addSpacing() to get more space.)

The border width defaults to 0, and the intra-widget width defaults to the same as the border width. Both are set using arguments to the constructor.

You will almost always want to use the convenience classes for QBoxLayout: QVBoxLayout and QHBoxLayout, because of their simpler constructors.

Examples: pref/pref.cpp layout/layout.cpp

## Member Function Documentation

### QBoxLayout::QBoxLayout ( QWidget * parent, Direction d, int border=0, int autoBorder = -1, const char * name=0 )

Creates a new QBoxLayout with direction d and main widget parent. parent may not be 0.

border is the number of pixels between the edge of the widget and the managed children. autoBorder is the default number of pixels between neighbouring children. If autoBorder is -1 the value of border is used.

name is the internal object name

See also: direction().

Examples: layout/layout.cpp

### QBoxLayout::QBoxLayout ( Direction d, int autoBorder = -1, const char * name=0 )

If autoBorder is -1, this QBoxLayout will inherit its parent's defaultBorder(), otherwise autoBorder is used.

You have to insert this box into another layout before using it.

### QBoxLayout::~QBoxLayout ()

Destroys this box.

### void QBoxLayout::addLayout ( QLayout * layout, int stretch = 0 )

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

See also: addWidget() and addSpacing().

Examples: pref/pref.cpp layout/layout.cpp widgets/widgets.cpp

### void QBoxLayout::addSpacing ( int size )

Adds a non-stretchable space with size size. QBoxLayout gives default border and spacing. This function adds additional space.

See also: addStretch().

Examples: widgets/widgets.cpp

### void QBoxLayout::addStretch ( int stretch = 0 )

Adds a stretchable space with zero minimum size and stretch factor stretch.

See also: addSpacing().

Examples: pref/pref.cpp widgets/widgets.cpp

### void QBoxLayout::addStrut ( int size )

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.

### void QBoxLayout::addWidget ( QWidget * widget, int stretch = 0, int align = AlignCenter )

Adds widget to the box, with stretch factor stretch and alignment align.

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 factor grow more.

If the stretch factor is 0 and nothing else in the QBoxLayout can grow at all, the widget may still grow up to its maximum size.

Alignment is perpendicular to direction(), alignment in the serial direction is done with addStretch().

For horizontal boxes, the possible alignments are

- `AlignCenter` centers vertically in the box.
- `AlignTop` aligns to the top border of the box.
- `AlignBottom` aligns to the bottom border of the box.

For vertical boxes, the possible alignments are

- `AlignCenter` centers horizontally in the box.
- `AlignLeft` aligns to the left border of the box.
- `AlignRight` aligns to the right border of the box.

Alignment only has effect if the size of the box is greater than the widget's maximum size.

See also: addLayout() and addSpacing().

Examples: pref/pref.cpp layout/layout.cpp widgets/widgets.cpp

### QBoxLayout::Direction QBoxLayout::direction() const

Returns the (serial) direction of the box. addWidget(), addBox() and addSpacing() works in this direction; the stretch stretches in this direction. Alignment works perpendicular to this direction.

The directions are `LeftToRight, RightToLeft, TopToBottom` and `BottomToTop.` For the last two, the shorter aliases `Down` and `Up` are also available.

See also: addWidget(), addBox() and addSpacing().

### void QBoxLayout::initGM () `[virtual protected]`

Initializes this box.

Reimplemented from QLayout.

### QChain * QBoxLayout::mainHorizontalChain () `[virtual protected]`

Returns the main horizontal chain, so that a box can be put into other boxes (or other types of QLayout).

Reimplemented from QLayout.

### QChain * QBoxLayout::mainVerticalChain () `[virtual protected]`

Returns the main vertical chain, so that a box can be put into other boxes (or other types of QLayout).

Reimplemented from QLayout.

Search the documentation, FAQ, qt-interest archive and more (uses www.troll.no):

This file is part of the Qt toolkit, copyright © 1995-99 Troll Tech, all rights reserved.

It was generated from the following files:

- qlayout.h: 1998/08/21
- qlayout.cpp: 1998/09/21