The QFormLayout class manages forms of input widgets and their associated labels.
More...
|
| | QFormLayout (QWidget *parent=nullptr) |
| |
| | ~QFormLayout () |
| |
| void | addItem (QLayoutItem *item) override |
| |
| void | addRow (const QString &labelText, QLayout *field) |
| |
| void | addRow (const QString &labelText, QWidget *field) |
| |
| void | addRow (QLayout *layout) |
| |
| void | addRow (QWidget *label, QLayout *field) |
| |
| void | addRow (QWidget *label, QWidget *field) |
| |
| void | addRow (QWidget *widget) |
| |
| int | count () const override |
| |
| Qt::Orientations | expandingDirections () const override |
| |
| FieldGrowthPolicy | fieldGrowthPolicy () const |
| |
| Qt::Alignment | formAlignment () const |
| |
| void | getItemPosition (int index, int *rowPtr, ItemRole *rolePtr) const |
| |
| void | getLayoutPosition (QLayout *layout, int *rowPtr, ItemRole *rolePtr) const |
| |
| void | getWidgetPosition (QWidget *widget, int *rowPtr, ItemRole *rolePtr) const |
| |
| bool | hasHeightForWidth () const override |
| |
| int | heightForWidth (int width) const override |
| |
| int | horizontalSpacing () const |
| |
| void | insertRow (int row, const QString &labelText, QLayout *field) |
| |
| void | insertRow (int row, const QString &labelText, QWidget *field) |
| |
| void | insertRow (int row, QLayout *layout) |
| |
| void | insertRow (int row, QWidget *label, QLayout *field) |
| |
| void | insertRow (int row, QWidget *label, QWidget *field) |
| |
| void | insertRow (int row, QWidget *widget) |
| |
| void | invalidate () override |
| |
| QLayoutItem * | itemAt (int index) const override |
| |
| QLayoutItem * | itemAt (int row, ItemRole role) const |
| |
| Qt::Alignment | labelAlignment () const |
| |
| QWidget * | labelForField (QLayout *field) const |
| |
| QWidget * | labelForField (QWidget *field) const |
| |
| QSize | minimumSize () const override |
| |
| int | rowCount () const |
| |
| RowWrapPolicy | rowWrapPolicy () const |
| |
| void | setFieldGrowthPolicy (FieldGrowthPolicy policy) |
| |
| void | setFormAlignment (Qt::Alignment alignment) |
| |
| void | setGeometry (const QRect &rect) override |
| |
| void | setHorizontalSpacing (int spacing) |
| |
| void | setItem (int row, ItemRole role, QLayoutItem *item) |
| |
| void | setLabelAlignment (Qt::Alignment alignment) |
| |
| void | setLayout (int row, ItemRole role, QLayout *layout) |
| |
| void | setRowWrapPolicy (RowWrapPolicy policy) |
| |
| void | setSpacing (int spacing) |
| |
| void | setVerticalSpacing (int spacing) |
| |
| void | setWidget (int row, ItemRole role, QWidget *widget) |
| |
| QSize | sizeHint () const override |
| |
| int | spacing () const |
| |
| QLayoutItem * | takeAt (int index) override |
| |
| int | verticalSpacing () const |
| |
| | QLayout () |
| |
| | QLayout (QWidget *parent) |
| |
| bool | activate () |
| |
| void | addWidget (QWidget *w) |
| |
| QMargins | contentsMargins () const |
| |
| QRect | contentsRect () const |
| |
| QSizePolicy::ControlTypes | controlTypes () const override |
| |
| Qt::Orientations | expandingDirections () const override |
| |
| QRect | geometry () const override |
| |
| void | getContentsMargins (int *left, int *top, int *right, int *bottom) const |
| |
| virtual int | indexOf (QWidget *widget) const |
| |
| void | invalidate () override |
| |
| bool | isEmpty () const override |
| |
| bool | isEnabled () const |
| |
| QLayout * | layout () override |
| |
| int | margin () const |
| |
| QSize | maximumSize () const override |
| |
| QWidget * | menuBar () const |
| |
| QSize | minimumSize () const override |
| |
| QWidget * | parentWidget () const |
| |
| void | removeItem (QLayoutItem *item) |
| |
| void | removeWidget (QWidget *widget) |
| |
| virtual QLayoutItem * | replaceWidget (QWidget *from, QWidget *to, Qt::FindChildOptions options=Qt::FindChildrenRecursively) |
| |
| bool | setAlignment (QLayout *layout, Qt::Alignment alignment) |
| |
| void | setAlignment (Qt::Alignment alignment) |
| |
| bool | setAlignment (QWidget *widget, Qt::Alignment alignment) |
| |
| void | setContentsMargins (const QMargins &margins) |
| |
| void | setContentsMargins (int left, int top, int right, int bottom) |
| |
| void | setEnabled (bool enable) |
| |
| void | setGeometry (const QRect &rect) override |
| |
| void | setMargin (int margin) |
| |
| void | setMenuBar (QWidget *widget) |
| |
| void | setSizeConstraint (SizeConstraint constraint) |
| |
| void | setSpacing (int spacing) |
| |
| SizeConstraint | sizeConstraint () const |
| |
| int | spacing () const |
| |
| void | update () |
| |
| | QObject (QObject *parent=nullptr) |
| |
| | ~QObject () |
| |
| bool | blockSignals (bool block) |
| |
| const QList< QObject * > & | children () const |
| |
| bool | connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
| |
| bool | connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
| |
| bool | disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const |
| |
| bool | disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const |
| |
| bool | disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const |
| |
| void | dumpObjectInfo () |
| |
| void | dumpObjectTree () |
| |
| QList< QString > | dynamicPropertyNames () const |
| |
| virtual bool | event (QEvent *event) |
| |
| virtual bool | eventFilter (QObject *watched, QEvent *event) |
| |
| template<typename T > |
| T | findChild (const QString &childName=QString ()) const |
| |
| template<class T > |
| QList< T > | findChildren (const QRegularExpression ®Exp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const |
| |
| template<class T > |
| QList< T > | findChildren (const QString &childName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const |
| |
| bool | inherits (const QString &className) const |
| |
| void | installEventFilter (QObject *filterObj) |
| |
| bool | isWidgetType () const |
| |
| bool | isWindowType () const |
| |
| void | killTimer (int id) |
| |
| const QMetaObject * | metaObject () const |
| |
| void | moveToThread (QThread *targetThread) |
| |
| QString | objectName () const |
| |
| QObject * | parent () const |
| |
| template<class T = QVariant> |
| T | property (const QString &name) const |
| |
| void | removeEventFilter (QObject *obj) |
| |
| void | setObjectName (const QString &name) |
| |
| void | setParent (QObject *parent) |
| |
| bool | setProperty (const QString &name, const QVariant &value) |
| |
| bool | signalsBlocked () const |
| |
| int | startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer) |
| |
| QThread * | thread () const |
| |
| | QLayoutItem (Qt::Alignment alignment=Qt::Alignment ()) |
| |
| virtual | ~QLayoutItem () |
| |
| Qt::Alignment | alignment () const |
| |
| virtual int | minimumHeightForWidth (int width) const |
| |
| void | setAlignment (Qt::Alignment alignment) |
| |
| virtual QSpacerItem * | spacerItem () |
| |
| virtual QWidget * | widget () |
| |
|
| void | destroyed (QObject *obj=nullptr) |
| |
| void | objectNameChanged (const QString &objectName) |
| |
| void | deleteLater () |
| |
| static QSize | closestAcceptableSize (const QWidget *widget, const QSize &size) |
| |
| static bool | connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
| |
| static bool | connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ()) |
| |
| static bool | connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
| |
| template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn > |
| static bool | connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection) |
| |
| template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T > |
| static bool | connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection) |
| |
| static bool | disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod) |
| |
| static bool | disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod) |
| |
| static bool | disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod) |
| |
| static bool | disconnect (const QObject *sender, std::nullptr_t, const QObject *receiver, std::nullptr_t) |
| |
| template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn > |
| static bool | disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...)) |
| |
| template<class Sender , class SignalClass , class... SignalArgs, class Receiver > |
| static bool | disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr) |
| |
| template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T > |
| static bool | disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod) |
| |
| static QMetaObject & | staticMetaObject () |
| |
| static QString | tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >()) |
| |
| void | addChildLayout (QLayout *layout) |
| |
| void | addChildWidget (QWidget *widget) |
| |
| QRect | alignmentRect (const QRect &rect) const |
| |
| void | childEvent (QChildEvent *event) override |
| |
| virtual void | childEvent (QChildEvent *event) |
| |
| virtual void | connectNotify (const QMetaMethod &signalMethod) const |
| |
| virtual void | customEvent (QEvent *event) |
| |
| virtual void | disconnectNotify (const QMetaMethod &signalMethod) const |
| |
| bool | isSignalConnected (const QMetaMethod &signalMethod) const |
| |
| int | receivers (const QString &signal) const |
| |
| QObject * | sender () const |
| |
| int | senderSignalIndex () const |
| |
| virtual void | timerEvent (QTimerEvent *event) |
| |
| T | qobject_cast (QObject *object) |
| |
| | QObjectList |
| |
The QFormLayout class manages two column forms of input widgets and their associated labels. The left column is usually the label control and the right column is a widget like a text line editor, spin box or combo box.
Using a QFormLayout has the following advantages over using a QGridLayout.
-
Uses the platform look and feel. For example, Mac OS X and KDE guidelines specify the labels should be right aligned. While Windows and GNOME applications normally use left alignment.
-
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.
Appearance
The table below shows the default appearance in different styles.
| QCommonStyle Derived Styles | QMacStyle | QFusionStyle | 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. | Style based on the Mac OS X Aqua guidelines. Labels are right-aligned, the fields do not 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 CopperSpice Extended styles. Labels are right-aligned, expanding fields grow to fill the available space, and row wrapping is enabled for long lines. |
The styles can be overridden by calling setLabelAlignment(), setFormAlignment(), setFieldGrowthPolicy(), and setRowWrapPolicy(). For example, to simulate the form layout appearance of QMacStyle use the following code.
formLayout->setRowWrapPolicy(QFormLayout::DontWrapRows);
formLayout->setFieldGrowthPolicy(QFormLayout::FieldsStayAtSizeHint);
formLayout->setFormAlignment(Qt::AlignHCenter | Qt::AlignTop);
formLayout->setLabelAlignment(Qt::AlignRight);
Example
Here is an example using QGridLayout.
nameLabel->setBuddy(nameLineEdit);
emailLabel =
new QLabel(
tr(
"&Email address:"));
emailLabel->setBuddy(emailLineEdit);
ageLabel->setBuddy(ageSpinBox);
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);
The following code uses QGridLayout and will produce a very similar output.
formLayout->
addRow(
tr(
"&Name:"), nameLineEdit);
formLayout->
addRow(
tr(
"&Email address:"), emailLineEdit);
formLayout->
addRow(
tr(
"&Age:"), ageSpinBox);
- See also
- QGridLayout, QBoxLayout, QStackedLayout