Qt常用控件之表单布局QFormLayout
表单布局QFormLayout
QFormLayout
是一个表单布局控件,属于 QGridLayout
的特殊情况,多用于左列提示,右列输入框这种 “表单” 样式。
1. 使用QFormLayout制作一个注册界面表单
addRow()
的第一个参数固定是 QLabel
,第二个参数可以是任意控件:
#include "widget.h"
#include "ui_widget.h"
#include <QFormLayout>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
QFormLayout* Flayout=new QFormLayout();
QLabel* label1=new QLabel("用户名");
QLabel* label2=new QLabel("手机号");
QLabel* label3=new QLabel("密码");
QLineEdit* line1=new QLineEdit();
QLineEdit* line2=new QLineEdit();
QLineEdit* line3=new QLineEdit();
QPushButton* pushbutton1=new QPushButton("注册");
Flayout->addRow(label1,line1);
Flayout->addRow(label2,line2);
Flayout->addRow(label3,line3);
Flayout->addRow(nullptr,pushbutton1);//nullptr能使用此处的位置空出来
this->setLayout(Flayout);
}
Widget::~Widget()
{
delete ui;
}