【Qt】表单布局QFormLayout

 表单布局QFormLayout

Qt 还提供了 QFormLayout , 属于是 QGridLayout 的特殊情况, 专⻔⽤于实现两列表单的布局.

这种表单布局多⽤于让⽤⼾填写信息的场景. 左侧列为提⽰, 右侧列为输⼊框

例子:使用QFormLayout创建表单

(1)设置三个label、三个lineEdit

(2)设置表单布局,将上述控件添加到表单布局中。

使⽤ addRow ⽅法来添加⼀⾏. 每⾏包含两个控件. 第⼀个控件固定是 QLabel / ⽂本, 第⼆个控件则可以是任意控件.

(3)设置一个按钮,将按钮添加到表单布局的右侧

如果把第⼀个参数填写为 NULL或者nullptr, 则什么都不显⽰ 

(4)执行程序

(5)代码展示

#include "widget.h"
#include "ui_widget.h"

#include <QPushButton>
#include <QFormLayout>
#include <QLineEdit>
#include <QLabel>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 设置三个label
    QLabel* label1 = new QLabel("姓名");
    QLabel* label2 = new QLabel("年龄");
    QLabel* label3 = new QLabel("电话");

    // 设置三个lineEdit
    QLineEdit* lineEidt1 = new QLineEdit();
    QLineEdit* lineEidt2 = new QLineEdit();
    QLineEdit* lineEidt3 = new QLineEdit();

    // 设置表单布局
    QFormLayout* layout = new QFormLayout();
    this->setLayout(layout);

    // 添加到layout中
    layout->addRow(label1, lineEidt1);
    layout->addRow(label2, lineEidt2);
    layout->addRow(label3, lineEidt3);

    // 设置一个按钮,将按钮添加到layout右侧
    QPushButton* button1 = new QPushButton("提交");
    layout->addRow(nullptr, button1);

}

Widget::~Widget()
{
    delete ui;
}

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值