当前位置:网站首页>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
边栏推荐
- Comprehensively develop the main channel of digital economy and digital group, and actively promote the utonmos digital Tibet market
- Heap structure and heap sort heapify
- JS continues to explore...
- IBEM mathematical formula detection data set
- 【吉林大学】考研初试复试资料分享
- Qt学习19 Qt 中的标准对话框(上)
- SQL Injection (AJAX/JSON/jQuery)
- Implementation of Muduo accept connection, disconnection and sending data
- 全局事件总线
- Mastering the cypress command line options is the basis for truly mastering cypress
猜你喜欢
SQL Injection (POST/Search)
Qt学习24 布局管理器(三)
太阳底下无新事,元宇宙能否更上层楼?
Flutter dynamic | fair 2.5.0 new version features
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
jvm-对象生命周期
Mastering the cypress command line options is the basis for truly mastering cypress
SQL Injection (GET/Select)
Go language web development series 25: Gin framework: using MD5 to verify the signature for the interface station
Mysql:insert date:SQL 错误 [1292] [22001]: Data truncation: Incorrect date value:
随机推荐
[bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update
jvm-对象生命周期
软件测试工作那么难找,只有外包offer,我该去么?
“又土又穷”的草根高校,凭什么被称为“东北小清华”?
IBEM mathematical formula detection data set
Qt学习19 Qt 中的标准对话框(上)
SQL Injection (POST/Select)
Students who do not understand the code can also send their own token, which is easy to learn BSC
Windos creates Cordova prompt because running scripts is prohibited on this system
3D视觉——2.人体姿态估计(Pose Estimation)入门——OpenPose含安装、编译、使用(单帧、实时视频)
金属有机骨架材料ZIF-8包载姜黄素([email protected]纳米颗粒)|纳米金属有机框架搭载雷帕霉素|科研试剂
[acnoi2022] guess numbers
Thrift threadmanager and three monitors
JS continues to explore...
Go language web development series 26: Gin framework: demonstrates the execution sequence of code when there are multiple middleware
【BW16 应用篇】安信可BW16模组与开发板更新固件烧录说明
Comprehensive case of MySQL data addition, deletion, modification and query
page owner特性浅析
JS general form submission 1-onsubmit
SQL Injection (AJAX/JSON/jQuery)