当前位置:网站首页>QT learning 22 layout manager (I)
QT learning 22 layout manager (I)
2022-07-03 13:58:00 【A little black sauce】
Qt Study 22 Layout manager ( One )
The problem is
current GUI The development way : Absolute positioning
Directly in Pixel level Specify the Location and size
void QWidget::move(int x, int y)
void QWidget::resize(int x, int y)
problem :
- Location and size of components Unable to adapt to changes in the parent window
Layout manager
- Solution : Layout manager
- Provide related classes Manage the layout of interface components
- can Auto arrange Interface components in the window
- After the window changes Automatically update the size of components
- Provide related classes Manage the layout of interface components
- QLayout yes Qt in Abstract base class of layout manager
- By inheritance QLayout Realized Different and complementary functions The layout manager for
- Qt Medium can Customize... As needed Layout manager
- Layout manager Not interface components , But the positioning strategy of interface components
- QBoxLayout Layout manager
- With level perhaps vertical Manage interface components in a way
- The layout manager can Nesting with each other , Form a more complex layout
- Layout nesting Almost all commonly used interface layouts can be completed
- Custom layout classes can achieve Personalized interface layout The effect of
- QBoxLayout Nested instances
Code experiments
#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(); // Initialize button control
//initControl(); // Absolute positioning , Unable to adapt window changes
//testVBoxLayout(); // Vertical layout management
//testHBoxLayout(); // Horizontal layout management
testVHBoxLayout(); // Nested layout management
}
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");
// Set the minimum size of the button
TestBtn1.setMinimumSize(160, 30);
TestBtn2.setMinimumSize(160, 30);
TestBtn3.setMinimumSize(160, 30);
TestBtn4.setMinimumSize(160, 30);
// Set button size policy
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();
// Add window controls to layout manager
layout->addWidget(&TestBtn1);
layout->addWidget(&TestBtn2);
layout->addWidget(&TestBtn3);
layout->addWidget(&TestBtn4);
// Layout manager sets the spacing between controls
layout->setSpacing(20);
// Set the layout manager of the current window
setLayout(layout);
}
void Widget::testHBoxLayout()
{
QHBoxLayout* layout = new QHBoxLayout();
// Add window controls to layout manager
layout->addWidget(&TestBtn1);
layout->addWidget(&TestBtn2);
layout->addWidget(&TestBtn3);
layout->addWidget(&TestBtn4);
// Layout manager sets the spacing between controls
layout->setSpacing(20);
// Set the layout manager of the current window
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();
}
Summary
- Absolute positioning Layout method Not adaptive Window changes
- Qt Provides related classes Manage the layout of interface components
- Qt Predefined Different and complementary functions The layout manager for
- The layout manager can Nested with each other to form a complex layout
边栏推荐
- ThreadPoolExecutor realizes multi-threaded concurrency and obtains the return value (elegant and concise way)
- 项目协作的进度如何推进| 社区征文
- GoLand 2021.1.1: configure the multi line display of the tab of the open file
- 解决MySql 1045 Access denied for user ‘root‘@‘localhost‘ (using password: YES)
- Uniapp tips - set background music
- Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
- jvm-对象生命周期
- JS continues to explore...
- Leetcode-1175.Prime Arrangements
- PhpMyAdmin stage file contains analysis traceability
猜你喜欢
Golang — 命令行工具cobra
Halcon combined with C # to detect surface defects -- Halcon routine autobahn
Unity EmbeddedBrowser浏览器插件事件通讯
【吉林大学】考研初试复试资料分享
JVM系列——概述,程序计数器day1-1
Spring cup eight school league
Mycms we media mall v3.4.1 release, user manual update
MySQL 数据处理值增删改
太阳底下无新事,元宇宙能否更上层楼?
Golang - command line tool Cobra
随机推荐
叶酸修饰的金属-有机骨架(ZIF-8)载黄芩苷|金属有机骨架复合磁性材料([email protected])|制备路线
GoLand 2021.2 configure go (go1.17.6)
Sequence table (implemented in C language)
从零开始的基于百度大脑EasyData的多人协同数据标注
Implementation of Muduo accept connection, disconnection and sending data
3D视觉——2.人体姿态估计(Pose Estimation)入门——OpenPose含安装、编译、使用(单帧、实时视频)
MySQL data processing value addition, deletion and modification
ThreadPoolExecutor realizes multi-threaded concurrency and obtains the return value (elegant and concise way)
mysql中的字段问题
[ACNOI2022]猜数
[combinatorics] permutation and combination (examples of combinatorial number of multiple sets | three counting models | selection problem | combinatorial problem of multiple sets | nonnegative intege
logback日志的整理
Mycms we media mall v3.4.1 release, user manual update
Uniapp skills - dom display and hiding
Ocean CMS vulnerability - search php
windos 创建cordova 提示 因为在此系统上禁止运行脚本
Summary of common error reporting problems and positioning methods of thrift
太阳底下无新事,元宇宙能否更上层楼?
[understanding by chance-37]: the structure of human sensory system determines that human beings are self-centered
Richview trvstyle liststyle list style (bullet number)