QFormLayout Class
The QFormLayout class lays out a form of labels and fields. More…
| Header | #include <QFormLayout> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
| Inherits | QLayout |
List of all members, including inherited members
Public Types
| enum | FieldGrowthPolicy |
| enum | RowWrapPolicy |
| enum | ItemRole |
Public Functions
| QFormLayout(QWidget *parent = nullptr) | |
| ~QFormLayout() | |
| void | addRow(QWidget *label, QWidget *field) |
| void | addRow(QWidget *label, QLayout *field) |
| void | addRow(const QString &labelText, QWidget *field) |
| void | addRow(const QString &labelText, QLayout *field) |
| void | addRow(QWidget *widget) |
| void | addRow(QLayout *layout) |
| void | insertRow(int row, QWidget *label, QWidget *field) |
| void | insertRow(int row, QWidget *label, QLayout *field) |
| void | insertRow(int row, const QString &labelText, QWidget *field) |
| void | insertRow(int row, const QString &labelText, QLayout *field) |
| void | insertRow(int row, QWidget *widget) |
| void | insertRow(int row, QLayout *layout) |
| void | removeRow(int row) |
| int | rowCount() const |
| void | setWidget(int row, ItemRole role, QWidget *widget) |
| void | setLayout(int row, ItemRole role, QLayout *layout) |
| QLayoutItem * | itemAt(int row, ItemRole role) const |
| void | getWidgetPosition(QWidget *widget, int *row, ItemRole *role) const |
| void | getLayoutPosition(QLayout *layout, int *row, ItemRole *role) const |
| QWidget * | labelForField(QWidget *field) const |
| QWidget * | labelForField(QLayout *field) const |
| FieldGrowthPolicy | fieldGrowthPolicy() const |
| void | setFieldGrowthPolicy(FieldGrowthPolicy policy) |
| RowWrapPolicy | rowWrapPolicy() const |
| void | setRowWrapPolicy(RowWrapPolicy policy) |
| Qt::Alignment | labelAlignment() const |
| void | setLabelAlignment(Qt::Alignment alignment) |
| Qt::Alignment | formAlignment() const |
| void | setFormAlignment(Qt::Alignment alignment) |
| int | horizontalSpacing() const |
| void | setHorizontalSpacing(int spacing) |
| int | verticalSpacing() const |
| void | setVerticalSpacing(int spacing) |
Reimplemented Public Functions
| void | addItem(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 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.

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.
| Constant | Description |
|---|---|
QFormLayout::FieldsStayAtSizeHint | The fields keep their size hint. |
QFormLayout::ExpandingFieldsGrow | The fields whose size policy is expanding grow, and the others keep their size hint. |
QFormLayout::AllNonFixedFieldsGrow | Every 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.
| Constant | Description |
|---|---|
QFormLayout::LabelRole | The label of the row. |
QFormLayout::FieldRole | The field of the row. |
QFormLayout::SpanningRole | The 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.
| Constant | Description |
|---|---|
QFormLayout::DontWrapRows | Labels are always before their field. |
QFormLayout::WrapLongRows | A label goes above its field when the form is too narrow for both side by side. |
QFormLayout::WrapAllRows | Labels are always above their field. |
Member Function Documentation
QFormLayout::QFormLayout(QWidget *parent = nullptr)
Constructs a form layout for parent, with no rows.
QFormLayout::~QFormLayout()
Destroys the layout. The widgets that are in it stay.
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.
int QFormLayout::count() const
Reimplements QLayout::count().
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.
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.
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().
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().
QSize QFormLayout::sizeHint() const
Reimplements QLayout::sizeHint().
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().