6.0.0-rc4Source

QFormLayout Class

The QFormLayout class lays out a form of labels and fields. More…

Header#include <QFormLayout>
CMaketarget_link_libraries(app PRIVATE Nt::Widgets)
ModuleNt Widgets C++ Classes
InheritsQLayout

List of all members, including inherited members

Public Types

Public Functions

QFormLayout(QWidget *parent = nullptr)
~QFormLayout()
voidaddRow(QWidget *label, QWidget *field)
voidaddRow(QWidget *label, QLayout *field)
voidaddRow(const QString &labelText, QWidget *field)
voidaddRow(const QString &labelText, QLayout *field)
voidaddRow(QWidget *widget)
voidaddRow(QLayout *layout)
voidinsertRow(int row, QWidget *label, QWidget *field)
voidinsertRow(int row, QWidget *label, QLayout *field)
voidinsertRow(int row, const QString &labelText, QWidget *field)
voidinsertRow(int row, const QString &labelText, QLayout *field)
voidinsertRow(int row, QWidget *widget)
voidinsertRow(int row, QLayout *layout)
voidremoveRow(int row)
introwCount() const
voidsetWidget(int row, ItemRole role, QWidget *widget)
voidsetLayout(int row, ItemRole role, QLayout *layout)
QLayoutItem *itemAt(int row, ItemRole role) const
voidgetWidgetPosition(QWidget *widget, int *row, ItemRole *role) const
voidgetLayoutPosition(QLayout *layout, int *row, ItemRole *role) const
QWidget *labelForField(QWidget *field) const
QWidget *labelForField(QLayout *field) const
FieldGrowthPolicyfieldGrowthPolicy() const
voidsetFieldGrowthPolicy(FieldGrowthPolicy policy)
RowWrapPolicyrowWrapPolicy() const
voidsetRowWrapPolicy(RowWrapPolicy policy)
Qt::AlignmentlabelAlignment() const
voidsetLabelAlignment(Qt::Alignment alignment)
Qt::AlignmentformAlignment() const
voidsetFormAlignment(Qt::Alignment alignment)
inthorizontalSpacing() const
voidsetHorizontalSpacing(int spacing)
intverticalSpacing() const
voidsetVerticalSpacing(int spacing)

Reimplemented Public Functions

voidaddItem(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 form has rows, and a row has a label and the field that it names: a line edit, a spin box, a combo box or any other widget or layout. The labels are in a column on the left, and the fields in a column to the right of them, so the fields line up. A row with only one widget, such as a button or a group box, takes the width of the form.

QFormLayout *form = new QFormLayout(window);
form->addRow("Name:", new QLineEdit);
form->addRow("Age:", new QSpinBox);
form->addRow(new QLabel("Notes:"), notes);
form->addRow(okButton);

In a narrow window a form can put each label above its field: see setRowWrapPolicy(). The fields grow to fill the form by default, as in the Fusion style of Qt, and setFieldGrowthPolicy() changes that.

A form with labels in the left column, fields that fill the right column, a field that is taller than its label, and a button on a row of its own.

QFormLayout in the light color scheme

See also QGridLayout, QBoxLayout and QLayout.

Differences from Qt

A new label cannot be given a buddy. There is no takeRow(), no removeRow() for a widget or a layout, no setSpacing() that is different from QLayout::setSpacing(), and no height for width (hasHeightForWidth()), so a word-wrapped label needs a fixed height. The form alignment only places the form when it is smaller than the space it has. The defaults are those of the Fusion style: the labels are on the left, and the fields grow.

Member Type Documentation

enum QFormLayout::FieldGrowthPolicy

This enum says how the fields of a form take the room that is left.

ConstantDescription
QFormLayout::FieldsStayAtSizeHintThe fields keep their size hint.
QFormLayout::ExpandingFieldsGrowThe fields whose size policy is expanding grow, and the others keep their size hint.
QFormLayout::AllNonFixedFieldsGrowEvery field that is not fixed in width grows to fill the column.

enum QFormLayout::ItemRole

This enum says what an item is in its row.

ConstantDescription
QFormLayout::LabelRoleThe label of the row.
QFormLayout::FieldRoleThe field of the row.
QFormLayout::SpanningRoleThe only item of a row, which takes the width of the form.

enum QFormLayout::RowWrapPolicy

This enum says when a label is put above its field instead of before it.

ConstantDescription
QFormLayout::DontWrapRowsLabels are always before their field.
QFormLayout::WrapLongRowsA label goes above its field when the form is too narrow for both side by side.
QFormLayout::WrapAllRowsLabels are always above their field.

Member Function Documentation

[explicit] QFormLayout::QFormLayout(QWidget *parent = nullptr)

Constructs a form layout for parent, with no rows.

[override] QFormLayout::~QFormLayout()

Destroys the layout. The widgets that are in it stay.

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

Reimplements QLayout::addItem().

Adds item as a row of its own, as wide as the form.

void QFormLayout::addRow(QWidget *label, QWidget *field)

Adds a row with label before field. The form takes over both widgets.

See also insertRow().

void QFormLayout::addRow(QWidget *label, QLayout *field)

This is an overloaded function.

Adds a row with label before the layout field.

void QFormLayout::addRow(const QString &labelText, QWidget *field)

This is an overloaded function.

Adds a row with a new label that has labelText before field.

void QFormLayout::addRow(const QString &labelText, QLayout *field)

This is an overloaded function.

Adds a row with a new label that has labelText before the layout field.

void QFormLayout::addRow(QWidget *widget)

This is an overloaded function.

Adds a row with only widget, as wide as the form.

void QFormLayout::addRow(QLayout *layout)

This is an overloaded function.

Adds a row with only layout, as wide as the form.

[override] int QFormLayout::count() const

Reimplements QLayout::count().

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

Reimplements QLayout::expandingDirections().

FieldGrowthPolicy QFormLayout::fieldGrowthPolicy() const

Returns how the fields take the room that is left. The default is AllNonFixedFieldsGrow, as in the Fusion style.

See also setFieldGrowthPolicy().

Qt::Alignment QFormLayout::formAlignment() const

Returns where the form is put when it is smaller than the space it has. The default is Qt::AlignLeft | Qt::AlignTop.

See also setFormAlignment().

void QFormLayout::getLayoutPosition(QLayout *layout, int *row, ItemRole *role) const

Finds the row and the role of layout and stores them in row and role. They are -1 if the layout is not in the form.

See also getWidgetPosition().

void QFormLayout::getWidgetPosition(QWidget *widget, int *row, ItemRole *role) const

Finds the row and the role of widget and stores them in row and role. They are -1 if the widget is not in the form.

See also getLayoutPosition().

int QFormLayout::horizontalSpacing() const

Returns the space between the label column and the field column: the one that setHorizontalSpacing() set, or else spacing().

See also setHorizontalSpacing().

void QFormLayout::insertRow(int row, QWidget *label, QWidget *field)

Inserts a row at row, with label before field; rows from row on move down. row is brought into the range 0 to rowCount().

See also addRow() and removeRow().

void QFormLayout::insertRow(int row, QWidget *label, QLayout *field)

This is an overloaded function.

Inserts a row at row, with label before the layout field.

void QFormLayout::insertRow(int row, const QString &labelText, QWidget *field)

This is an overloaded function.

Inserts a row at row, with a new label that has labelText before field.

void QFormLayout::insertRow(int row, const QString &labelText, QLayout *field)

This is an overloaded function.

Inserts a row at row, with a new label that has labelText before the layout field.

void QFormLayout::insertRow(int row, QWidget *widget)

This is an overloaded function.

Inserts a row at row with only widget, as wide as the form.

void QFormLayout::insertRow(int row, QLayout *layout)

This is an overloaded function.

Inserts a row at row with only layout, as wide as the form.

QLayoutItem * QFormLayout::itemAt(int row, ItemRole role) const

Returns the item in the row row with the given role, or nullptr if there is none.

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

Reimplements QLayout::itemAt().

Qt::Alignment QFormLayout::labelAlignment() const

Returns how the labels are placed in their column. The default is Qt::AlignLeft.

See also setLabelAlignment().

QWidget * QFormLayout::labelForField(QWidget *field) const

Returns the label of the row that has field, or nullptr if there is none.

QWidget * QFormLayout::labelForField(QLayout *field) const

This is an overloaded function.

Returns the label of the row that has the layout field, or nullptr if there is none.

[override] QSize QFormLayout::minimumSize() const

Reimplements QLayout::minimumSize().

void QFormLayout::removeRow(int row)

Removes the row row, and deletes the widgets and layouts in it. Does nothing if there is no such row.

See also insertRow().

int QFormLayout::rowCount() const

Returns the number of rows.

RowWrapPolicy QFormLayout::rowWrapPolicy() const

Returns when a label is put above its field. The default is DontWrapRows.

See also setRowWrapPolicy().

void QFormLayout::setFieldGrowthPolicy(FieldGrowthPolicy policy)

Sets how the fields take the room that is left to policy.

See also fieldGrowthPolicy().

void QFormLayout::setFormAlignment(Qt::Alignment alignment)

Sets where the form is put when it does not fill the space it has to alignment.

See also formAlignment().

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

Reimplements QLayout::setGeometry().

void QFormLayout::setHorizontalSpacing(int spacing)

Sets the space between the label column and the field column to spacing.

See also horizontalSpacing().

void QFormLayout::setLabelAlignment(Qt::Alignment alignment)

Sets how the labels are placed in their column to alignment, one of Qt::AlignLeft, Qt::AlignRight and Qt::AlignHCenter. A label that is above its field is always at the left.

See also labelAlignment().

void QFormLayout::setLayout(int row, ItemRole role, QLayout *layout)

Puts layout in the row row with the given role; see setWidget(). The layout becomes a child of this one.

See also setWidget().

void QFormLayout::setRowWrapPolicy(RowWrapPolicy policy)

Sets when a label is put above its field to policy.

See also rowWrapPolicy().

void QFormLayout::setVerticalSpacing(int spacing)

Sets the space between the rows to spacing.

See also verticalSpacing().

void QFormLayout::setWidget(int row, ItemRole role, QWidget *widget)

Puts widget in the row row with the given role. The row may be rowCount(), which adds a row. Nothing happens if the row is out of range or the role is taken in it.

See also setLayout() and itemAt().

[override] QSize QFormLayout::sizeHint() const

Reimplements QLayout::sizeHint().

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

Reimplements QLayout::takeAt().

int QFormLayout::verticalSpacing() const

Returns the space between the rows, and between a label and its field above it: the one that setVerticalSpacing() set, or else spacing().

See also setVerticalSpacing().