测试运行效果如图:
#include "widget.h"
#include "ui_widget.h"
#include <QFormLayout>
#include <QLineEdit>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setFixedSize(300, 300);
QFormLayout* layout = new QFormLayout(this);
QLineEdit* LineEdit1 = new QLineEdit();
QLineEdit* LineEdit2 = new QLineEdit();
QLineEdit* LineEdit3 = new QLineEdit();
QLineEdit* LineEdit4 = new QLineEdit();
QLineEdit* LineEdit5 = new QLineEdit();
layout->addRow("name:", LineEdit1);
layout->addRow("phone:", LineEdit2);
layout->addRow("mail:", LineEdit3);
layout->addRow("adddress:",LineEdit4);
layout->addRow("job:", LineEdit5);
layout->setSpacing(10);
layout->setRowWrapPolicy(QFormLayout::WrapLongRows);//当标签和文本框在本行显示不全时,文本框会显示在下一行,使得标签独占一行
layout->setLabelAlignment(Qt::AlignLeft);//设置标签的对齐方式
setWindowTitle("QFormLayout");
}