QtBase
v6.3.1
|
The QFormLayout class manages forms of input widgets and their associated labels. More...
#include <qformlayout.h>
Classes | |
class | TakeRowResult |
Contains the result of a QFormLayout::takeRow() call. \inmodule QtWidgets. More... | |
Public Types | |
enum | FieldGrowthPolicy { FieldsStayAtSizeHint , ExpandingFieldsGrow , AllNonFixedFieldsGrow } |
enum | RowWrapPolicy { DontWrapRows , WrapLongRows , WrapAllRows } |
enum | ItemRole { LabelRole = 0 , FieldRole = 1 , SpanningRole = 2 } |
Public Types inherited from QLayout | |
enum | SizeConstraint { SetDefaultConstraint , SetNoConstraint , SetMinimumSize , SetFixedSize , SetMaximumSize , SetMinAndMaxSize } |
Properties | |
FieldGrowthPolicy | fieldGrowthPolicy |
the way in which the form's fields grow More... | |
RowWrapPolicy | rowWrapPolicy |
the way in which the form's rows wrap More... | |
Qt::Alignment | labelAlignment |
the horizontal alignment of the labels More... | |
Qt::Alignment | formAlignment |
the alignment of the form layout's contents within the layout's geometry More... | |
int | horizontalSpacing |
the spacing between widgets that are laid out side by side More... | |
int | verticalSpacing |
the spacing between widgets that are laid out vertically More... | |
Properties inherited from QLayout | |
int | spacing |
the spacing between widgets inside the layout More... | |
QMargins | contentsMargins |
SizeConstraint | sizeConstraint |
the resize mode of the layout More... | |
Properties inherited from QObject | |
QString | objectName |
the name of this object More... | |
The QFormLayout class manages forms of input widgets and their associated labels.
\inmodule QtWidgets
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:
\list
For example, the \l{http://developer.apple.com/library/mac/#documentation/UserExperience/Conceptual/AppleHIGuidelines/Intro/Intro.html}{\macos Aqua} and KDE guidelines specify that the labels should be right-aligned, whereas Windows and GNOME applications normally use left-alignment.
For devices with small displays, QFormLayout can be set to \l{WrapLongRows}{wrap long rows}, or even to \l{WrapAllRows}{wrap all rows}.
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:
Compare this with the following code, written using QGridLayout:
\endlist
The table below shows the default appearance in different styles.
\table \header
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:
Definition at line 53 of file qformlayout.h.
This enum specifies the different policies that can be used to control the way in which the form's fields grow.
\value FieldsStayAtSizeHint The fields never grow beyond their \l{QWidgetItem::sizeHint()}{effective size hint}. This is the default for QMacStyle.
\value ExpandingFieldsGrow Fields with an horizontal \l{QSizePolicy}{size policy} of \l{QSizePolicy::}{Expanding} or \l{QSizePolicy::}{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.
\value AllNonFixedFieldsGrow 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.
Enumerator | |
---|---|
FieldsStayAtSizeHint | |
ExpandingFieldsGrow | |
AllNonFixedFieldsGrow |
Definition at line 69 of file qformlayout.h.
This enum specifies the types of widgets (or other layout items) that may appear in a row.
\value LabelRole A label widget. \value FieldRole A field widget. \value SpanningRole A widget that spans label and field columns.
Enumerator | |
---|---|
LabelRole | |
FieldRole | |
SpanningRole |
Definition at line 83 of file qformlayout.h.
This enum specifies the different policies that can be used to control the way in which the form's rows wrap.
\value DontWrapRows Fields are always laid out next to their label. This is the default policy for all styles except Qt Extended styles.
\value WrapLongRows 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.
\value WrapAllRows Fields are always laid out below their label.
Enumerator | |
---|---|
DontWrapRows | |
WrapLongRows | |
WrapAllRows |
Definition at line 76 of file qformlayout.h.
\variable QFormLayout::TakeRowResult::labelItem
Contains the layout item corresponding to the label of the row.
\variable QFormLayout::TakeRowResult::fieldItem
Contains the layout item corresponding to the field of the row.
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().
Definition at line 1198 of file qformlayout.cpp.
QFormLayout::~QFormLayout | ( | ) |
Destroys the form layout.
Definition at line 1206 of file qformlayout.cpp.
|
overridevirtual |
\reimp
Implements QLayout.
Definition at line 1634 of file qformlayout.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
This overload automatically creates a QLabel behind the scenes with labelText as its text.
Definition at line 1257 of file qformlayout.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
This overload automatically creates a QLabel behind the scenes with labelText as its text. The field is set as the new QLabel's \l{QLabel::setBuddy()}{buddy}.
Definition at line 1246 of file qformlayout.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Adds the specified layout at the end of this form layout. The layout spans both columns.
Definition at line 1279 of file qformlayout.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 1234 of file qformlayout.cpp.
Adds a new row to the bottom of this form layout, with the given label and field.
Definition at line 1226 of file qformlayout.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Adds the specified widget at the end of this form layout. The widget spans both columns.
Definition at line 1268 of file qformlayout.cpp.
|
overridevirtual |
\reimp
Implements QLayout.
Definition at line 1646 of file qformlayout.cpp.
|
overridevirtual |
QFormLayout::FieldGrowthPolicy QFormLayout::fieldGrowthPolicy | ( | ) | const |
Definition at line 1983 of file qformlayout.cpp.
Qt::Alignment QFormLayout::formAlignment | ( | ) | const |
Definition at line 2077 of file qformlayout.cpp.
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.
Definition at line 1862 of file qformlayout.cpp.
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.
Definition at line 1890 of file qformlayout.cpp.
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.
Definition at line 1910 of file qformlayout.cpp.
|
overridevirtual |
\reimp
Reimplemented from QLayoutItem.
Definition at line 1710 of file qformlayout.cpp.
|
overridevirtual |
\reimp
Reimplemented from QLayoutItem.
Definition at line 1721 of file qformlayout.cpp.
int QFormLayout::horizontalSpacing | ( | ) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
This overload automatically creates a QLabel behind the scenes with labelText as its text.
Definition at line 1351 of file qformlayout.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
This overload automatically creates a QLabel behind the scenes with labelText as its text. The field is set as the new QLabel's \l{QLabel::setBuddy()}{buddy}.
Definition at line 1329 of file qformlayout.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
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.
Definition at line 1385 of file qformlayout.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 1308 of file qformlayout.cpp.
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.
Definition at line 1291 of file qformlayout.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
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.
Definition at line 1367 of file qformlayout.cpp.
|
overridevirtual |
\reimp
Reimplemented from QLayoutItem.
Definition at line 1802 of file qformlayout.cpp.
|
overridevirtual |
QLayoutItem * QFormLayout::itemAt | ( | int | row, |
ItemRole | role | ||
) | const |
Returns the layout item in the given row with the specified role (column). Returns \nullptr if there is no such item.
Definition at line 1834 of file qformlayout.cpp.
Qt::Alignment QFormLayout::labelAlignment | ( | ) | const |
Definition at line 2047 of file qformlayout.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 1941 of file qformlayout.cpp.
Returns the label associated with the given field.
Definition at line 1922 of file qformlayout.cpp.
|
overridevirtual |
\reimp
Implements QLayoutItem.
Definition at line 1788 of file qformlayout.cpp.
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():
If you want to remove the row from the layout without deleting the widgets, use takeRow() instead.
Definition at line 1446 of file qformlayout.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
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():
If you want to remove the row from the form layout without deleting the inserted layout, use takeRow() instead.
Definition at line 1499 of file qformlayout.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
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():
If you want to remove the row from the layout without deleting the widgets, use takeRow() instead.
Definition at line 1472 of file qformlayout.cpp.
int QFormLayout::rowCount | ( | ) | const |
Returns the number of rows in the form.
Definition at line 1822 of file qformlayout.cpp.
QFormLayout::RowWrapPolicy QFormLayout::rowWrapPolicy | ( | ) | const |
Definition at line 2016 of file qformlayout.cpp.
void QFormLayout::setFieldGrowthPolicy | ( | FieldGrowthPolicy | policy | ) |
Definition at line 1974 of file qformlayout.cpp.
void QFormLayout::setFormAlignment | ( | Qt::Alignment | alignment | ) |
\reimp
Reimplemented from QLayout.
Definition at line 1747 of file qformlayout.cpp.
void QFormLayout::setHorizontalSpacing | ( | int | spacing | ) |
void QFormLayout::setItem | ( | int | row, |
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.
Definition at line 2293 of file qformlayout.cpp.
void QFormLayout::setLabelAlignment | ( | Qt::Alignment | alignment | ) |
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().
Definition at line 2271 of file qformlayout.cpp.
void QFormLayout::setRowWrapPolicy | ( | RowWrapPolicy | policy | ) |
|
overridevirtual |
This function sets both the vertical and horizontal spacing to spacing.
Reimplemented from QLayout.
Definition at line 2151 of file qformlayout.cpp.
void QFormLayout::setVerticalSpacing | ( | int | spacing | ) |
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().
Definition at line 2251 of file qformlayout.cpp.
|
overridevirtual |
\reimp
Implements QLayoutItem.
Definition at line 1775 of file qformlayout.cpp.
|
overridevirtual |
If the vertical spacing is equal to the horizontal spacing, this function returns that value; otherwise it returns -1.
Reimplemented from QLayout.
Definition at line 2164 of file qformlayout.cpp.
|
overridevirtual |
\reimp
Implements QLayout.
Definition at line 1666 of file qformlayout.cpp.
QFormLayout::TakeRowResult QFormLayout::takeRow | ( | int | row | ) |
Removes the specified row from this form layout.
row must be non-negative and less than rowCount().
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():
If you want to remove the row from the layout and delete the widgets, use removeRow() instead.
Definition at line 1528 of file qformlayout.cpp.
QFormLayout::TakeRowResult QFormLayout::takeRow | ( | QLayout * | layout | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Removes the specified layout from this form layout.
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.
If you want to remove the row from the form layout and delete the inserted layout, use removeRow() instead.
Definition at line 1613 of file qformlayout.cpp.
QFormLayout::TakeRowResult QFormLayout::takeRow | ( | QWidget * | widget | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Removes the specified widget from this form layout.
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.
If you want to remove the row from the layout and delete the widgets, use removeRow() instead.
Definition at line 1573 of file qformlayout.cpp.
int QFormLayout::verticalSpacing | ( | ) | const |
|
readwrite |
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 \l{formAlignment}{form alignment}.
Definition at line 178 of file qformlayout.h.
|
readwrite |
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.
Definition at line 178 of file qformlayout.h.
|
readwrite |
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.
Definition at line 178 of file qformlayout.h.
|
readwrite |
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.
Definition at line 178 of file qformlayout.h.
|
readwrite |
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.
Definition at line 178 of file qformlayout.h.
|
readwrite |
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.
Definition at line 178 of file qformlayout.h.