内容表

上一话题

QFontDialog

下一话题

QFrame

QFormLayout

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

Inheritance diagram of PySide2.QtWidgets.QFormLayout

概要

函数

详细描述

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.

    例如, 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 wrap long rows , or even to wrap all rows .

  • Convenient API for creating label–field pairs.

    addRow() overload that takes a QString QWidget * creates a QLabel behind the scenes and automatically set up its buddy. We can then write code like this:

    formLayout = QFormLayout()
    formLayout.addRow(self.tr("&Name:"), nameLineEdit)
    formLayout.addRow(self.tr("&Email:"), emailLineEdit)
    formLayout.addRow(self.tr("&Age:"), ageSpinBox)
    setLayout(formLayout)
    													

    Compare this with the following code, written using QGridLayout :

    nameLabel = QLabel(self.tr("&Name:"))
    nameLabel.setBuddy(nameLineEdit)
    emailLabel = QLabel(self.tr("&Name:"))
    emailLabel.setBuddy(emailLineEdit)
    ageLabel = QLabel(self.tr("&Name:"))
    ageLabel.setBuddy(ageSpinBox)
    gridLayout = 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

qformlayout-win1

qformlayout-mac2

qformlayout-kde3

qformlayout-qpe4

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() ,和 setRowWrapPolicy() . For example, to simulate the form layout appearance of QMacStyle on all platforms, but with left-aligned labels, you could write:

formLayout.trowWrapPolicy(QFormLayout.DontWrapRows)
formLayout.setFieldGrowthPolicy(QFormLayout.FieldsStayAtSizeHint)
formLayout.setFormAlignment(Qt.AlignHCenter | Qt.AlignTop)
formLayout.setLabelAlignment(Qt.AlignLeft)
											
class QFormLayout ( [ parent=None ] )
param parent

QWidget

Constructs a new form layout with the given parent 小部件。

另请参阅

setLayout()

PySide2.QtWidgets.QFormLayout. FieldGrowthPolicy

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

常量

描述

QFormLayout.FieldsStayAtSizeHint

The fields never grow beyond their effective size hint . This is the default for QMacStyle.

QFormLayout.ExpandingFieldsGrow

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

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.

另请参阅

fieldGrowthPolicy

PySide2.QtWidgets.QFormLayout. RowWrapPolicy

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

常量

描述

QFormLayout.DontWrapRows

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

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

QFormLayout.WrapAllRows

Fields are always laid out below their label.

另请参阅

rowWrapPolicy

PySide2.QtWidgets.QFormLayout. ItemRole

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

常量

描述

QFormLayout.LabelRole

A label widget.

QFormLayout.FieldRole

A field widget.

QFormLayout.SpanningRole

A widget that spans label and field columns.

PySide2.QtWidgets.QFormLayout. addRow ( layout )
参数

layout QLayout

这是重载函数。

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

PySide2.QtWidgets.QFormLayout. addRow ( label , field )
参数

这是重载函数。

PySide2.QtWidgets.QFormLayout. addRow ( label , field )
参数

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

另请参阅

insertRow()

PySide2.QtWidgets.QFormLayout. addRow ( widget )
参数

widget QWidget

这是重载函数。

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

PySide2.QtWidgets.QFormLayout. addRow ( labelText , field )
参数
PySide2.QtWidgets.QFormLayout. addRow ( labelText , field )
参数
PySide2.QtWidgets.QFormLayout. fieldGrowthPolicy ( )
返回类型

FieldGrowthPolicy

PySide2.QtWidgets.QFormLayout. formAlignment ( )
返回类型

Alignment

PySide2.QtWidgets.QFormLayout. getItemPosition ( index )
参数

index int

Retrieves the row and role (column) of the item at the specified index 。若 index is out of bounds, *``rowPtr`` is set to -1; otherwise the row is stored in *``rowPtr`` and the role is stored in *``rolePtr`` .

PySide2.QtWidgets.QFormLayout. getLayoutPosition ( layout )
参数

layout QLayout

Retrieves the row and role (column) of the specified child layout 。若 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`` .

PySide2.QtWidgets.QFormLayout. getWidgetPosition ( widget )
参数

widget 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`` .

PySide2.QtWidgets.QFormLayout. horizontalSpacing ( )
返回类型

int

PySide2.QtWidgets.QFormLayout. insertRow ( row , label , field )
参数

Inserts a new row at position row in this form layout, with the given label and field 。若 row is out of bounds, the new row is added at the end.

另请参阅

addRow()

PySide2.QtWidgets.QFormLayout. insertRow ( row , labelText , field )
参数
  • row int

  • labelText – unicode

  • field QWidget

PySide2.QtWidgets.QFormLayout. insertRow ( row , labelText , field )
参数
  • row int

  • labelText – unicode

  • field QLayout

PySide2.QtWidgets.QFormLayout. insertRow ( row , widget )
参数

这是重载函数。

Inserts the specified widget 在位置 row in this form layout. The widget spans both columns. If row is out of bounds, the widget is added at the end.

PySide2.QtWidgets.QFormLayout. insertRow ( row , label , field )
参数

这是重载函数。

PySide2.QtWidgets.QFormLayout. insertRow ( row , layout )
参数

这是重载函数。

Inserts the specified layout 在位置 row in this form layout. The layout spans both columns. If row is out of bounds, the widget is added at the end.

PySide2.QtWidgets.QFormLayout. itemAt ( row , role )
参数
返回类型

QLayoutItem

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

PySide2.QtWidgets.QFormLayout. labelAlignment ( )
返回类型

Alignment

PySide2.QtWidgets.QFormLayout. labelForField ( field )
参数

field QLayout

返回类型

QWidget

这是重载函数。

PySide2.QtWidgets.QFormLayout. labelForField ( field )
参数

field QWidget

返回类型

QWidget

Returns the label associated with the given field .

另请参阅

itemAt()

PySide2.QtWidgets.QFormLayout. removeRow ( layout )
参数

layout QLayout

这是重载函数。

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() 代替。

另请参阅

takeRow()

PySide2.QtWidgets.QFormLayout. removeRow ( widget )
参数

widget QWidget

这是重载函数。

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() 代替。

另请参阅

takeRow()

PySide2.QtWidgets.QFormLayout. removeRow ( row )
参数

row 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() 代替。

另请参阅

takeRow()

PySide2.QtWidgets.QFormLayout. rowCount ( )
返回类型

int

Returns the number of rows in the form.

另请参阅

count()

PySide2.QtWidgets.QFormLayout. rowWrapPolicy ( )
返回类型

RowWrapPolicy

PySide2.QtWidgets.QFormLayout. setFieldGrowthPolicy ( policy )
参数

policy FieldGrowthPolicy

PySide2.QtWidgets.QFormLayout. setFormAlignment ( alignment )
参数

alignment Alignment

另请参阅

formAlignment()

PySide2.QtWidgets.QFormLayout. setHorizontalSpacing ( spacing )
参数

spacing int

PySide2.QtWidgets.QFormLayout. setItem ( row , role , item )
参数

设置项在给定 row 为给定 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.

警告

Do not use this function to add child layouts or child widget items. Use setLayout() or setWidget() 代替。

另请参阅

setLayout()

PySide2.QtWidgets.QFormLayout. setLabelAlignment ( alignment )
参数

alignment Alignment

另请参阅

labelAlignment()

PySide2.QtWidgets.QFormLayout. setLayout ( row , role , layout )
参数

Sets the sub-layout in the given row 为给定 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.

注意

For most applications, addRow() or insertRow() should be used instead of .

另请参阅

setWidget()

PySide2.QtWidgets.QFormLayout. setRowWrapPolicy ( policy )
参数

policy RowWrapPolicy

另请参阅

rowWrapPolicy()

PySide2.QtWidgets.QFormLayout. setVerticalSpacing ( spacing )
参数

spacing int

另请参阅

verticalSpacing()

PySide2.QtWidgets.QFormLayout. setWidget ( row , role , widget )
参数

Sets the widget in the given row 为给定 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.

注意

For most applications, addRow() or insertRow() should be used instead of .

另请参阅

setLayout()

PySide2.QtWidgets.QFormLayout. verticalSpacing ( )
返回类型

int