QFormLayout

PyQt6.QtWidgets.QFormLayout

Inherits from QLayout.

Description

The QFormLayout class manages forms of input widgets and their associated labels.

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 WrapLongRows, or even to WrapAllRows.

  • 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

image-qformlayout-win-png

image-qformlayout-mac-png

image-qformlayout-kde-png

image-qformlayout-qpe-png

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);

Classes

TakeRowResult

Enums

FieldGrowthPolicy

This enum specifies the different policies that can be used to control the way in which the form’s fields grow.

Member

Value

Description

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.

ExpandingFieldsGrow

1

Fields with an horizontal QSizePolicy 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.

FieldsStayAtSizeHint

0

The fields never grow beyond their sizeHint(). This is the default for QMacStyle.


ItemRole

This enum specifies the types of widgets (or other layout items) that may appear in a row.

Member

Value

Description

FieldRole

1

A field widget.

LabelRole

0

A label widget.

SpanningRole

2

A widget that spans label and field columns.


RowWrapPolicy

This enum specifies the different policies that can be used to control the way in which the form’s rows wrap.

See also

rowWrapPolicy().

Member

Value

Description

DontWrapRows

0

Fields are always laid out next to their label. This is the default policy for all styles except Qt Extended styles.

WrapAllRows

2

Fields are always laid out below their label.

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.

Methods

__init__(parent: QWidget = None)

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 layout().

See also

setLayout().


addItem(QLayoutItem)

TODO


addRow(QWidget)

This is an overloaded function.

Adds the specified widget at the end of this form layout. The widget spans both columns.


addRow(QLayout)

This is an overloaded function.

Adds the specified layout at the end of this form layout. The layout spans both columns.


addRow(QWidget, QWidget)

Adds a new row to the bottom of this form layout, with the given label and field.

See also

insertRow().


addRow(QWidget, QLayout)

This is an overloaded function.


addRow(Optional[str], QWidget)

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 setBuddy().


addRow(Optional[str], QLayout)

This is an overloaded function.

This overload automatically creates a QLabel behind the scenes with labelText as its text.


count() int

TODO


expandingDirections() Orientation

TODO


fieldGrowthPolicy() FieldGrowthPolicy

formAlignment() AlignmentFlag

See also

setFormAlignment().


getItemPosition(int) (int, ItemRole)

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.


getLayoutPosition(QLayout) (int, ItemRole)

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.


getWidgetPosition(QWidget) (int, ItemRole)

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.


hasHeightForWidth() bool

TODO


heightForWidth(int) int

TODO


horizontalSpacing() int

insertRow(int, QWidget)

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.


insertRow(int, QLayout)

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.


insertRow(int, QWidget, QWidget)

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

addRow().


insertRow(int, QWidget, QLayout)

This is an overloaded function.


insertRow(int, Optional[str], QWidget)

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 setBuddy().


insertRow(int, Optional[str], QLayout)

This is an overloaded function.

This overload automatically creates a QLabel behind the scenes with labelText as its text.


invalidate()

TODO


isRowVisible(QLayout) bool

This is an overloaded function.

Returns true if some items in the row corresponding to layout are visible, otherwise returns false.


isRowVisible(QWidget) bool

This is an overloaded function.

Returns true if some items in the row corresponding to widget are visible, otherwise returns false.


isRowVisible(int) bool

Returns true if some items in the row row are visible, otherwise returns false.


itemAt(int) QLayoutItem

TODO


itemAt(int, ItemRole) QLayoutItem

Returns the layout item in the given row with the specified role (column). Returns nullptr if there is no such item.

See also

itemAt(), setItem().


labelAlignment() AlignmentFlag

labelForField(QWidget) QWidget

Returns the label associated with the given field.

See also

itemAt().


labelForField(QLayout) QWidget

This is an overloaded function.


minimumSize() QSize

TODO


removeRow(int)

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<QLineEdit> le = new QLineEdit;
# flay->insertRow(2, "User:", le);
# // later:
# flay->removeRow(2); // le == nullptr at this point

If you want to remove the row from the layout without deleting the widgets, use takeRow() instead.

See also

takeRow().


removeRow(QWidget)

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<QLineEdit> le = new QLineEdit;
# flay->insertRow(2, "User:", le);
# // later:
# flay->removeRow(le); // le == nullptr at this point

If you want to remove the row from the layout without deleting the widgets, use takeRow() instead.

See also

takeRow().


removeRow(QLayout)

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<QVBoxLayout> vbl = new QVBoxLayout;
# flay->insertRow(2, "User:", vbl);
# // later:
# flay->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.

See also

takeRow().


rowCount() int

Returns the number of rows in the form.

See also

count().


rowWrapPolicy() RowWrapPolicy

See also

setRowWrapPolicy().


setFieldGrowthPolicy(FieldGrowthPolicy)

setFormAlignment(AlignmentFlag)

See also

formAlignment().


setGeometry(QRect)

TODO


setHorizontalSpacing(int)

setItem(int, ItemRole, QLayoutItem)

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.

Warning: Do not use this function to add child layouts or child widget items. Use setLayout() or setWidget() instead.

See also

setLayout().


setLabelAlignment(AlignmentFlag)

See also

labelAlignment().


setLayout(int, ItemRole, QLayout)

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 .

See also

setWidget().


setRowVisible(QLayout, bool)

This is an overloaded function.

Shows the row corresponding to layout if on is true, otherwise hides the row.

See also

removeRow(), takeRow().


setRowVisible(QWidget, bool)

This is an overloaded function.

Shows the row corresponding to widget if on is true, otherwise hides the row.

See also

removeRow(), takeRow().


setRowVisible(int, bool)

Shows the row row if on is true, otherwise hides the row.

row must be non-negative and less than rowCount().


setRowWrapPolicy(RowWrapPolicy)

See also

rowWrapPolicy().


setSpacing(int)

TODO


setVerticalSpacing(int)

See also

verticalSpacing().


setWidget(int, ItemRole, QWidget)

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 .

See also

setLayout().


sizeHint() QSize

TODO


spacing() int

TODO


takeAt(int) QLayoutItem

TODO


takeRow(int) TakeRowResult

Removes the specified row from this form layout.

row must be non-negative and less than rowCount().

Note: 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<QLineEdit> le = new QLineEdit;
# flay->insertRow(2, "User:", le);
# // later:
# QFormLayout::TakeRowResult result = flay->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

See also

removeRow().


takeRow(QWidget) TakeRowResult

This is an overloaded function.

Removes the specified widget from this form layout.

Note: 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<QLineEdit> le = new QLineEdit;
# flay->insertRow(2, "User:", le);
# // later:
# QFormLayout::TakeRowResult result = flay->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

See also

removeRow().


takeRow(QLayout) TakeRowResult

This is an overloaded function.

Removes the specified layout from this form layout.

Note: 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<QVBoxLayout> vbl = new QVBoxLayout;
# flay->insertRow(2, "User:", vbl);
# // later:
# QFormLayout::TakeRowResult result = flay->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

See also

removeRow().


verticalSpacing() int