当前位置:网站首页>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
边栏推荐
- Go language web development series 30: gin: grouping by version for routing
- jvm-运行时数据区
- pytorch 载入历史模型时更换gpu卡号,map_location设置
- MySQL 数据增删改查综合案例
- Stack application (balancer)
- Go language web development series 26: Gin framework: demonstrates the execution sequence of code when there are multiple middleware
- Ocean CMS vulnerability - search php
- 静态链表(数组的下标代替指针)
- 全面发展数字经济主航道 和数集团积极推动UTONMOS数藏市场
- 如何使用lxml判断网站公告是否更新
猜你喜欢
![[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion](/img/3b/28327bbf5eb19254f03500a41e2adb.jpg)
[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion

Mycms we media mall v3.4.1 release, user manual update

NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon

SQL Injection (GET/Select)

How to use lxml to judge whether the website announcement is updated

“又土又穷”的草根高校,凭什么被称为“东北小清华”?

Uniapp tips - scrolling components

全面发展数字经济主航道 和数集团积极推动UTONMOS数藏市场

Students who do not understand the code can also send their own token, which is easy to learn BSC

SQL Injection (POST/Search)
随机推荐
软件测试工作那么难找,只有外包offer,我该去么?
pytorch 载入历史模型时更换gpu卡号,map_location设置
Windos creates Cordova prompt because running scripts is prohibited on this system
Golang — 命令行工具cobra
RocksDB LRUCache
[combinatorics] permutation and combination (two counting principles, examples of set permutation | examples of set permutation and circle permutation)
Go language unit test 5: go language uses go sqlmock and Gorm to do database query mock
logback日志的整理
Unity EmbeddedBrowser浏览器插件事件通讯
Flutter dynamic | fair 2.5.0 new version features
[技术发展-24]:现有物联网通信技术特点
FPGA测试方法以Mentor工具为例
Golang - command line tool Cobra
Go: send the get request and parse the return JSON (go1.16.4)
Go language web development series 28: solve cross domain access of CORS with gin contrib / CORS
Go 1.16.4: manage third-party libraries with Mod
[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion
栈应用(平衡符)
金属有机骨架MOFs装载非甾体类抗炎药物|ZIF-8包裹普鲁士蓝负载槲皮素(制备方法)
Common network state detection and analysis tools