当前位置:网站首页>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
边栏推荐
- MIL-100( Fe) 包裹小分子阿司匹林形成[email protected](Fe)|甘草次酸修饰金属有机框架材料UiO-66-NH2(简称UiO-66-NH2-GA)
- Unity Render Streaming通过Js与Unity自定义通讯
- Golang — template
- [understanding by chance-37]: the structure of human sensory system determines that human beings are self-centered
- Uniapp skills - scrolling components -1
- 使用vscode查看Hex或UTF-8编码
- 核酸修饰的金属有机框架药物载体|PCN-223金属有机骨架包载Ad金刚烷|ZIF-8包裹阿霉素(DOX)
- Mysql:insert date:sql error [1292] [22001]: data truncation: incorrect date value:
- [ACNOI2022]猜数
- JS general form submission 1-onsubmit
猜你喜欢

Uniapp tips - scrolling components

解决MySql 1045 Access denied for user ‘root‘@‘localhost‘ (using password: YES)
![Mysql:insert date:sql error [1292] [22001]: data truncation: incorrect date value:](/img/2f/33504391a661ecb63d42d75acf3a37.png)
Mysql:insert date:sql error [1292] [22001]: data truncation: incorrect date value:

NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线

GoLand 2021.1: rename the go project

Go language unit test 5: go language uses go sqlmock and Gorm to do database query mock
![[bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update](/img/b8/31609303fd817c48b6fff7c43f31e5.png)
[bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update

Unable to stop it, domestic chips have made another breakthrough, and some links have reached 4nm

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

Dlopen() implements dynamic loading of third-party libraries
随机推荐
Go language web development series 29: Gin framework uses gin contrib / sessions library to manage sessions (based on cookies)
IBEM 数学公式检测数据集
pytorch 载入历史模型时更换gpu卡号,map_location设置
The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can
Uniapp tips - scrolling components
Ocean CMS vulnerability - search php
page owner特性浅析
Students who do not understand the code can also send their own token, which is easy to learn BSC
怎样删除对象的某个属性或⽅法
IBEM mathematical formula detection data set
Universal dividend source code, supports the dividend of any B on the BSC
Record 405 questions about bank callback post request
Dlopen() implements dynamic loading of third-party libraries
Another industry has been broken by Chinese chips. No wonder the leading analog chip companies in the United States have cut prices and sold off
Golang - command line tool Cobra
信创产业现状、分析与预测
Go 1.16.4: manage third-party libraries with Mod
记录关于银行回调post请求405 问题
Error running 'application' in idea running: the solution of command line is too long
Qt学习24 布局管理器(三)