QFormLayout Class▲
-
Header: QFormLayout
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
-
qmake: QT += widgets
-
Inherits: QLayout
-
Group: QFormLayout is part of geomanagement
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:
SélectionnezQFormLayout
*
formLayout=
new
QFormLayout; formLayout-&
gt;addRow(tr("&Name:"
), nameLineEdit); formLayout-&
gt;addRow(tr("&Email:"
), emailLineEdit); formLayout-&
gt;addRow(tr("&Age:"
), ageSpinBox); setLayout(formLayout);Compare this with the following code, written using QGridLayout:
SélectionneznameLabel
=
new
QLabel(tr("&Name:"
)); nameLabel-&
gt;setBuddy(nameLineEdit); emailLabel=
new
QLabel(tr("&Name:"
)); emailLabel-&
gt;setBuddy(emailLineEdit); ageLabel=
new
QLabel(tr("&Name:"
)); ageLabel-&
gt;setBuddy(ageSpinBox); QGridLayout*
gridLayout=
new
QGridLayout; gridLayout-&
gt;addWidget(nameLabel,0
,0
); gridLayout-&
gt;addWidget(nameLineEdit,0
,1
); gridLayout-&
gt;addWidget(emailLabel,1
,0
); gridLayout-&
gt;addWidget(emailLineEdit,1
,1
); gridLayout-&
gt;addWidget(ageLabel,2
,0
); gridLayout-&
gt;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-&
gt;setRowWrapPolicy(QFormLayout::
DontWrapRows);
formLayout-&
gt;setFieldGrowthPolicy(QFormLayout::
FieldsStayAtSizeHint);
formLayout-&
gt;setFormAlignment(Qt::
AlignHCenter |
Qt::
AlignTop);
formLayout-&
gt;setLabelAlignment(Qt::
AlignLeft);
See Also▲
See also QGridLayout, QBoxLayout, 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▲
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▲
See also itemAt(), 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▲
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:
-
fieldGrowthPolicy() const
-
void setFieldGrowthPolicy( policy)
See Also▲
See also formAlignment, 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:
-
formAlignment() const
-
void setFormAlignment( alignment)
See Also▲
See also labelAlignment, 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▲
See also verticalSpacing, QStyle::pixelMetric(), 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:
-
labelAlignment() const
-
void setLabelAlignment( alignment)
See Also▲
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:
-
rowWrapPolicy() const
-
void setRowWrapPolicy( policy)
See Also▲
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▲
Member Function Documentation▲
[explicit] QFormLayout::QFormLayout(QWidget *parent = nullptr)▲
Constructs a new form layout with the given parent widget.
The layout is set directly as the top-level layout for parent. There can be only one top-level layout for a widget. It is returned by QWidget::layout().
See Also▲
See also QWidget::setLayout()
[virtual] QFormLayout::~QFormLayout()▲
Destroys the form layout.
[override virtual] void QFormLayout::addItem(QLayoutItem *item)▲
Reimplements: QLayout::addItem(QLayoutItem *item).
void QFormLayout::addRow(QWidget *label, QWidget *field)▲
Adds a new row to the bottom of this form layout, with the given label and field.
See Also▲
See also insertRow()
void QFormLayout::addRow(QWidget *label, QLayout *field)▲
This is an overloaded function.
void QFormLayout::addRow(const QString &labelText, QWidget *field)▲
This is an overloaded function.
This overload automatically creates a QLabel behind the scenes with labelText as its text. The field is set as the new QLabel's buddy.
void QFormLayout::addRow(const QString &labelText, QLayout *field)▲
This is an overloaded function.
This overload automatically creates a QLabel behind the scenes with labelText as its text.
void QFormLayout::addRow(QWidget *widget)▲
This is an overloaded function.
Adds the specified widget at the end of this form layout. The widget spans both columns.
void QFormLayout::addRow(QLayout *layout)▲
This is an overloaded function.
Adds the specified layout at the end of this form layout. The layout spans both columns.
[override virtual] int QFormLayout::count() const▲
Reimplements: QLayout::count() const.
[override virtual] Qt::Orientations QFormLayout::expandingDirections() const▲
Reimplements: QLayout::expandingDirections() const.
void QFormLayout::getItemPosition(int index, int *rowPtr, QFormLayout::ItemRole *rolePtr) const▲
Retrieves the row and role (column) of the item at the specified index. If index is out of bounds, *rowPtr is set to -1; otherwise the row is stored in *rowPtr and the role is stored in *rolePtr.
See Also▲
See also itemAt(), count(), getLayoutPosition(), getWidgetPosition()
void QFormLayout::getLayoutPosition(QLayout *layout, int *rowPtr, QFormLayout::ItemRole *rolePtr) const▲
Retrieves the row and role (column) of the specified child layout. If layout is not in the form layout, *rowPtr is set to -1; otherwise the row is stored in *rowPtr and the role is stored in *rolePtr.
void QFormLayout::getWidgetPosition(QWidget *widget, int *rowPtr, QFormLayout::ItemRole *rolePtr) const▲
Retrieves the row and role (column) of the specified widget in the layout. If widget is not in the layout, *rowPtr is set to -1; otherwise the row is stored in *rowPtr and the role is stored in *rolePtr.
See Also▲
See also getItemPosition(), itemAt()
[override virtual] bool QFormLayout::hasHeightForWidth() const▲
Reimplements: QLayoutItem::hasHeightForWidth() const.
[override virtual] int QFormLayout::heightForWidth(int width) const▲
Reimplements: QLayoutItem::heightForWidth(int) const.
void QFormLayout::insertRow(int row, QWidget *label, QWidget *field)▲
Inserts a new row at position row in this form layout, with the given label and field. If row is out of bounds, the new row is added at the end.
See Also▲
See also addRow()
void QFormLayout::insertRow(int row, QWidget *label, QLayout *field)▲
This is an overloaded function.
void QFormLayout::insertRow(int row, const QString &labelText, QWidget *field)▲
This is an overloaded function.
This overload automatically creates a QLabel behind the scenes with labelText as its text. The field is set as the new QLabel's buddy.
void QFormLayout::insertRow(int row, const QString &labelText, QLayout *field)▲
This is an overloaded function.
This overload automatically creates a QLabel behind the scenes with labelText as its text.
void QFormLayout::insertRow(int row, QWidget *widget)▲
This is an overloaded function.
Inserts the specified widget at position row in this form layout. The widget spans both columns. If row is out of bounds, the widget is added at the end.
void QFormLayout::insertRow(int row, QLayout *layout)▲
This is an overloaded function.
Inserts the specified layout at position row in this form layout. The layout spans both columns. If row is out of bounds, the widget is added at the end.
[override virtual] void QFormLayout::invalidate()▲
Reimplements: QLayout::invalidate().
[since 6.4] bool QFormLayout::isRowVisible(int row) const▲
Returns true if some items in the row row are visible, otherwise returns false.
This function was introduced in Qt 6.4.
[since 6.4] bool QFormLayout::isRowVisible(QWidget *widget) const▲
This is an overloaded function.
Returns true if some items in the row corresponding to widget are visible, otherwise returns false.
This function was introduced in Qt 6.4.
[since 6.4] bool QFormLayout::isRowVisible(QLayout *layout) const▲
This is an overloaded function.
Returns true if some items in the row corresponding to layout are visible, otherwise returns false.
This function was introduced in Qt 6.4.
QLayoutItem *QFormLayout::itemAt(int row, QFormLayout::ItemRole role) const▲
Returns the layout item in the given row with the specified role (column). Returns nullptr if there is no such item.
See Also▲
See also QLayout::itemAt(), setItem()
[override virtual] QLayoutItem *QFormLayout::itemAt(int index) const▲
Reimplements: QLayout::itemAt(int index) const.
QWidget *QFormLayout::labelForField(QWidget *field) const▲
QWidget *QFormLayout::labelForField(QLayout *field) const▲
This is an overloaded function.
[override virtual] QSize QFormLayout::minimumSize() const▲
Reimplements: QLayout::minimumSize() const.
[since 5.8] void QFormLayout::removeRow(int row)▲
Deletes row row from this form layout.
row must be non-negative and less than rowCount().
After this call, rowCount() is decremented by one. All widgets and nested layouts that occupied this row are deleted. That includes both the field widget(s) and the label, if any. All following rows are shifted up one row and the freed vertical space is redistributed amongst the remaining rows.
You can use this function to undo a previous addRow() or insertRow():
QFormLayout *
flay =
...;
QPointer&
lt;QLineEdit&
gt; le =
new
QLineEdit;
flay-&
gt;insertRow(2
, "User:"
, le);
// later:
flay-&
gt;removeRow(2
); // le == nullptr at this point
If you want to remove the row from the layout without deleting the widgets, use takeRow() instead.
This function was introduced in Qt 5.8.
See Also▲
See also takeRow()
[since 5.8] void QFormLayout::removeRow(QWidget *widget)▲
This is an overloaded function.
Deletes the row corresponding to widget from this form layout.
After this call, rowCount() is decremented by one. All widgets and nested layouts that occupied this row are deleted. That includes both the field widget(s) and the label, if any. All following rows are shifted up one row and the freed vertical space is redistributed amongst the remaining rows.
You can use this function to undo a previous addRow() or insertRow():
QFormLayout *
flay =
...;
QPointer&
lt;QLineEdit&
gt; le =
new
QLineEdit;
flay-&
gt;insertRow(2
, "User:"
, le);
// later:
flay-&
gt;removeRow(le); // le == nullptr at this point
If you want to remove the row from the layout without deleting the widgets, use takeRow() instead.
This function was introduced in Qt 5.8.
See Also▲
See also takeRow()
[since 5.8] void QFormLayout::removeRow(QLayout *layout)▲
This is an overloaded function.
Deletes the row corresponding to layout from this form layout.
After this call, rowCount() is decremented by one. All widgets and nested layouts that occupied this row are deleted. That includes both the field widget(s) and the label, if any. All following rows are shifted up one row and the freed vertical space is redistributed amongst the remaining rows.
You can use this function to undo a previous addRow() or insertRow():
QFormLayout *
flay =
...;
QPointer&
lt;QVBoxLayout&
gt; vbl =
new
QVBoxLayout;
flay-&
gt;insertRow(2
, "User:"
, vbl);
// later:
flay-&
gt;removeRow(layout); // vbl == nullptr at this point
If you want to remove the row from the form layout without deleting the inserted layout, use takeRow() instead.
This function was introduced in Qt 5.8.
See Also▲
See also takeRow()
int QFormLayout::rowCount() const▲
[override virtual] void QFormLayout::setGeometry(const QRect &rect)▲
Reimplements: QLayout::setGeometry(const QRect &r).
void QFormLayout::setItem(int row, QFormLayout::ItemRole role, QLayoutItem *item)▲
Sets the item in the given row for the given role to item, extending the layout with empty rows if necessary.
If the cell is already occupied, the item is not inserted and an error message is sent to the console. The item spans both columns.
Do not use this function to add child layouts or child widget items. Use setLayout() or setWidget() instead.
See Also▲
See also setLayout()
void QFormLayout::setLayout(int row, QFormLayout::ItemRole role, QLayout *layout)▲
Sets the sub-layout in the given row for the given role to layout, extending the form layout with empty rows if necessary.
If the cell is already occupied, the layout is not inserted and an error message is sent to the console.
Note: For most applications, addRow() or insertRow() should be used instead of setLayout().
See Also▲
See also setWidget()
[since 6.4] void QFormLayout::setRowVisible(int row, bool on)▲
Shows the row row if on is true, otherwise hides the row.
row must be non-negative and less than rowCount().
This function was introduced in Qt 6.4.
See Also▲
See also isRowVisible(), removeRow(), takeRow()
[since 6.4] void QFormLayout::setRowVisible(QWidget *widget, bool on)▲
This is an overloaded function.
Shows the row corresponding to widget if on is true, otherwise hides the row.
This function was introduced in Qt 6.4.
See Also▲
[since 6.4] void QFormLayout::setRowVisible(QLayout *layout, bool on)▲
This is an overloaded function.
Shows the row corresponding to layout if on is true, otherwise hides the row.
This function was introduced in Qt 6.4.
See Also▲
[override virtual] void QFormLayout::setSpacing(int spacing)▲
Reimplements an access function for property: QLayout::spacing.
This function sets both the vertical and horizontal spacing to spacing.
See Also▲
See also spacing(), setVerticalSpacing(), setHorizontalSpacing()
void QFormLayout::setWidget(int row, QFormLayout::ItemRole role, QWidget *widget)▲
Sets the widget in the given row for the given role to widget, extending the layout with empty rows if necessary.
If the cell is already occupied, the widget is not inserted and an error message is sent to the console.
Note: For most applications, addRow() or insertRow() should be used instead of setWidget().
See Also▲
See also setLayout()
[override virtual] QSize QFormLayout::sizeHint() const▲
Reimplements: QLayoutItem::sizeHint() const.
[override virtual] int QFormLayout::spacing() const▲
Reimplements an access function for property: QLayout::spacing.
If the vertical spacing is equal to the horizontal spacing, this function returns that value; otherwise it returns -1.
See Also▲
See also setSpacing(), verticalSpacing(), horizontalSpacing()
[override virtual] QLayoutItem *QFormLayout::takeAt(int index)▲
Reimplements: QLayout::takeAt(int index).
[since 5.8] QFormLayout::TakeRowResult QFormLayout::takeRow(int row)▲
Removes the specified row from this form layout.
row must be non-negative and less than rowCount().
This function doesn't delete anything.
After this call, rowCount() is decremented by one. All following rows are shifted up one row and the freed vertical space is redistributed amongst the remaining rows.
You can use this function to undo a previous addRow() or insertRow():
QFormLayout *
flay =
...;
QPointer&
lt;QLineEdit&
gt; le =
new
QLineEdit;
flay-&
gt;insertRow(2
, "User:"
, le);
// later:
QFormLayout::
TakeRowResult result =
flay-&
gt;takeRow(2
);
If you want to remove the row from the layout and delete the widgets, use removeRow() instead.
Returns A structure containing both the widget and corresponding label layout items
This function was introduced in Qt 5.8.
See Also▲
See also removeRow()
[since 5.8] QFormLayout::TakeRowResult QFormLayout::takeRow(QWidget *widget)▲
This is an overloaded function.
Removes the specified widget from this form layout.
This function doesn't delete anything.
After this call, rowCount() is decremented by one. All following rows are shifted up one row and the freed vertical space is redistributed amongst the remaining rows.
QFormLayout *
flay =
...;
QPointer&
lt;QLineEdit&
gt; le =
new
QLineEdit;
flay-&
gt;insertRow(2
, "User:"
, le);
// later:
QFormLayout::
TakeRowResult result =
flay-&
gt;takeRow(widget);
If you want to remove the row from the layout and delete the widgets, use removeRow() instead.
Returns A structure containing both the widget and corresponding label layout items
This function was introduced in Qt 5.8.
See Also▲
See also removeRow()
[since 5.8] QFormLayout::TakeRowResult QFormLayout::takeRow(QLayout *layout)▲
This is an overloaded function.
Removes the specified layout from this form layout.
This function doesn't delete anything.
After this call, rowCount() is decremented by one. All following rows are shifted up one row and the freed vertical space is redistributed amongst the remaining rows.
QFormLayout *
flay =
...;
QPointer&
lt;QVBoxLayout&
gt; vbl =
new
QVBoxLayout;
flay-&
gt;insertRow(2
, "User:"
, vbl);
// later:
QFormLayout::
TakeRowResult result =
flay-&
gt;takeRow(widget);
If you want to remove the row from the form layout and delete the inserted layout, use removeRow() instead.
Returns A structure containing both the widget and corresponding label layout items
This function was introduced in Qt 5.8.
See Also▲
See also removeRow()