Qt开发:窗体布局(QFormLayout)

一、QFormLayout介绍

窗体布局管理器QFormLayout用来管理表单的输入部件以及与它们相关的标签,窗体布局管理器将它的子部件分为两列,左边是一些标签,右边是一些输入部件。

二、QFormLayout的常用函数

AddRow()常用的重载函数

void addRow(QWidget *label, QWidget *field)
void addRow(QWidget *label, QLayout *field)
void addRow(const QString &labelText, QWidget *field)
void addRow(const QString &labelText, QLayout *field)

代码示例如下:

#include "main_window.h"

MainWindow::MainWindow(QWidget *parent)
    : QWidget(parent)
{
   QLineEdit *pUserEdit = new QLineEdit(this);
   QLineEdit *pPasswordEdit = new QLineEdit(this);
   QLineEdit *pVerifyLineEdit = new QLineEdit(this);

   QFormLayout *pLayout = new QFormLayout(this);
   pLayout->addRow("用户名:", pUserEdit);
   pLayout->addRow("密码:", pPasswordEdit);
   pLayout->addRow("验证码:", pVerifyLineEdit);
   pLayout->setSpacing(10);
   pLayout->setMargin(10);

   this->setLayout(pLayout);
}

在这里插入图片描述

设置换行策略,输入框始终在标签旁边

void setRowWrapPolicy(QFormLayout::RowWrapPolicy policy)

在这里插入图片描述
输入框始终在标签下边

pLayout->setRowWrapPolicy(QFormLayout::RowWrapPolicy::WrapAllRows);

在这里插入图片描述
标签有足够的空间适应,如果最小大小比可用空间大,则输入框则会被换到

#include "main_window.h"

MainWindow::MainWindow(QWidget *parent)
    : QWidget(parent)
{
   this->setFixedWidth(200);

   QLineEdit *pUserEdit = new QLineEdit(this);
   QLineEdit *pPasswordEdit = new QLineEdit(this);
   QLineEdit *pVerifyLineEdit = new QLineEdit(this);

   QFormLayout *pLayout = new QFormLayout(this);
   pLayout->addRow("用户名11111111111111111:", pUserEdit);
   pLayout->addRow("密码:", pPasswordEdit);
   pLayout->addRow("验证码:", pVerifyLineEdit);
   pLayout->setRowWrapPolicy(QFormLayout::RowWrapPolicy::WrapLongRows);
   pLayout->setSpacing(10);
   pLayout->setMargin(10);

   this->setLayout(pLayout);
}

在这里插入图片描述

三、QFormLayout布局管理器的嵌套的示例

#include "main_window.h"

MainWindow::MainWindow(QWidget *parent)
    : QWidget(parent)
{
   QLineEdit *pUserEdit = new QLineEdit(this);
   QLineEdit *pPasswordEdit = new QLineEdit(this);
   QLineEdit *pVerifyLineEdit = new QLineEdit(this);
   QLineEdit *pTestEdit = new QLineEdit(this);

   QVBoxLayout *pVLayout = new QVBoxLayout();
   pVLayout->addWidget(pVerifyLineEdit);
   pVLayout->addWidget(pTestEdit);

   QFormLayout *pLayout = new QFormLayout(this);
   pLayout->addRow("用户名:", pUserEdit);
   pLayout->addRow("密码:", pPasswordEdit);
   pLayout->addRow("验证码:", pVLayout);
   pLayout->setSpacing(10);
   pLayout->setMargin(10);

   this->setLayout(pLayout);
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值