QBoxLayout Class
The QBoxLayout class lines up child widgets horizontally or vertically. More…
| Header | #include <QBoxLayout> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
| Inherits | QLayout |
| Inherited by | QHBoxLayout, QVBoxLayout |
List of all members, including inherited members
Public Types
| enum | Direction |
Public Functions
| QBoxLayout(Direction direction, QWidget *parent = nullptr) | |
| ~QBoxLayout() | |
| Direction | direction() const |
| void | setDirection(Direction direction) |
| void | addSpacing(int size) |
| void | addStretch(int stretch = 0) |
| void | addWidget(QWidget *widget, int stretch = 0, Qt::Alignment alignment = Qt::Alignment()) |
| void | addLayout(QLayout *layout, int stretch = 0) |
| void | insertSpacing(int index, int size) |
| void | insertStretch(int index, int stretch = 0) |
| void | insertWidget(int index, QWidget *widget, int stretch = 0, Qt::Alignment alignment = Qt::Alignment()) |
| void | insertLayout(int index, QLayout *layout, int stretch = 0) |
| void | setStretch(int index, int stretch) |
| int | stretch(int index) const |
| bool | setStretchFactor(QWidget *widget, int stretch) |
Reimplemented Public Functions
| void | addItem(QLayoutItem *item) |
| void | removeItem(QLayoutItem *item) |
| QLayoutItem * | itemAt(int index) const |
| QLayoutItem * | takeAt(int index) |
| int | count() const |
| QSize | sizeHint() const |
| QSize | minimumSize() const |
| Qt::Orientations | expandingDirections() const |
| void | setGeometry(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.
| Constant | Description |
|---|---|
QBoxLayout::LeftToRight | Horizontally from the left. |
QBoxLayout::RightToLeft | Horizontally from the right. |
QBoxLayout::TopToBottom | Vertically from the top. |
QBoxLayout::BottomToTop | Vertically from the bottom. |
Member Function Documentation
QBoxLayout::QBoxLayout(Direction direction, QWidget *parent = nullptr)
Constructs a box layout with the given direction for parent.
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.
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().
int QBoxLayout::count() const
Reimplements QLayout::count().
Direction QBoxLayout::direction() const
Returns the direction of the layout.
See also setDirection().
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().
QLayoutItem * QBoxLayout::itemAt(int index) const
Reimplements QLayout::itemAt().
QSize QBoxLayout::minimumSize() const
Reimplements QLayout::minimumSize().
void QBoxLayout::removeItem(QLayoutItem *item)
Reimplements QLayout::removeItem().
void QBoxLayout::setDirection(Direction direction)
Sets the direction of the layout to direction.
See also direction().
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.
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().
QLayoutItem * QBoxLayout::takeAt(int index)
Reimplements QLayout::takeAt().