QT c++ 代码布局原则 简单例子

本文描述QT c++ widget代码布局遵循的原则:实中套虚,虚中套实

本文最后列出了代码下载链接。

在QT6.2.4 msvc2019编译通过。

所谓是实体组件:比如界面框、文本标签、组合框、文本框、按钮、表格、图片框等。

所谓是Layout组件:比如垂直布局、水平布局、网格布局、表单布局、堆栈布局。

所谓布局是指定排列规则

1.界面

图1-界面效果

图2-布局关系示意

说明:图中的实线示意实体组件,虚线示意布局方式

2.代码

2.1界面类头文件

#ifndef WIDGET_H

#define WIDGET_H

#include <QWidget>

#include <QWidget>

#include <QGroupBox>

#include <QLabel>

#include <QLineEdit>

#include <QTextEdit>

#include <QPushButton>

QT_BEGIN_NAMESPACE

namespace Ui {

class Widget;

}

QT_END_NAMESPACE

class Widget : public QWidget

{

Q_OBJECT

public:

Widget(QWidget *parent = nullptr);

~Widget();

void initUI();

private:

Ui::Widget *ui;

QLabel * lb;

QGroupBox * gb;

QLineEdit * le;

QPushButton *btn1;

QTextEdit * txt;

};

#endif // WIDGET_H

2.2 cpp文件

#include "widget.h"

#include "ui_widget.h"

Widget::Widget(QWidget *parent)

: QWidget(parent)

, ui(new Ui::Widget)

{

ui->setupUi(this);

initUI();

}

void Widget::initUI()

{

QVBoxLayout * mainlayout = new QVBoxLayout(this);指定布局属于啥组件

lb =new QLabel(this);

lb->setText("自动");

gb=new QGroupBox();

QHBoxLayout *hboxlayout=new QHBoxLayout(gb);//指定布局属于啥组件

le=new QLineEdit(this);

le->setFixedWidth(100);

le->setFixedHeight(20);

btn1 = new QPushButton(this);

btn1->setFixedWidth(100);

btn1->setFixedHeight(20);

btn1->setText(tr("button1"));

hboxlayout->addWidget(le);//指定布局包含啥组件

hboxlayout->addWidget(btn1);//指定布局包含啥组件

// gb->setLayout(hboxlayout);//指定组件包含啥布局,因为上没有一行的参数是gb,本行可忽略

mainlayout->addWidget (lb);//指定布局包含啥组件

mainlayout->addWidget(gb);//指定布局包含啥组件

this->setLayout(mainlayout);

}

Widget::~Widget()

{

delete ui;

}

3.代码下载链接

https://download.csdn.net/download/weixin_39926429/89238564

相关推荐
笑鸿的学习笔记7 分钟前
qt-C++语法笔记之Stretch与Spacer的关系分析
c++·笔记·qt
留不住丨晚霞12 分钟前
说说SpringBoot常用的注解?
java·开发语言
hardStudy_h22 分钟前
C++——内联函数与Lambda表达式
开发语言·jvm·c++
艾莉丝努力练剑1 小时前
【C语言】学习过程教训与经验杂谈:思想准备、知识回顾(三)
c语言·开发语言·数据结构·学习·算法
ZZZS05161 小时前
stack栈练习
c++·笔记·学习·算法·动态规划
位东风1 小时前
【c++学习记录】状态模式,实现一个登陆功能
c++·学习·状态模式
witton2 小时前
Go语言网络游戏服务器模块化编程
服务器·开发语言·游戏·golang·origin·模块化·耦合
枯萎穿心攻击3 小时前
ECS由浅入深第三节:进阶?System 的行为与复杂交互模式
开发语言·unity·c#·游戏引擎
Jerry Lau3 小时前
go go go 出发咯 - go web开发入门系列(一) helloworld
开发语言·前端·golang
nananaij3 小时前
【Python基础入门 re模块实现正则表达式操作】
开发语言·python·正则表达式