QFormLayout类通过管理输入控件和他们相关联的标签实现布局。 QFormLayout是一个方便的布局类,它将上面的控件分成两列布局。左栏包括标签,右列则是那些值控件(QLineEdit,QSpinBox等)。 传统上,这样的两栏式布局形式采用QGridLayout实现。 QFormLayout是更高级别的替代方案,提供了以下优点:
-
支持不同操作系统,有不同的外观。
例如,Mac OS X的Aqua和KDE的指引指定的标签应该是右对齐的,而Windows和GNOME应用程序通常使用左对齐。
-
支持自动换行
For devices with small displays, QFormLayout can be set to wrap long rows, or even to wrap all rows.
-
函数接口简单,标签和值控件,可以直接一对对的插入
void addRow(QWidget * label, QWidget * field)。
addRow() 函数在插入一个label和一个field后,自动将两者设置为buddy关系。而buddy的好处是,假如label 的快捷键是 "ALT+w",按下快捷键时,输入焦点自动跳到label的buddy——lineEdit上 。- 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-columnQGridLayout.) | Style based on the Mac OS X Aquaguidelines. 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. |
QFormLayout属性介绍
1、QFormLayout类管理输入型控件和它的label组成的那些form表格,包括它的界面参数如下图
2、界面中对应的代码如下表,
Cpp代码
- formLayout = new QFormLayout(widget1);
- formLayout->setSpacing(6);
- formLayout->setContentsMargins(11, 11, 11, 11);
- formLayout->setObjectName(QString::fromUtf8("formLayout"));
- formLayout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
- formLayout->setRowWrapPolicy(QFormLayout::DontWrapRows);
- formLayout->setContentsMargins(0, 0, 0, 0);
- label_4 = new QLabel(widget1);
- label_4->setObjectName(QString::fromUtf8("label_4"));
- formLayout->setWidget(0, QFormLayout::LabelRole, label_4);
- lineEdit = new QLineEdit(widget1);
- lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
- formLayout->setWidget(0, QFormLayout::FieldRole, lineEdit);
- label_5 = new QLabel(widget1);
- label_5->setObjectName(QString::fromUtf8("label_5"));
- formLayout->setWidget(1, QFormLayout::LabelRole, label_5);
- comboBox = new QComboBox(widget1);
- comboBox->setObjectName(QString::fromUtf8("comboBox"));
- formLayout->setWidget(1, QFormLayout::FieldRole, comboBox);
3、其中值得一说的是:LayoutFieldGrowthPolicy属性
enum 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 forQMacStyle. |
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. |
4、还有一个属性值得说:LayoutRowWrapPolicy
This property holds the way in which the form's rows wrap.
//这个属性设置了表格如何排版各个元素
If you want to display each label above its associated field (instead of next to it), set this property to WrapAllRows.
//如果你想把每个标签放在相关字段的上方,而不是和它相邻,就设置这个属性值为WrapAllRows。
enum 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 andQS60Style. |
QFormLayout::WrapAllRows | 2 | Fields are always laid out below their label. |