QFormLayout Class
The QFormLayout class manages forms of input widgets and their associated labels. More...
| Header: | #include <QFormLayout> |
| qmake: | QT += widgets |
| Since: | Qt 4.4 |
| Inherits: | QLayout |
This class was introduced in Qt 4.4.
Public Types
| struct | TakeRowResult |
| enum | FieldGrowthPolicy { FieldsStayAtSizeHint, ExpandingFieldsGrow, AllNonFixedFieldsGrow } |
| enum | ItemRole { LabelRole, FieldRole, SpanningRole } |
| enum | RowWrapPolicy { DontWrapRows, WrapLongRows, WrapAllRows } |
Properties
|
|
Public Functions
| QFormLayout::FieldGrowthPolicy | fieldGrowthPolicy() const |
| Qt::Alignment | formAlignment() const |
| int | horizontalSpacing() const |
| Qt::Alignment | labelAlignment() const |
| QFormLayout::RowWrapPolicy | rowWrapPolicy() const |
| void | setFieldGrowthPolicy(QFormLayout::FieldGrowthPolicy policy) |
| void | setFormAlignment(Qt::Alignment alignment) |
| void | setHorizontalSpacing(int spacing) |
| void | setLabelAlignment(Qt::Alignment alignment) |
| void | setRowWrapPolicy(QFormLayout::RowWrapPolicy policy) |
| void | setVerticalSpacing(int spacing) |
| int | verticalSpacing() const |
Detailed Description
QFormLayout is a convenience layout class that lays out its children in a two-column form. The left column consists of labels and the right column consists of "field" widgets (line editors, spin boxes, etc.).
Traditionally, such two-column form layouts were achieved using QGridLayout. QFormLayout is a higher-level alternative that provides the following advantages:
- Adherence to the different platform's look and feel guidelines.
For example, the macOS Aqua and KDE guidelines specify that the labels should be right-aligned, whereas Windows and GNOME applications normally use left-alignment.
- Support for wrapping long rows.
For devices with small displays, QFormLayout can be set to wrap long rows, or even to wrap all rows.
- Convenient API for creating label--field pairs.
The addRow() overload that takes a QString and a QWidget * creates a QLabel behind the scenes and automatically set up its buddy. We can then write code like this:
QFormLayout *formLayout = new QFormLayout; formLayout->addRow(tr("&Name:"), nameLineEdit); formLayout->addRow(tr("&Email:"), emailLineEdit); formLayout->addRow(tr("&Age:"), ageSpinBox); setLayout(formLayout);
Compare this with the following code, written using QGridLayout:
nameLabel = new QLabel(tr("&Name:")); nameLabel->setBuddy(nameLineEdit); emailLabel = new QLabel(tr("&Name:")); emailLabel->setBuddy(emailLineEdit); ageLabel = new QLabel(tr("&Name:")); ageLabel->setBuddy(ageSpinBox); QGridLayout *gridLayout = new QGridLayout; gridLayout->addWidget(nameLabel, 0, 0); gridLayout->addWidget(nameLineEdit, 0, 1); gridLayout->addWidget(emailLabel, 1, 0); gridLayout->addWidget(emailLineEdit, 1, 1); gridLayout->addWidget(ageLabel, 2, 0); gridLayout->addWidget(ageSpinBox, 2, 1); setLayout(gridLayout);
The table below shows the default appearance in different styles.
| QCommonStyle derived styles (except QPlastiqueStyle) | QMacStyle | QPlastiqueStyle | Qt Extended styles |
|---|---|---|---|
![]() | ![]() | ![]() | ![]() |
| Traditional style used for Windows, GNOME, and earlier versions of KDE. Labels are left aligned, and expanding fields grow to fill the available space. (This normally corresponds to what we would get using a two-column QGridLayout.) | Style based on the macOS Aqua guidelines. Labels are right-aligned, the fields don't grow beyond their size hint, and the form is horizontally centered. | Recommended style for KDE applications. Similar to MacStyle, except that the form is left-aligned and all fields grow to fill the available space. | Default style for Qt Extended styles. Labels are right-aligned, expanding fields grow to fill the available space, and row wrapping is enabled for long lines. |
The form styles can be also be overridden individually by calling setLabelAlignment(), setFormAlignment(), setFieldGrowthPolicy(), and setRowWrapPolicy(). For example, to simulate the form layout appearance of QMacStyle on all platforms, but with left-aligned labels, you could write:
formLayout->setRowWrapPolicy(QFormLayout::DontWrapRows); formLayout->setFieldGrowthPolicy(QFormLayout::FieldsStayAtSizeHint); formLayout->setFormAlignment(Qt::AlignHCenter | Qt::AlignTop); formLayout->setLabelAlignment(Qt::AlignLeft);
See also QGridLayout, QBoxLayout, and QStackedLayout.
Member Type Documentation
enum QFormLayout::FieldGrowthPolicy
This enum specifies the different policies that can be used to control the way in which the form's fields grow.
| Constant | Value | Description |
|---|---|---|
QFormLayout::FieldsStayAtSizeHint | 0 | The fields never grow beyond their effective size hint. This is the default for QMacStyle. |
QFormLayout::ExpandingFieldsGrow | 1 | Fields with an horizontal size policy of Expanding or MinimumExpanding will grow to fill the available space. The other fields will not grow beyond their effective size hint. This is the default policy for Plastique. |
QFormLayout::AllNonFixedFieldsGrow | 2 | All fields with a size policy that allows them to grow will grow to fill the available space. This is the default policy for most styles. |
See also fieldGrowthPolicy.
enum QFormLayout::ItemRole
This enum specifies the types of widgets (or other layout items) that may appear in a row.
| Constant | Value | Description |
|---|---|---|
QFormLayout::LabelRole | 0 | A label widget. |
QFormLayout::FieldRole | 1 | A field widget. |
QFormLayout::SpanningRole | 2 | A widget that spans label and field columns. |
See also itemAt() and getItemPosition().
enum QFormLayout::RowWrapPolicy
This enum specifies the different policies that can be used to control the way in which the form's rows wrap.
| Constant | Value | Description |
|---|---|---|
QFormLayout::DontWrapRows | 0 | Fields are always laid out next to their label. This is the default policy for all styles except Qt Extended styles. |
QFormLayout::WrapLongRows | 1 | Labels are given enough horizontal space to fit the widest label, and the rest of the space is given to the fields. If the minimum size of a field pair is wider than the available space, the field is wrapped to the next line. This is the default policy for Qt Extended styles. |
QFormLayout::WrapAllRows | 2 | Fields are always laid out below their label. |
See also rowWrapPolicy.
Property Documentation
fieldGrowthPolicy : FieldGrowthPolicy
This property holds the way in which the form's fields grow
The default value depends on the widget or application style. For QMacStyle, the default is FieldsStayAtSizeHint; for QCommonStyle derived styles (like Plastique and Windows), the default is ExpandingFieldsGrow; for Qt Extended styles, the default is AllNonFixedFieldsGrow.
If none of the fields can grow and the form is resized, extra space is distributed according to the current form alignment.
Access functions:
| QFormLayout::FieldGrowthPolicy | fieldGrowthPolicy() const |
| void | setFieldGrowthPolicy(QFormLayout::FieldGrowthPolicy policy) |
See also formAlignment and rowWrapPolicy.
formAlignment : Qt::Alignment
This property holds the alignment of the form layout's contents within the layout's geometry
The default value depends on the widget or application style. For QMacStyle, the default is Qt::AlignHCenter | Qt::AlignTop; for the other styles, the default is Qt::AlignLeft | Qt::AlignTop.
Access functions:
| Qt::Alignment | formAlignment() const |
| void | setFormAlignment(Qt::Alignment alignment) |
See also labelAlignment and rowWrapPolicy.
horizontalSpacing : int
This property holds the spacing between widgets that are laid out side by side
By default, if no value is explicitly set, the layout's horizontal spacing is inherited from the parent layout, or from the style settings for the parent widget.
Access functions:
| int | horizontalSpacing() const |
| void | setHorizontalSpacing(int spacing) |
See also verticalSpacing, QStyle::pixelMetric(), and PM_LayoutHorizontalSpacing.
labelAlignment : Qt::Alignment
This property holds the horizontal alignment of the labels
The default value depends on the widget or application style. For QCommonStyle derived styles, except for QPlastiqueStyle, the default is Qt::AlignLeft; for the other styles, the default is Qt::AlignRight.
Access functions:
| Qt::Alignment | labelAlignment() const |
| void | setLabelAlignment(Qt::Alignment alignment) |
See also formAlignment.
rowWrapPolicy : RowWrapPolicy
This property holds the way in which the form's rows wrap
The default value depends on the widget or application style. For Qt Extended styles, the default is WrapLongRows; for the other styles, the default is DontWrapRows.
If you want to display each label above its associated field (instead of next to it), set this property to WrapAllRows.
Access functions:
| QFormLayout::RowWrapPolicy | rowWrapPolicy() const |
| void | setRowWrapPolicy(QFormLayout::RowWrapPolicy policy) |
See also fieldGrowthPolicy.
verticalSpacing : int
This property holds the spacing between widgets that are laid out vertically
By default, if no value is explicitly set, the layout's vertical spacing is inherited from the parent layout, or from the style settings for the parent widget.
Access functions:
| int | verticalSpacing() const |
| void | setVerticalSpacing(int spacing) |
See also horizontalSpacing, QStyle::pixelMetric(), and PM_LayoutHorizontalSpacing.



