6.0.0-rc4Source

QLayout Class

The QLayout class is the base class of geometry managers. More…

Header#include <QLayout>
CMaketarget_link_libraries(app PRIVATE Nt::Widgets)
ModuleNt Widgets C++ Classes
InheritsQObject, QLayoutItem
Inherited byQBoxLayout, QFormLayout, QGridLayout, QStackedLayout

List of all members, including inherited members

Public Functions

QLayout(QWidget *parent = nullptr)
~QLayout()
intspacing() const
voidsetSpacing(int spacing)
voidsetContentsMargins(int left, int top, int right, int bottom)
voidsetContentsMargins(const QMargins &margins)
QMarginscontentsMargins() const
QRectcontentsRect() const
voidaddWidget(QWidget *widget)
virtual voidaddItem(QLayoutItem *item)
virtual voidremoveItem(QLayoutItem *item)
voidremoveWidget(QWidget *widget)
virtual QLayoutItem *itemAt(int index) const
virtual QLayoutItem *takeAt(int index)
virtual intcount() const
intindexOf(const QWidget *widget) const
QWidget *parentWidget() const
boolactivate()
voidupdate()

Reimplemented Public Functions

voidinvalidate()
QRectgeometry() const
voidsetGeometry(const QRect &rect)
boolisEmpty() const
QLayout *layout()
QSizemaximumSize() const
Qt::OrientationsexpandingDirections() const

Protected Functions

voidaddChildWidget(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

[explicit] 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.

[override] 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().

[protected] 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.

[virtual] 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.

[virtual] int QLayout::count() const

Returns the number of items in the layout.

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

Reimplements QLayoutItem::expandingDirections().

[override] 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.

[override] 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.

[override] bool QLayout::isEmpty() const

Reimplements QLayoutItem::isEmpty().

Returns true if the layout has no items that take space.

[virtual] QLayoutItem * QLayout::itemAt(int index) const

Returns the item at index, or nullptr if there is none.

[override] QLayout * QLayout::layout()

Reimplements QLayoutItem::layout().

[override] 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.

[virtual] 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.

[override] 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().

[virtual] 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().