6.0.0-rc4Source

QBoxLayout Class

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

Header#include <QBoxLayout>
CMaketarget_link_libraries(app PRIVATE Nt::Widgets)
ModuleNt Widgets C++ Classes
InheritsQLayout
Inherited byQHBoxLayout, QVBoxLayout

List of all members, including inherited members

Public Types

enumDirection

Public Functions

QBoxLayout(Direction direction, QWidget *parent = nullptr)
~QBoxLayout()
Directiondirection() const
voidsetDirection(Direction direction)
voidaddSpacing(int size)
voidaddStretch(int stretch = 0)
voidaddWidget(QWidget *widget, int stretch = 0, Qt::Alignment alignment = Qt::Alignment())
voidaddLayout(QLayout *layout, int stretch = 0)
voidinsertSpacing(int index, int size)
voidinsertStretch(int index, int stretch = 0)
voidinsertWidget(int index, QWidget *widget, int stretch = 0, Qt::Alignment alignment = Qt::Alignment())
voidinsertLayout(int index, QLayout *layout, int stretch = 0)
voidsetStretch(int index, int stretch)
intstretch(int index) const
boolsetStretchFactor(QWidget *widget, int stretch)

Reimplemented Public Functions

voidaddItem(QLayoutItem *item)
voidremoveItem(QLayoutItem *item)
QLayoutItem *itemAt(int index) const
QLayoutItem *takeAt(int index)
intcount() const
QSizesizeHint() const
QSizeminimumSize() const
Qt::OrientationsexpandingDirections() const
voidsetGeometry(const QRect &rect)

Detailed Description

A box layout puts its items in a row or a column, as its direction says. Each item gets its size hint along the box, and the space that is left over (or missing) is shared out as in Qt. If the box is bigger than the hints, the items with a stretch factor share the space in proportion to the factors; without any, the items that want to expand (their size policy has the Expanding flag, as a spacer made by addStretch() does) share it equally, and if there are none, all items that can grow do. An item is never larger than its maximum size or smaller than its minimum. If the box is too small, every item gives up the same amount of its hint, down to its minimum size. Spacers do not have spacing next to them. Across the box, items fill the available space within their limits, and are centered where they cannot fill it.

QVBoxLayout *layout = new QVBoxLayout(window);
layout->addWidget(new QLabel("Title"));
layout->addWidget(list, 1); // takes the remaining space
QHBoxLayout *row = new QHBoxLayout;
row->addStretch();
row->addWidget(ok);
layout->addLayout(row);

See also QHBoxLayout, QVBoxLayout and QLayout.

Differences from Qt

There is no height-for-width, addStrut(), setAlignment() on the layout or insertItem(). The alignment of an item applies across the box only: along the box an item always gets the space that the algorithm gives it.

Member Type Documentation

enum QBoxLayout::Direction

This enum gives the direction in which a box layout places its items.

ConstantDescription
QBoxLayout::LeftToRightHorizontally from the left.
QBoxLayout::RightToLeftHorizontally from the right.
QBoxLayout::TopToBottomVertically from the top.
QBoxLayout::BottomToTopVertically from the bottom.

Member Function Documentation

[explicit] QBoxLayout::QBoxLayout(Direction direction, QWidget *parent = nullptr)

Constructs a box layout with the given direction for parent.

[override] QBoxLayout::~QBoxLayout()

Destroys the layout and the items in it, but not the widgets. A layout that is in another layout is removed from it.

[override] void QBoxLayout::addItem(QLayoutItem *item)

Reimplements QLayout::addItem().

Adds item at the end of the layout, which takes ownership of it.

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

Adds layout at the end of the layout with the stretch factor stretch. It becomes a child of this layout.

See also insertLayout().

void QBoxLayout::addSpacing(int size)

Adds a fixed space of size pixels at the end of the layout, besides the spacing.

See also addStretch().

void QBoxLayout::addStretch(int stretch = 0)

Adds stretchable space with the stretch factor stretch at the end of the layout.

See also addSpacing().

void QBoxLayout::addWidget(QWidget *widget, int stretch = 0, Qt::Alignment alignment = Qt::Alignment())

Adds widget at the end of the layout with the stretch factor stretch and the alignment. Across the box, a widget with an alignment gets its size hint instead of all the space, and is placed as the alignment says.

See also insertWidget() and addLayout().

[override] int QBoxLayout::count() const

Reimplements QLayout::count().

Direction QBoxLayout::direction() const

Returns the direction of the layout.

See also setDirection().

[override] Qt::Orientations QBoxLayout::expandingDirections() const

Reimplements QLayout::expandingDirections().

void QBoxLayout::insertLayout(int index, QLayout *layout, int stretch = 0)

Inserts layout at index with the stretch factor stretch. It becomes a child of this layout.

void QBoxLayout::insertSpacing(int index, int size)

Inserts a fixed space of size pixels at index. An index of -1 means the end.

void QBoxLayout::insertStretch(int index, int stretch = 0)

Inserts stretchable space with the stretch factor stretch at index.

void QBoxLayout::insertWidget(int index, QWidget *widget, int stretch = 0, Qt::Alignment alignment = Qt::Alignment())

Inserts widget at index, with the stretch factor stretch and the alignment; see addWidget().

[override] QLayoutItem * QBoxLayout::itemAt(int index) const

Reimplements QLayout::itemAt().

[override] QSize QBoxLayout::minimumSize() const

Reimplements QLayout::minimumSize().

[override] void QBoxLayout::removeItem(QLayoutItem *item)

Reimplements QLayout::removeItem().

void QBoxLayout::setDirection(Direction direction)

Sets the direction of the layout to direction.

See also direction().

[override] void QBoxLayout::setGeometry(const QRect &rect)

Reimplements QLayout::setGeometry().

void QBoxLayout::setStretch(int index, int stretch)

Sets the stretch factor of the item at index to stretch.

See also stretch().

bool QBoxLayout::setStretchFactor(QWidget *widget, int stretch)

Sets the stretch factor of widget to stretch and returns true if the widget is in this layout.

[override] QSize QBoxLayout::sizeHint() const

Reimplements QLayout::sizeHint().

int QBoxLayout::stretch(int index) const

Returns the stretch factor of the item at index, or 0.

See also setStretch().

[override] QLayoutItem * QBoxLayout::takeAt(int index)

Reimplements QLayout::takeAt().