当前位置:网站首页>QT notes - realize form adaptation
QT notes - realize form adaptation
2022-07-24 12:04:00 【Cool breeze in the old street °】
We want all controls to change with the size of the window .
The first method :
Ideas :
1. We need to get all the controls
m_Widget = this->findChildren<QWidget*>(QString(), Qt::FindDirectChildrenOnly);
2. We need to know the location and size of all controls
3. rewrite resizeEvent event
protected:
void resizeEvent(QResizeEvent* event);
4. Get the zoom ratio of the main window
5. Let each control * The zoom ratio
#include "ui_QtEventFilter.h"
#include <QResizeEvent>
#include <QRect>
class QtEventFilter : public QWidget
{
Q_OBJECT
public:
QtEventFilter(QWidget *parent = Q_NULLPTR);
~QtEventFilter();
protected:
void resizeEvent(QResizeEvent* event);
private:
Ui::QtEventFilterClass ui;
QList<QWidget*> m_Widget; // Store all child controls
QMap<QWidget*, QRect> m_WidgetRect; // Save the initial size of each child control
};
#include "QtEventFilter.h"
#include <QDebug>
QtEventFilter::QtEventFilter(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
// Get all the controls
m_Widget = this->findChildren<QWidget*>(QString(), Qt::FindDirectChildrenOnly);
// Traverse the control to get the size and position
foreach(auto widget, m_Widget)
{
m_WidgetRect.insert(widget, QRect(widget->x(), widget->y(), widget->width(), widget->height()));
}
}
QtEventFilter::~QtEventFilter()
{
}
void QtEventFilter::resizeEvent(QResizeEvent* event)
{
float width = this->width() * 1./ 600;
float height = this->height() * 1./400;
for ( auto it= m_WidgetRect.begin(); it != m_WidgetRect.end(); it++ )
{
it.key()->setGeometry(it.value().x() * width, it.value().y() * height, it.value().width() * width, it.value().height() * height);
}
QWidget::resizeEvent(event);
}
The second method :
Find the current UI file , Click a button in the picture , One is horizontal layout , A vertical layout , This can also change with the size of the window 
Reference blog :
https://blog.csdn.net/hua12134/article/details/84888250
边栏推荐
- CCF 1-2 question answering record (1)
- Cgo+gsoap+onvif learning summary: 9. Go and C conduct socket communication and onvif protocol processing
- An analysis of the CPU surge of an RFID tag management system in.Net
- Shengxin weekly issue 37
- Markdown mathematical formula syntax
- 使用Prometheus+Grafana实时监控服务器性能
- 1184. Distance between bus stops: simple simulation problem
- Day5: construct program logic
- Install MariaDB columnstore (version 10.3)
- 1184. 公交站间的距离 : 简单模拟题
猜你喜欢

Source code analysis sentry user behavior record implementation process

String -- 344. Reverse string

1184. Distance between bus stops: simple simulation problem

基于ARM和FPGA的数字示波器设计——QMJ

Chapter 1 Introduction
![MOS tube - Notes on rapid recovery application (I) [principle]](/img/a1/8427c9b1d0ea0cecce820816510045.png)
MOS tube - Notes on rapid recovery application (I) [principle]
![[markdown grammar advanced] make your blog more exciting (IV: set font style and color comparison table)](/img/a5/c92e0404c6a970a62595bc7a3b68cd.gif)
[markdown grammar advanced] make your blog more exciting (IV: set font style and color comparison table)

Types and history of bugs in it circle

Use prometheus+grafana to monitor server performance in real time

6k+ star, a deep learning code base for Xiaobai! One line of code implements all attention mechanisms!
随机推荐
链表——142. 环形链表 II
QT | summary of the use of edit box
安装jmeter
[mathematical basis of Cyberspace Security Chapter 9] finite field
哈希——18. 四数之和
哈希——242.有效的字母异位词
MySQL advanced (XVII) cannot connect to database server problem analysis
08.01 adjacency matrix
String - Sword finger offer 05. replace spaces
一文看懂MES系统能实现企业哪些目标
Share the typora tool
Remember to optimize my personal blog once
L1-059 敲笨钟
JVM visualvm: multi hop fault handling tool
Blue team resource collection
Common shortcuts to VIM editor
理解数据的存与取
VMware virtual machine and vSphere migrate to each other
Optimization method of "great mathematics for use" -- optimal design of Cascade Reservoir Irrigation
1184. Distance between bus stops: simple simulation problem