|  | Home · All Classes · Modules

# QFormLayout Class Reference
[QtGui module]

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

Inherits QLayout.

### Types

- enum FieldGrowthPolicy { FieldsStayAtSizeHint, ExpandingFieldsGrow, AllNonFixedFieldsGrow }
- enum ItemRole { LabelRole, FieldRole, SpanningRole }
- enum RowWrapPolicy { DontWrapRows, WrapLongRows, WrapAllRows }

### Methods

- __init__ (self, QWidget parent = None)
- addItem (self, QLayoutItem)
- addRow (self, QWidget, QWidget)
- addRow (self, QWidget, QLayout)
- addRow (self, QString, QWidget)
- addRow (self, QString, QLayout)
- addRow (self, QWidget)
- addRow (self, QLayout)
- int count (self)
- Qt.Orientations expandingDirections (self)
- FieldGrowthPolicy fieldGrowthPolicy (self)
- Qt.Alignment formAlignment (self)
- (int, ItemRole) getItemPosition (self, int)
- (int, ItemRole) getLayoutPosition (self, QLayout)
- (int, ItemRole) getWidgetPosition (self, QWidget)
- bool hasHeightForWidth (self)
- int heightForWidth (self, int)
- int horizontalSpacing (self)
- insertRow (self, int, QWidget, QWidget)
- insertRow (self, int, QWidget, QLayout)
- insertRow (self, int, QString, QWidget)
- insertRow (self, int, QString, QLayout)
- insertRow (self, int, QWidget)
- insertRow (self, int, QLayout)
- invalidate (self)
- QLayoutItem itemAt (self, int, ItemRole)
- QLayoutItem itemAt (self, int)
- Qt.Alignment labelAlignment (self)
- QWidget labelForField (self, QWidget)
- QWidget labelForField (self, QLayout)
- QSize minimumSize (self)
- int rowCount (self)
- RowWrapPolicy rowWrapPolicy (self)
- setFieldGrowthPolicy (self, FieldGrowthPolicy)
- setFormAlignment (self, Qt.Alignment)
- setGeometry (self, QRect)
- setHorizontalSpacing (self, int)
- setItem (self, int, ItemRole, QLayoutItem)
- setLabelAlignment (self, Qt.Alignment)
- setLayout (self, int, ItemRole, QLayout)
- setRowWrapPolicy (self, RowWrapPolicy)
- setSpacing (self, int)
- setVerticalSpacing (self, int)
- setWidget (self, int, ItemRole, QWidget)
- QSize sizeHint (self)
- int spacing (self)
- QLayoutItem takeAt (self, int)
- int verticalSpacing (self)

## Detailed 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 Mac OS X 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 Mac OS X 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.

## Type Documentation

### 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.

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

### 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 and QS60Style.
`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 and and QS60Style.
`QFormLayout.WrapAllRows` | `2` | Fields are always laid out below their label.

See also rowWrapPolicy.

## Method Documentation

### QFormLayout.__init__ (self, QWidget parent = None)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a new form layout with the given parent widget.

See also QWidget.setLayout().

### QFormLayout.addItem (self, QLayoutItem)

The QLayoutItem argument has it's ownership transferred to Qt.

Reimplemented from QLayout.addItem().

### QFormLayout.addRow (self, QWidget, QWidget)

The QWidget argument has it's ownership transferred to Qt.

The QWidget argument has it's ownership transferred to Qt.

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

See also insertRow().

### QFormLayout.addRow (self, QWidget, QLayout)

The QWidget argument has it's ownership transferred to Qt.

The QLayout argument has it's ownership transferred to Qt.

This is an overloaded function.

### QFormLayout.addRow (self, QString, QWidget)

The QWidget argument has it's ownership transferred to Qt.

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.

### QFormLayout.addRow (self, QString, QLayout)

The QLayout argument has it's ownership transferred to Qt.

This is an overloaded function.

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

### QFormLayout.addRow (self, QWidget)

The QWidget argument has it's ownership transferred to Qt.

This is an overloaded function.

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

### QFormLayout.addRow (self, QLayout)

The QLayout argument has it's ownership transferred to Qt.

This is an overloaded function.

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

### int QFormLayout.count (self)

Reimplemented from QLayout.count().

### Qt.Orientations QFormLayout.expandingDirections (self)

Reimplemented from QLayoutItem.expandingDirections().

### FieldGrowthPolicy QFormLayout.fieldGrowthPolicy (self)

### Qt.Alignment QFormLayout.formAlignment (self)

### (int, ItemRole) QFormLayout.getItemPosition (self, int)

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 itemAt(), count(), getLayoutPosition(), and getWidgetPosition().

### (int, ItemRole) QFormLayout.getLayoutPosition (self, QLayout)

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.

### (int, ItemRole) QFormLayout.getWidgetPosition (self, QWidget)

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 getItemPosition() and itemAt().

### bool QFormLayout.hasHeightForWidth (self)

Reimplemented from QLayoutItem.hasHeightForWidth().

### int QFormLayout.heightForWidth (self, int)

Reimplemented from QLayoutItem.heightForWidth().

### int QFormLayout.horizontalSpacing (self)

### QFormLayout.insertRow (self, int, QWidget, QWidget)

The QWidget argument has it's ownership transferred to Qt.

The QWidget argument has it's ownership transferred to Qt.

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

### QFormLayout.insertRow (self, int, QWidget, QLayout)

The QWidget argument has it's ownership transferred to Qt.

The QLayout argument has it's ownership transferred to Qt.

This is an overloaded function.

### QFormLayout.insertRow (self, int, QString, QWidget)

The QWidget argument has it's ownership transferred to Qt.

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.

### QFormLayout.insertRow (self, int, QString, QLayout)

The QLayout argument has it's ownership transferred to Qt.

This is an overloaded function.

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

### QFormLayout.insertRow (self, int, QWidget)

The QWidget argument has it's ownership transferred to Qt.

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.

### QFormLayout.insertRow (self, int, QLayout)

The QLayout argument has it's ownership transferred to Qt.

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.

### QFormLayout.invalidate (self)

Reimplemented from QLayoutItem.invalidate().

### QLayoutItem QFormLayout.itemAt (self, int, ItemRole)

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

See also QLayout.itemAt() and setItem().

### QLayoutItem QFormLayout.itemAt (self, int)

Reimplemented from QLayout.itemAt().

### Qt.Alignment QFormLayout.labelAlignment (self)

### QWidget QFormLayout.labelForField (self, QWidget)

Returns the label associated with the given field.

See also itemAt().

### QWidget QFormLayout.labelForField (self, QLayout)

This is an overloaded function.

### QSize QFormLayout.minimumSize (self)

Reimplemented from QLayoutItem.minimumSize().

### int QFormLayout.rowCount (self)

Returns the number of rows in the form.

See also QLayout.count().

### RowWrapPolicy QFormLayout.rowWrapPolicy (self)

### QFormLayout.setFieldGrowthPolicy (self, FieldGrowthPolicy)

### QFormLayout.setFormAlignment (self, Qt.Alignment)

### QFormLayout.setGeometry (self, QRect)

Reimplemented from QLayoutItem.setGeometry().

### QFormLayout.setHorizontalSpacing (self, int)

### QFormLayout.setItem (self, int, ItemRole, QLayoutItem)

The QLayoutItem argument has it's ownership transferred to Qt.

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

### QFormLayout.setLabelAlignment (self, Qt.Alignment)

### QFormLayout.setLayout (self, int, ItemRole, QLayout)

The QLayout argument has it's ownership transferred to Qt.

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

### QFormLayout.setRowWrapPolicy (self, RowWrapPolicy)

### QFormLayout.setSpacing (self, int)

This function sets both the vertical and horizontal spacing to spacing.

See also spacing(), setVerticalSpacing(), and setHorizontalSpacing().

### QFormLayout.setVerticalSpacing (self, int)

### QFormLayout.setWidget (self, int, ItemRole, QWidget)

The QWidget argument has it's ownership transferred to Qt.

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

### QSize QFormLayout.sizeHint (self)

Reimplemented from QLayoutItem.sizeHint().

### int QFormLayout.spacing (self)

If the vertical spacing is equal to the horizontal spacing, this function returns that value; otherwise it returns -1.

See also setSpacing(), verticalSpacing(), and horizontalSpacing().

### QLayoutItem QFormLayout.takeAt (self, int)

The QLayoutItem result

Reimplemented from QLayout.takeAt().

### int QFormLayout.verticalSpacing (self)