首页 > 其他分享 >Qt之窗体布局(QFormLayout)

Qt之窗体布局(QFormLayout)

时间:2023-05-19 17:55:17浏览次数:38  
标签:QFormLayout Qt QWidget 窗体 pLayout QLineEdit new addRow

窗体布局管理器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

相关文章

  • VTK 9.2 Qt 5.14 安装及错误处理
    参考VTK9.1.0在Windows10+VS2019+Qt5.15.2环境下编译安装以及VTK应用于QT_vtk-qt安装包_isongxw的博客-CSDN博客安装注意:编译release和debug,通过切换配置为release和debug,文件都是在cmake的CMAKE_INSTALL_PREFIX指定的文件夹,需要编译完一种后,把这个文件夹改名(比如debug配置,则改......
  • 威纶通HMI通过MQTT和和利时ioTDA连接
    1.1ioTDA新建产品1.2 模型定义 2.1注册设备 2.2 自动生成接入凭证  3.1 打开Epro软件,开启MQTT功能进行通信设置 3.2接口格式$oc/devices/ff67d1b8a5a815bd5249d15bde1afbcc_e17961e8c3df4cee9adea8199ae2263d/sys/gateway/sub_devices/properties......
  • Qt Creator 你必须要掌握的快捷操作
    多使用快捷键能显著提高工作效率,尽可能减少键盘,鼠标之间切换所浪费的时间。我这里列出个人认为非常重要必须掌握的 QtCreator 快捷键。看你知道几个?.1.Ctrl(按住)+Tab快速切换已打开的文件 .2.快速添加方法实体(.cpp)声明,将光标移动到h文件中的方法声明。按Alt(按住)+E......
  • QtCreator中常用快捷键总结
    F1                        查看帮助F2                        跳转到函数定义(和Ctrl+鼠标左键一样的效果)Shift+F2               声明和定义之间切换F4                       头文......
  • 通过MQTT.fx接入和利时互联平台( mqtt协议测试 )
    1.1新建产品  1.2 模型定义 2.1注册设备 2.2 自动生成接入凭证   3.1打开MQTT.fx进行通讯配置   平台端设备也会先显示在线;  3.2MQTT.fx发布接口和数据格式  $oc/devices/ff67d1b8a5a815bd5249d15bde1afbcc_e17961e8c3df4......
  • closeSocket:fail task not found, uniapp 微信小程序连接不上mqtt!!!
    原来使用的mqttjs版本为4.3.7,连接一直报错,closeSocket:failtasknotfound。降低mqtt.js版本使用4.1.0。引入mqtt的方式使用importmqttfrom'mqtt/dist/mqtt.js'!!!使用constmqtt=require('mqtt'),require方式也提示无法连接!!!......
  • Qt+QtWebApp开发笔记(二):http服务器日志系统介绍、添加日志系统至Demo测试
    前言  上一篇使用QtWebApp的基于Qt的轻量级http服务器实现了一个静态网页返回的Demo,网页服务器很重要的就是日志,因为在服务器类上并没有直接返回,所以,本篇先把日志加上。 Demo  下载地址  链接:https://pan.baidu.com/s/1BPVRLS07qk-WPi-txERKbg?pwd=1234......
  • mqtt资料收集链接
    1:https://www.cloudmqtt.com/docs-api.html 2:  https://github.com/bluerhinos/phpMQTT 3:  https://blog.risingstack.com/getting-started-with-nodejs-and-mqtt/ 4:  https://github.com/saikath/Mqtt-Example-using-javascript-nodejs 5:  https://githu......
  • Qt C++5.9开发指南
     第1章认识Qt1.1Qt简介1、Qt是一套应用程序开发类库,但与MFC不同,Qt是跨平台开发类库。2、跨平台意味着只需要编写一次程序,在不同平台上无需改动或只是需要少许改动后再编译,就可以形成不同平台上运行的版本。1.2Qt的获取与安装1.2.1Qt的许可类型1.2.2Qt的版本1、如果不......
  • Qt之格栅布局(QGridLayout)
    QGridLayout是Qt框架中的一个布局管理器类,用于将子部件按照网格方式排列。它是QLayout类的子类,可在水平和垂直方向上自动调整和布局子部件的位置和大小。以下是QGridLayout的一些特点和用法:1.网格布局:QGridLayout将子部件按照网格形式排列,每个子部件占据一个单元格。可以根据需......