QLayout Class
The QLayout class is the base class of geometry managers. More…
| Header | #include <QLayout> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
| Inherits | QObject, QLayoutItem |
| Inherited by | QBoxLayout, QFormLayout, QGridLayout, QStackedLayout |
List of all members, including inherited members
Public Functions
| QLayout(QWidget *parent = nullptr) | |
| ~QLayout() | |
| int | spacing() const |
| void | setSpacing(int spacing) |
| void | setContentsMargins(int left, int top, int right, int bottom) |
| void | setContentsMargins(const QMargins &margins) |
| QMargins | contentsMargins() const |
| QRect | contentsRect() const |
| void | addWidget(QWidget *widget) |
| virtual void | addItem(QLayoutItem *item) |
| virtual void | removeItem(QLayoutItem *item) |
| void | removeWidget(QWidget *widget) |
| virtual QLayoutItem * | itemAt(int index) const |
| virtual QLayoutItem * | takeAt(int index) |
| virtual int | count() const |
| int | indexOf(const QWidget *widget) const |
| QWidget * | parentWidget() const |
| bool | activate() |
| void | update() |
Reimplemented Public Functions
| void | invalidate() |
| QRect | geometry() const |
| void | setGeometry(const QRect &rect) |
| bool | isEmpty() const |
| QLayout * | layout() |
| QSize | maximumSize() const |
| Qt::Orientations | expandingDirections() const |
Protected Functions
| void | addChildWidget(QWidget *widget) |
Detailed Description
A layout places the child widgets of a widget, and resizes them when the widget is resized or when the size hint of a child changes. Create a layout on a widget, or give it to the widget with QWidget::setLayout(), then add widgets:
QHBoxLayout *layout = new QHBoxLayout(window);
layout->addWidget(new QLabel("Name:"));
layout->addWidget(new QPushButton("OK"));
Adding a widget makes it a child of the widget that the layout manages. Use QBoxLayout and its subclasses QHBoxLayout and QVBoxLayout, or derive your own layout from QLayout.
The layout is done again from the event loop when invalidate() is called, which the layout does by itself when a child widget calls QWidget::updateGeometry().
See also QBoxLayout and QWidget.
Differences from Qt
There is no setSizeConstraint(), setMenuBar(), height-for-width, setAlignment() for widgets, addChildLayout(), widgetEvent() or layoutItems. QGridLayout, QFormLayout and QStackedLayout are not implemented.
Member Function Documentation
QLayout::QLayout(QWidget *parent = nullptr)
Constructs a layout for parent, which has no layout yet. If parent is nullptr, add the layout to a widget later with QWidget::setLayout() or to another layout.
QLayout::~QLayout()
Destroys the layout. The widgets in it are not destroyed.
bool QLayout::activate()
Does the layout now: places the items in the geometry of the widget that the layout manages. A window grows if it is smaller than the minimum size of its layout. Returns false if there is no widget.
See also invalidate().
void QLayout::addChildWidget(QWidget *widget)
Makes widget a child of the widget that the layout manages, and shows it if that widget is visible and widget was not hidden. Layouts call it for the widgets that are added.
void QLayout::addItem(QLayoutItem *item)
Adds item to the layout, which takes ownership of it.
void QLayout::addWidget(QWidget *widget)
Adds widget to the layout, making it a child of the widget that the layout manages. A box layout has its own addWidget() with more arguments.
QMargins QLayout::contentsMargins() const
Returns the margins. Unless they were set, they are zero for a layout in another layout and what the style prescribes for the layout of a widget.
See also setContentsMargins().
QRect QLayout::contentsRect() const
Returns the rectangle that the layout has for its items: its geometry minus the margins.
int QLayout::count() const
Returns the number of items in the layout.
Qt::Orientations QLayout::expandingDirections() const
Reimplements QLayoutItem::expandingDirections().
QRect QLayout::geometry() const
Reimplements QLayoutItem::geometry().
int QLayout::indexOf(const QWidget *widget) const
Returns the index of the item that holds widget, or -1 if the widget is not directly in this layout.
void QLayout::invalidate()
Reimplements QLayoutItem::invalidate().
Tells the layout that its size hint changed, so that it does its work again from the event loop.
bool QLayout::isEmpty() const
Reimplements QLayoutItem::isEmpty().
Returns true if the layout has no items that take space.
QLayoutItem * QLayout::itemAt(int index) const
Returns the item at index, or nullptr if there is none.
QLayout * QLayout::layout()
Reimplements QLayoutItem::layout().
QSize QLayout::maximumSize() const
Reimplements QLayoutItem::maximumSize().
QWidget * QLayout::parentWidget() const
Returns the widget that the layout manages, also for a layout in another layout, or nullptr if the layout has no widget yet.
void QLayout::removeItem(QLayoutItem *item)
Removes item from the layout. The item is not deleted.
void QLayout::removeWidget(QWidget *widget)
Removes widget from the layout, also when it is in a layout inside this one. The widget stays a child of its parent. Widgets are removed from their layout when they are deleted.
void QLayout::setContentsMargins(int left, int top, int right, int bottom)
Sets the space between the edge of the widget and the items to left, top, right and bottom pixels.
See also contentsMargins().
void QLayout::setContentsMargins(const QMargins &margins)
This is an overloaded function.
Sets the margins to margins.
void QLayout::setGeometry(const QRect &rect)
Reimplements QLayoutItem::setGeometry().
Stores rect as the geometry of the layout. Subclasses place their items in it.
void QLayout::setSpacing(int spacing)
Sets the spacing between the items to spacing pixels.
See also spacing().
int QLayout::spacing() const
Returns the spacing between the items, in pixels. Unless it was set, it is that of the parent layout or the one that the style prescribes.
See also setSpacing().
QLayoutItem * QLayout::takeAt(int index)
Removes the item at index from the layout and returns it. The caller owns the returned item.
void QLayout::update()
Does the same as invalidate().