当前位置:网站首页>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
边栏推荐
- Mysql:insert date:sql error [1292] [22001]: data truncation: incorrect date value:
- windos 创建cordova 提示 因为在此系统上禁止运行脚本
- Qt学习17 对话框及其类型
- jvm-对象生命周期
- 软件测试工作那么难找,只有外包offer,我该去么?
- Static linked list (subscript of array instead of pointer)
- 1px problem of mobile terminal
- The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can
- 可编程逻辑器件软件测试
- [bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update
猜你喜欢

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

Qt学习20 Qt 中的标准对话框(中)

Rasp implementation of PHP

Multi person collaborative data annotation based on Baidu brain easydata from scratch

Ocean CMS vulnerability - search php

Go language unit test 4: go language uses gomonkey to test functions or methods

Conversion function and explicit
![[技术发展-24]:现有物联网通信技术特点](/img/f3/a219fe8e7438b8974d2226b4c3d4a4.png)
[技术发展-24]:现有物联网通信技术特点

Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases

Richview trvstyle liststyle list style (bullet number)
随机推荐
Uniapp skills - scrolling components -1
Mysql:insert date:SQL 错误 [1292] [22001]: Data truncation: Incorrect date value:
There is nothing new under the sun. Can the meta universe go higher?
【吉林大学】考研初试复试资料分享
Software testing is so hard to find, only outsourcing offers, should I go?
3D vision - 2 Introduction to pose estimation - openpose includes installation, compilation and use (single frame, real-time video)
Conversion function and explicit
Unity render streaming communicates with unity through JS
如何使用lxml判断网站公告是否更新
Thrift threadmanager and three monitors
Unity Render Streaming通过Js与Unity自定义通讯
Qt学习19 Qt 中的标准对话框(上)
How to promote the progress of project collaboration | community essay solicitation
Using registered classes to realize specific type matching function template
JS continues to explore...
Solve MySQL 1045 access denied for user 'root' @ 'localhost' (using password: yes)
金属有机骨架MIL-88负载阿霉素DOX|叶酸修饰UiO-66-NH2负载阿霉素[email protected]纳米粒子
Implementation of Muduo accept connection, disconnection and sending data
MySQL 数据处理值增删改
page owner特性浅析