博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Qt5.9布局管理器实例(QVBoxLayout,QHBoxLayout,QGridLayout)(一个简单的手写界面实例)
阅读量:3532 次
发布时间:2019-05-20

本文共 3957 字,大约阅读时间需要 13 分钟。

本博客主要总结用Qt5.9手写一个界面,该界面的pushButton,linetxt控件都用代码来实现,同时手写水平布局、垂直布局、栅格布局,具体的用法如下所述。

注意:QGridLayout,QHLayout,QVLayout三个布局管理器类,可以混合包括,但是有且只能有一个主要布局。比如栅格布局里面包括了水平布局,垂直布局,栅格布局三块。

1.1新建一个widget工程,然后分别在widget.h,widget.cpp分别添加如下代码,main.cpp函数不变。

widget.h

#ifndef WIDGET_H
#define WIDGET_H
 
 
#include 
 
#include 
//垂直布局
#include 
//水平布局
#include 
//栅格布局
 
 
#include 
//按钮空间类
#include 
//单行文本类
#include 
//labal标签类
 
 
class Widget : public QWidget
{
Q_OBJECT
 
public:
Widget(QWidget *parent = 0);
~Widget();
 
private:
QPushButton *btn1;                      //定义一个按钮
QHBoxLayout *hlayout1, *hlayout2;       //两个水平布局
QVBoxLayout *vlayout3;                  //垂直布局
QGridLayout *glayout4;                  //栅格布局
QLineEdit *edit1, *edit2, *edit3;       //单行文本框
QLabel *label11;                        //标签11
QLabel *label12;                        //标签12
 
private slots:
void on_clicked();                           //单击按钮槽函数
 
};
 
#endif // WIDGET_H

widget.cpp

#include "widget.h"
 
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
//窗口UI界面初始化
hlayout1 = new QHBoxLayout;                     //水平布局初始化
hlayout2 = new QHBoxLayout;
vlayout3 = new QVBoxLayout(this);               //垂直布局初始化   this表示在Widget窗口垂直布局
 
//    glayout4 = new QGridLayout(this);               //栅格布局初始化   this表示在Widget窗口栅格布局
 
btn1 = new QPushButton;                         //按钮初始化
edit1 = new QLineEdit;                          //单行文本初始化
edit2 = new QLineEdit;                          //单行文本初始化
edit3 = new QLineEdit;                          //单行文本初始化
label11 = new QLabel;                           //label标签初始化
label12 = new QLabel;                           //这个控件没有任何父控件
 
//栅格布局方式
//    glayout4->addWidget(btn1,0,0);                  //将按钮加入水平布局(0,0)    表示第0行,第0列
//    glayout4->addWidget(edit1,0,1);                 //将文本加入水平布局(0,1)
//    glayout4->addWidget(edit2,0,2);
//    glayout4->addWidget(edit3,1,0);                 //将文本加入水平布局(0,1)    表示第0行,第1列
//    glayout4->addWidget(label11,1,1);               //将label标签加入水平布局(1,1)
//    btn1->setText(tr("确定"));                        //初始化label标签内容
//    label11->setText(tr("label"));
 
//水平布局方式    用垂直布局包裹水平布局
hlayout1->setMargin(50);                        //设置布局两边间距
hlayout1->addWidget(btn1);                      //水平布局hlayout1添加按钮对象
hlayout1->addWidget(edit1);
hlayout1->addWidget(edit2);
hlayout1->addWidget(edit3);
hlayout2->addWidget(label11);                   //水平布局hlayout2添加标签对象
label11->setText(tr("label%1").arg(rand()));
vlayout3->addLayout(hlayout1);                  //垂直布局vlayout3添加水平布局hlayout1
vlayout3->addLayout(hlayout2);                  //垂直布局vlayout3添加水平布局hlayout2
 
//    this->resize(300,100);                          //定义窗口尺寸
 
//点击btn1按钮,触发槽函数on_clicked()
connect(btn1,SIGNAL(clicked(bool)),this,SLOT(on_clicked()));    //关联函数  将信号与槽绑定
}
 
Widget::~Widget()
{
//delete hlayout1;在QT内部,不要单独delete一个控件的指针
//QT的窗口在退出的时,自动delete它相关的子控件
delete label12;                                 //释放label12标签内存
}
 
void Widget::on_clicked()
{
edit1->setText(tr("I an %1 lineTxt").arg(rand()));
edit2->setText(tr("I an %1 lineTxt").arg(rand()));
edit3->setText(tr("I an %1 lineTxt").arg(rand()));
label11->setText(tr("label%1").arg(rand()));
}
 

1.2编译运行后结果如下图所示:

注意:下面有且只能有一个this指针。

hlayout1 = new QHBoxLayout;                     //水平布局初始化
hlayout2 = new QHBoxLayout;
vlayout3 = new QVBoxLayout(this);               //垂直布局初始化   this表示在Widget窗口垂直布局

程序在运用布局管理器时,上面的this表示在Widget窗口中,所以布局管理器只能选择一个this,不能出现两个布局管理器,同时调用一个this。也就说,当用栅格布局管理器初始化时,用了this指针,则在垂直管理器中,就不能用this初始化,否则会出现布局冲突。

参考内容:

http://www.cnblogs.com/Bonker/p/3454956.html(详细参考,可以设置各个空间间距,布局边缘距离)

https://blog.csdn.net/jingzhesiye/article/details/6657166(综合应用:栅格布局里面嵌入水平布局、垂直布局、栅格布局)

https://blog.csdn.net/tototuzuoquan/article/details/38480613

https://blog.csdn.net/ddupd/article/details/38292277

你可能感兴趣的文章
Omap138开发板下以uboot2012.04.01为例分析uboot执行(七)
查看>>
Omap138开发板下以uboot2012.04.01为例分析uboot执行(八)
查看>>
中国大学MOOC—陆军工程大学数据结构MOOC习题集(2018秋)7-3 中位数
查看>>
Java发送邮件 注册成功发送邮件
查看>>
Mybatis的简单使用(增删改查),解决数据库字段名和实体类映射属性名不一致的问题
查看>>
Mybatis配置log4j文件 分页查询(limit,rowBounds)
查看>>
Mysql利用注解进行开发
查看>>
Mybatis一对多查询,多对一查询
查看>>
Spring配置bean.xml文件的头目录模板
查看>>
代理模式之------动态代理
查看>>
Spring实现AOP的三种方式
查看>>
Mybatis-Spring简单的配置和使用,配置事务
查看>>
SpringMVC和Mybatis整合使用的配置文件
查看>>
代码特效插件pycharm
查看>>
python实现tcp客户端从服务端下载文件
查看>>
将字符串 “k:1|k1:2|k2:3|k3:4” 转换成字典{“k”:1,”k1”:2,”k2”:3,”k3”:4}
查看>>
AttributeError: 'tuple' object has no attribute 'decode'
查看>>
node爬虫(牛刀小试)
查看>>
关于vue的seo优化
查看>>
字符串在html中的页面中的换行
查看>>