当前位置:网站首页>Qt学习22 布局管理器(一)
Qt学习22 布局管理器(一)
2022-07-03 13:23:00 【一个小黑酱】
Qt学习22 布局管理器(一)
存在的问题
目前的GUI开发方式:绝对定位
直接在像素级指定各个组件的位置和大小
void QWidget::move(int x, int y)
void QWidget::resize(int x, int y)
问题:
- 组件的位置和大小无法自适应父窗口的变化
布局管理器
- 解决方案:布局管理器
- 提供相关的类对界面组件进行布局管理
- 能够自动排列窗口中的界面组件
- 窗口变化后自动更新组件的大小
- 提供相关的类对界面组件进行布局管理
- QLayout是Qt中布局管理器的抽象基类
- 通过继承QLayout实现了功能各异且互补的布局管理器
- Qt中可以根据需要自定义布局管理器
- 布局管理器不是界面部件,而是界面部件的定位策略

- QBoxLayout布局管理器
- 以水平或者垂直的方式管理界面组件


- 布局管理器可以相互嵌套,形成更加复杂的布局方式
- 布局嵌套几乎可以完成所有常用的界面布局
- 自定义布局类可以达到个性化界面布局的效果
- QBoxLayout嵌套实例

代码实验
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
class Widget : public QWidget
{
Q_OBJECT
private:
QPushButton TestBtn1;
QPushButton TestBtn2;
QPushButton TestBtn3;
QPushButton TestBtn4;
void initBtn();
void initControl();
void testVBoxLayout();
void testHBoxLayout();
void testVHBoxLayout();
public:
Widget(QWidget *parent = 0);
~Widget();
};
#endif // WIDGET_H
#include "Widget.h"
#include <QBoxLayout>
Widget::Widget(QWidget *parent) : QWidget(parent),
TestBtn1(this), TestBtn2(this), TestBtn3(this), TestBtn4(this)
{
initBtn(); // 初始化按钮控件
//initControl(); // 绝对定位,无法自适应窗口变化
//testVBoxLayout(); // 垂直布局管理
//testHBoxLayout(); // 水平布局管理
testVHBoxLayout(); // 嵌套布局管理
}
Widget::~Widget()
{
}
void Widget::initBtn()
{
TestBtn1.setText("Test Button 1");
TestBtn2.setText("Test Button 2");
TestBtn3.setText("Test Button 3");
TestBtn4.setText("Test Button 4");
// 设置按钮最小大小
TestBtn1.setMinimumSize(160, 30);
TestBtn2.setMinimumSize(160, 30);
TestBtn3.setMinimumSize(160, 30);
TestBtn4.setMinimumSize(160, 30);
// 设置按钮大小策略
TestBtn1.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
TestBtn2.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
TestBtn3.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
TestBtn4.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}
void Widget::initControl()
{
TestBtn1.setText("Test Button 1");
TestBtn1.move(20, 20);
TestBtn1.resize(160, 30);
TestBtn2.setText("Test Button 2");
TestBtn2.move(20, 70);
TestBtn2.resize(160, 30);
TestBtn3.setText("Test Button 3");
TestBtn3.move(20, 120);
TestBtn3.resize(160, 30);
TestBtn4.setText("Test Button 4");
TestBtn4.move(20, 170);
TestBtn4.resize(160, 30);
}
void Widget::testVBoxLayout()
{
QVBoxLayout* layout = new QVBoxLayout();
// 布局管理器添加窗口控件
layout->addWidget(&TestBtn1);
layout->addWidget(&TestBtn2);
layout->addWidget(&TestBtn3);
layout->addWidget(&TestBtn4);
// 布局管理器设置控件间的间距
layout->setSpacing(20);
// 设置当前窗口的布局管理器
setLayout(layout);
}
void Widget::testHBoxLayout()
{
QHBoxLayout* layout = new QHBoxLayout();
// 布局管理器添加窗口控件
layout->addWidget(&TestBtn1);
layout->addWidget(&TestBtn2);
layout->addWidget(&TestBtn3);
layout->addWidget(&TestBtn4);
// 布局管理器设置控件间的间距
layout->setSpacing(20);
// 设置当前窗口的布局管理器
setLayout(layout);
}
void Widget::testVHBoxLayout()
{
QHBoxLayout* hLayout1 = new QHBoxLayout();
QHBoxLayout* hLayout2 = new QHBoxLayout();
QVBoxLayout* vLayout = new QVBoxLayout();
hLayout1->addWidget(&TestBtn1);
hLayout1->addWidget(&TestBtn2);
hLayout1->setSpacing(20);
hLayout2->addWidget(&TestBtn3);
hLayout2->addWidget(&TestBtn4);
hLayout2->setSpacing(20);
vLayout->addLayout(hLayout1);
vLayout->addLayout(hLayout2);
vLayout->setSpacing(20);
setLayout(vLayout);
}
#include "Widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
小结
- 绝对定位的布局方式无法自适应窗口的变化
- Qt提供了相关的类对界面组件进行布局管理
- Qt预定义了功能各异且互补的布局管理器
- 布局管理器能够相互嵌套形成复杂的布局
边栏推荐
- SQL Injection (POST/Search)
- Leetcode-1175.Prime Arrangements
- [quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion
- Red hat satellite 6: better management of servers and clouds
- Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases
- Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
- GoLand 2021.1: rename the go project
- 掌握Cypress命令行选项,是真正掌握Cypress的基础
- logback日志的整理
- MyCms 自媒体商城 v3.4.1 发布,使用手册更新
猜你喜欢

Kivy教程之 盒子布局 BoxLayout将子项排列在垂直或水平框中(教程含源码)

Go language web development series 27: Gin framework: using gin swagger to implement interface documents

Logback log sorting

Setting up remote links to MySQL on Linux

挡不住了,国产芯片再度突进,部分环节已进到4nm

Flutter动态化 | Fair 2.5.0 新版本特性

Screenshot of the operation steps of upload labs level 4-level 9

Golang — 命令行工具cobra

Ocean CMS vulnerability - search php

太阳底下无新事,元宇宙能否更上层楼?
随机推荐
Leetcode-1175. Prime Arrangements
[技术发展-24]:现有物联网通信技术特点
Libuv库 - 设计概述(中文版)
[understanding by chance-37]: the structure of human sensory system determines that human beings are self-centered
Unity Render Streaming通过Js与Unity自定义通讯
Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
php 迷宫游戏
Spark practice 1: build spark operation environment in single node local mode
RichView TRVStyle ListStyle 列表样式(项目符号编号)
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
如何使用lxml判断网站公告是否更新
JS 将伪数组转换成数组
顺序表(C语言实现)
[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
项目协作的进度如何推进| 社区征文
Go language web development series 29: Gin framework uses gin contrib / sessions library to manage sessions (based on cookies)
Resolved (error in viewing data information in machine learning) attributeerror: target_ names
Windos creates Cordova prompt because running scripts is prohibited on this system
[développement technologique - 24]: caractéristiques des technologies de communication Internet des objets existantes
Halcon combined with C # to detect surface defects -- Halcon routine autobahn