当前位置:网站首页>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预定义了功能各异且互补的布局管理器
- 布局管理器能够相互嵌套形成复杂的布局
边栏推荐
- MySQL 数据处理值增删改
- Screenshot of the operation steps of upload labs level 4-level 9
- Start signing up CCF C ³- [email protected] chianxin: Perspective of Russian Ukrainian cyber war - Security confrontation and sanctions g
- Mastering the cypress command line options is the basis for truly mastering cypress
- Go: send the get request and parse the return JSON (go1.16.4)
- [技術發展-24]:現有物聯網通信技術特點
- Ocean CMS vulnerability - search php
- Unity Render Streaming通过Js与Unity自定义通讯
- Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
- Asp. Net core1.1 without project JSON, so as to generate cross platform packages
猜你喜欢
Summary of common error reporting problems and positioning methods of thrift
SQL Injection (POST/Search)
太阳底下无新事,元宇宙能否更上层楼?
Golang - command line tool Cobra
3D视觉——2.人体姿态估计(Pose Estimation)入门——OpenPose含安装、编译、使用(单帧、实时视频)
物联网毕设 --(STM32f407连接云平台检测数据)
Libuv库 - 设计概述(中文版)
Students who do not understand the code can also send their own token, which is easy to learn BSC
Flutter dynamic | fair 2.5.0 new version features
Go language web development series 29: Gin framework uses gin contrib / sessions library to manage sessions (based on cookies)
随机推荐
[how to earn a million passive income]
软件测试工作那么难找,只有外包offer,我该去么?
GoLand 2021.2 configure go (go1.17.6)
MySQL 数据增删改查综合案例
Stack application (balancer)
Leetcode-1175.Prime Arrangements
Unity Render Streaming通过Js与Unity自定义通讯
GoLand 2021.1: rename the go project
Kivy教程之 盒子布局 BoxLayout将子项排列在垂直或水平框中(教程含源码)
掌握Cypress命令行选项,是真正掌握Cypress的基础
Replace the GPU card number when pytorch loads the historical model, map_ Location settings
HALCON联合C#检测表面缺陷——HALCON例程autobahn
Mysql:insert date:SQL 错误 [1292] [22001]: Data truncation: Incorrect date value:
JVM系列——概述,程序计数器day1-1
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Red Hat Satellite 6:更好地管理服务器和云
This math book, which has been written by senior ml researchers for 7 years, is available in free electronic version
RichView TRVStyle ListStyle 列表样式(项目符号编号)
pytorch 载入历史模型时更换gpu卡号,map_location设置
[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion