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

相关推荐
luj_176836 分钟前
大律师考核应重能力与科技素养
c语言·开发语言·c++·经验分享·算法
刃神太酷啦1 小时前
Linux 系统 MySQL 完整安装配置教程:从卸载 MariaDB 到优化 my.cnf----《Hello MySQL!》(1)
android·linux·c语言·c++·mysql·leetcode·mariadb
leonkay2 小时前
C# 特性(Attribute)——【1】基础讲解
开发语言·青少年编程·面试·c#·.net·个人开发
harmful_sheep2 小时前
java group by常见用法
java·开发语言·python
SKH.2 小时前
Linux软件编程(6)线程间通信
java·开发语言·数据库
whcyhhh2 小时前
头歌实践教学平台:数据科学与大数据技术导论(十六)
大数据·开发语言·python
萌动的小火苗2 小时前
数据结构面试题【无答案】
c语言·开发语言·数据结构·链表
宸津-代码粉碎机2 小时前
AI工程化高阶实战|从Demo到生产落地核心架构、全套源码与避坑指南
java·大数据·开发语言·人工智能·python·架构
XR1234567883 小时前
办公网自主创新建设怎么选?信创办公场景的选型逻辑
开发语言·php
郝学胜-神的一滴4 小时前
Effective Python 条款4:字符串格式化大乱斗
开发语言·网络·python·程序人生·软件工程