QFormLayout ClassThe QFormLayout class manages forms of input widgets and their associated labels. More... #include <QFormLayout> Inherits: QLayout. This class was introduced in Qt 4.4. Public Types
Properties
Public Functions
Reimplemented Public Functions
Static Public Members
Protected Functions
Additional Inherited MembersDetailed DescriptionThe 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:
The table below shows the default appearance in different styles.
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
|
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.
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().
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.
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 | fieldGrowthPolicy() const |
void | setFieldGrowthPolicy(FieldGrowthPolicy policy) |
See also formAlignment and rowWrapPolicy.
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.
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.
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.
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 | rowWrapPolicy() const |
void | setRowWrapPolicy(RowWrapPolicy policy) |
See also fieldGrowthPolicy.
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.
Constructs a new form layout with the given parent widget.
See also QWidget::setLayout().
Destroys the form layout.
Reimplemented from QLayout::addItem().
Adds a new row to the bottom of this form layout, with the given label and field.
See also insertRow().
This is an overloaded function.
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.
This is an overloaded function.
This overload automatically creates a QLabel behind the scenes with labelText as its text.
This is an overloaded function.
Adds the specified widget at the end of this form layout. The widget spans both columns.
This is an overloaded function.
Adds the specified layout at the end of this form layout. The layout spans both columns.
Reimplemented from QLayout::count().
Reimplemented from QLayoutItem::expandingDirections().
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().
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.
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().
Reimplemented from QLayoutItem::hasHeightForWidth().
Reimplemented from QLayoutItem::heightForWidth().
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().
This is an overloaded function.
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.
This is an overloaded function.
This overload automatically creates a QLabel behind the scenes with labelText as its text.
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.
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.
Reimplemented from QLayoutItem::invalidate().
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().
Reimplemented from QLayout::itemAt().
Returns the label associated with the given field.
See also itemAt().
This is an overloaded function.
Reimplemented from QLayoutItem::minimumSize().
Returns the number of rows in the form.
See also QLayout::count().
Reimplemented from QLayoutItem::setGeometry().
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().
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().
This function sets both the vertical and horizontal spacing to spacing.
See also spacing(), setVerticalSpacing(), and setHorizontalSpacing().
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().
Reimplemented from QLayoutItem::sizeHint().
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().
Reimplemented from QLayout::takeAt().