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

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,后面反行编辑器editQFormLayout* 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.lryc.cn/news/246983.html

相关文章:

  • springboot中4级配置文件优先级
  • Python(八十九)函数的参数的内存分析
  • 西南科技大学C++程序设计实验二(类与对象一)
  • 代码随想录二刷 |哈希表 |四数之和
  • KMP算法【数据结构】
  • 测开笔记--Typescript: 文件复制到指定目录
  • 数字滚动vue-count-to
  • 扩散模型实战(十一):剖析Stable Diffusion Pipeline各个组件
  • Mysql面试题总结
  • 学习知识随笔(Django)
  • 基于element自动表格
  • Python基础语法之学习数据转换
  • 最新AI创作系统ChatGPT网站运营源码、支持GPT-4-Turbo模型,图片对话识图理解,支持DALL-E3文生图
  • Kotlin中常见的List使用
  • 汽车电子 -- 车载ADAS之LCA(变道辅助系统)
  • MongoDB——golang操作(链接,CURD,聚合)
  • 音视频项目—基于FFmpeg和SDL的音视频播放器解析(十八)
  • 绿色能源守护者:光伏运维无人机
  • i已学赋能智慧教育时代的幼儿教育
  • [栈迁移+ret滑梯]gyctf_2020_borrowstack
  • PTA:用函数实现从数列中删除一个数
  • C++设计模式之工厂模式(中)——工厂模式
  • 关于el-table的二次封装及使用,支持自定义列内容
  • 【Vue】Vue3 配置全局 scss 变量
  • C语言—二维数组
  • GUI加分游戏
  • 多线程的重要资料-信号量
  • 区块链相关技术、概念以及技术实现过程中的一些关键问题 Smart Contracts and Blockchains
  • 黑马点评Redis笔记
  • MX6ULL学习笔记 (一)交叉工具链的安装