窗体布局管理器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); QFormLayout *pLayout = new QFormLayout(this); pLayout->addRow("用户名:", pUserEdit); pLayout->addRow("密码:", pPasswordEdit); pLayout->addRow("验证码:", pVerifyLineEdit); pLayout->setSpacing(10); pLayout->setMargin(10); this->setLayout(pLayout); }
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)
下面是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); }
void setRowWrapPolicy(QFormLayout::RowWrapPolicy policy):设置换行策略
QFormLayout::RowWrapPolicy::DontWrapRows:输入框始终在标签旁边
pLayout->setRowWrapPolicy(QFormLayout::RowWrapPolicy::DontWrapRows);
QFormLayout::RowWrapPolicy::WrapAllRows:输入框始终在标签下下边
pLayout->setRowWrapPolicy(QFormLayout::RowWrapPolicy::WrapAllRows);
QFormLayout::RowWrapPolicy::WrapLongRows:标签有足够的空间适应,如果最小大小比可用空间大,则输入框则会被换到下一行
#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,Qt,QWidget,窗体,pLayout,QLineEdit,new,addRow From: https://www.cnblogs.com/QingYiShouJiuRen/p/17415929.html