当前位置: 首页 > article >正文

qt 简单了解QHBoxLayout QVBoxLayout QFormLayout水平,垂直,表单布局管理器.

 QHBoxLayout水平布局,QVBoxLayout垂直布局,QFormLayout表单布局管理器,是常用的布局管理器,是用代码编写应用界面必不可少的功能类.

1.tips

这里值得注意的是,2个单选按钮(QRadioButton)同时放进一个水平布局管理器(QHBoxLayout)中,相当于放进了一个分组器中,此时,2个单选按钮只能同时选中一个.水平布局管理器,顾名思义:放在这个水平布局管理器中的3个控件,只能均匀的排列成一行.代码如下

    //添加到水平布局管理器
    QHBoxLayout* sexLayout = new QHBoxLayout;
    //添加部件,这里把2个单选按钮男女,一起放到了布局管理器中(相当于分组框),此时只能选中一个
    sexLayout->addWidget(sexLabel);    //标签
    sexLayout->addWidget(manBtn);      //单选按钮
    sexLayout->addWidget(womenBtn);    //单选按钮

2.tips

把标签和行编辑器联系起来,建立"伙伴关系".使用函数setBuddy()使2者连接起来,达到使用快捷键定位到该行编辑器进行输入.详细看如下代码.

3.tips

不通过设计模式布局界面,而是使用代码把控件,标签,行编辑器等,进行桌面布局.

#include "codelayout.h"
#include <QApplication>
#include <QLabel>
#include <QLineEdit>
#include <QFormLayout>
#include <QRadioButton>
#include <QPushButton>
#include <QSpacerItem>//空格项,分隔控件与控件的距离.



//使用代码进行构建,不使用设计模式进行设计
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    CodeLayout w;

    //标签名
    QLabel* nameLabel = new QLabel("姓名:(&N)");
    QLabel* ageLabel = new QLabel("年龄:(&A)");
    QLabel* emailLabel = new QLabel("邮箱:(&E)");
    QLabel* numLabel = new QLabel("门牌号码:");

    //文本输入框,文本编辑器
    QLineEdit* nameLineEdit = new QLineEdit;
    QLineEdit* ageLineEdit = new QLineEdit;
    QLineEdit* emailLineEdit = new QLineEdit;
    QLineEdit* numLineEdit = new QLineEdit;

    //设置伙伴关系
    nameLabel->setBuddy(nameLineEdit);
    ageLabel->setBuddy(ageLineEdit);
    emailLabel->setBuddy(emailLineEdit);


    //常用于表单,标签布局(水平),布局管理器...前面放标签lable,后面反行编辑器edit
    QFormLayout* headerLayout = new QFormLayout;//new 一个对象. QFormLayout(&w)指定w为父窗体
    headerLayout->addRow(nameLabel,nameLineEdit);
    headerLayout->addRow(ageLabel,ageLineEdit);
    headerLayout->addRow(emailLabel,emailLineEdit);
    headerLayout->addRow(numLabel,numLineEdit);

    QLabel* sexLabel = new QLabel("性别:");
    QRadioButton* manBtn = new QRadioButton;
    QRadioButton* womenBtn = new QRadioButton;
    manBtn->setText("男");
    womenBtn->setText("女");


    //添加到水平布局管理器
    QHBoxLayout* sexLayout = new QHBoxLayout;
    //添加部件,这里把2个单选按钮男女,一起放到了布局管理器中(相当于分组框),此时只能选中一个
    sexLayout->addWidget(sexLabel);
    sexLayout->addWidget(manBtn);
    sexLayout->addWidget(womenBtn);

    //空格项.分隔控件与控件的距离.提供空白空间使其不紧挨一起
    QSpacerItem* spacer = new QSpacerItem(30,30);
    QPushButton* okBtn = new QPushButton("确定");
    QHBoxLayout* spacerLayout = new QHBoxLayout;
    //使确定按钮左右缩短一点
    spacerLayout->addItem(spacer);
    spacerLayout->addWidget(okBtn);
    spacerLayout->addItem(spacer);



    //垂直布局管理器.主布局管理器.
    QVBoxLayout* mainLayout =  new QVBoxLayout(&w);//指定w为父窗体
    mainLayout->addLayout(headerLayout);
    mainLayout->addLayout(sexLayout);
    mainLayout->addItem(spacer);        //添加空隙对象
    //mainLayout->addWidget(okBtn);     //添加部件
    mainLayout->addLayout(spacerLayout);//添加确定部件按钮
    mainLayout->setMargin(10);          //与窗体的间隙
    mainLayout->setSpacing(10);         //控件间的间隙

    //设置窗体布局
    w.setLayout(mainLayout);
    w.show();


    return a.exec();
}


http://www.kler.cn/news/150205.html

相关文章:

  • BUUCTF刷题之路-pwn-ciscn_2019_n_81
  • Elasticsearch 聚合查询(Aggregation)详解
  • 虚拟机指定开放数据库3306端口
  • Golang开发之------ Beego框架
  • 异步操作的方法
  • genimage 打包镜像
  • ESP32-Web-Server编程- WebSocket 编程
  • leetcode二叉树
  • Spring Boot项目Service类单元测试自动生成
  • TikTok区块链实践:数字社交媒体的去中心化未来
  • 记录一次登录相关bug
  • Hive_last_value()
  • 解决QT信号在信号和槽连接前发出而导致槽函数未调用问题
  • 爬虫代理技术与构建本地代理池的实践
  • 亚马逊云科技向量数据库助力生成式AI成功落地实践探秘(二)
  • Java LeetCode篇-深入了解关于单链表的经典解法
  • Linux命令中的符号
  • 初学者如何入门深度学习:以手写数字字符识别为例看AI 的学习路径,一图胜千言!
  • 福德植保无人机:农业科技的新篇章
  • 小航助学题库蓝桥杯题库c++选拔赛(23年8月)(含题库教师学生账号)
  • 卷积神经网络(CNN)注意力检测
  • 统计英语单词
  • 在Docker上部署Springboot项目
  • 大一学编程怎么学?刚接触编程怎么学习,有没有中文编程开发语言工具?
  • 为什么预处理对象会提升处理的性能
  • 线性可分SVM摘记
  • java学习part23异常try catch
  • Elasticsearch:ES|QL 函数及操作符
  • HTTP常见响应码
  • 小航助学题库蓝桥杯题库c++选拔赛(22年1月)(含题库教师学生账号)