当前位置:网站首页>QT learning 26 integrated example of layout management
QT learning 26 integrated example of layout management
2022-07-07 07:59:00 【A little black sauce】
Qt Study 26 Comprehensive example of layout management
Demand analysis
- Practice developing a wizard user interface
- Show on the same interface Different wizard pages
- adopt “ The previous step ” and “ next step ” Button to switch
- The element components and component layout on different pages are different
- The components in the page are arranged through the layout manager
Solution
- Interface design through layout nesting
- adopt QStackedLayout Manage different pages
- Generate different pages through sub components
Programming experiment - Wizard user interface
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QStackedLayout>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
class Widget : public QWidget
{
Q_OBJECT
private:
QStackedLayout sLayout;
QPushButton preBtn;
QPushButton nextBtn;
QLabel fLabel1;
QLabel fLabel2;
QLabel fLabel3;
QLabel fLabel4;
QLineEdit sEdit;
QPushButton tBtn1;
QPushButton tBtn2;
void initControl();
QWidget* get1stPage();
QWidget* get2ndPage();
QWidget* get3rdPage();
private slots:
void onPreBtnClicked();
void onNextBtnClicked();
public:
Widget(QWidget *parent = 0);
~Widget();
};
#endif // WIDGET_H
#include "Widget.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QFormLayout>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
initControl();
}
Widget::~Widget()
{
}
void Widget::initControl()
{
QVBoxLayout* vLayout = new QVBoxLayout();
QHBoxLayout* hLayout = new QHBoxLayout();
vLayout->addLayout(&sLayout);
vLayout->addLayout(hLayout);
preBtn.setText("pre page");
preBtn.setMinimumSize(160, 30);
nextBtn.setText("next page");
nextBtn.setMinimumSize(160, 30);
connect(&preBtn, SIGNAL(clicked()), this, SLOT(onPreBtnClicked()));
connect(&nextBtn, SIGNAL(clicked()), this, SLOT(onNextBtnClicked()));
hLayout->addWidget(&preBtn);
hLayout->addWidget(&nextBtn);
sLayout.addWidget(get1stPage());
sLayout.addWidget(get2ndPage());
sLayout.addWidget(get3rdPage());
setLayout(vLayout);
}
QWidget *Widget::get1stPage()
{
QWidget* ret = new QWidget();
QGridLayout* gLayout = new QGridLayout();
fLabel1.setText("This");
fLabel2.setText("is");
fLabel3.setText("1st");
fLabel4.setText("page");
gLayout->addWidget(&fLabel1, 0, 0);
gLayout->addWidget(&fLabel2, 0, 1);
gLayout->addWidget(&fLabel3, 1, 0);
gLayout->addWidget(&fLabel4, 1, 1);
ret->setLayout(gLayout);
return ret;
}
QWidget *Widget::get2ndPage()
{
QWidget* ret = new QWidget();
QFormLayout* fLayout = new QFormLayout();
sEdit.setText("This is 2nd page");
fLayout->addRow("Hint:", &sEdit);
ret->setLayout(fLayout);
return ret;
}
QWidget *Widget::get3rdPage()
{
QWidget* ret = new QWidget();
QVBoxLayout* vLayout = new QVBoxLayout();
tBtn1.setText("This is");
tBtn2.setText("3rd page");
vLayout->addWidget(&tBtn1);
vLayout->addWidget(&tBtn2);
ret->setLayout(vLayout);
return ret;
}
void Widget::onPreBtnClicked()
{
int index = ((sLayout.currentIndex() - 1) + 3) % 3;
sLayout.setCurrentIndex(index);
}
void Widget::onNextBtnClicked()
{
int index = (sLayout.currentIndex() + 1) % 3;
sLayout.setCurrentIndex(index);
}
#include "Widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
matters needing attention
- Container of any component You can specify the layout manager
- Components in the same layout manager have the same parent component
- While setting layout management Implicit designated parent-child relationship
- In the figure Components 1 and Components 2 Managed by the same layout manager , Have the same parent component .
Summary
- The layout manager can Nested with each other to form a complex user interface
- Any container component can set the layout manager
- Components in the same layout manager have the same parent component
- The parent-child relationship between components is Qt An important way of memory management in
边栏推荐
- json 数据展平pd.json_normalize
- 有 Docker 谁还在自己本地安装 Mysql ?
- Mysql高低版本切换需要修改的配置5-8(此处以aicode为例)
- Value sequence (subsequence contribution problem)
- CTF daily question day43 rsa5
- Cnopendata list data of Chinese colleges and Universities
- Ansible
- C语言通信行程卡后台系统
- pytest+allure+jenkins安装问题:pytest: error: unrecognized arguments: --alluredir
- Numbers that appear only once
猜你喜欢
[2022 ciscn] replay of preliminary web topics
探索干货篇!Apifox 建设思路
[experience sharing] how to expand the cloud service icon for Visio
[CV] Wu Enda machine learning course notes | Chapter 8
即刻报名|飞桨黑客马拉松第三期等你挑战
Operation suggestions for today's spot Silver
[GUET-CTF2019]虚假的压缩包
【数字IC验证快速入门】17、SystemVerilog学习之基本语法4(随机化Randomization)
3D reconstruction - stereo correction
buuctf misc USB
随机推荐
[webrtc] M98 screen and window acquisition
LeetCode 40:组合总和 II
Is the test cycle compressed? Teach you 9 ways to deal with it
Zhilian + AV, AITO asked M7 to do more than ideal one
Open source ecosystem | create a vibrant open source community and jointly build a new open source ecosystem!
The principle and implementation of buffer playback of large video files
Pytest+allure+jenkins installation problem: pytest: error: unrecognized arguments: --alluredir
[CV] Wu Enda machine learning course notes | Chapter 8
大视频文件的缓冲播放原理以及实现
Hands on deep learning (IV) -- convolutional neural network CNN
Linux server development, redis source code storage principle and data model
Linux server development, MySQL stored procedures, functions and triggers
leetcode:105. Constructing binary trees from preorder and inorder traversal sequences
Detailed explanation of uboot image generation process of Hisilicon chip (hi3516dv300)
misc ez_usb
[UVM practice] Chapter 1: configuring the UVM environment (taking VCs as an example), run through the examples in the book
PHP exports millions of data
【p2p】本地抓包
[UTCTF2020]file header
[advanced digital IC Verification] command query method and common command interpretation of VCs tool