当前位置:网站首页>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
边栏推荐
猜你喜欢

Introduction to Devops and common Devops tools

在kuborad图形化界面中,操作Kubernetes 集群,实现mysql中的主从复制

Share the typora tool

Three small knowledge points about data product managers

JMeter while controller

1184. 公交站间的距离 : 简单模拟题

微信公众号开发:素材管理(临时、永久)

The art of management - driving software R & D efficiency through leadership

One week's wonderful content sharing (issue 13)

One of his birds sold for 60million -- the collection of eight mountain people in the Ming and Qing Dynasties
随机推荐
[Commons beanautils topic] 005- convertutils topic
Learning materials about red team
How to use a third party without obtaining root permission topic: MIUI chapter
L1-064 估值一亿的AI核心代码
Install MariaDB columnstore (version 10.3)
A* and JPS
Record a garbage collection and analysis of gceasy
MySQL advanced (XVII) cannot connect to database server problem analysis
The art of management - driving software R & D efficiency through leadership
【我也想刷穿 LeetCode啊】468. 验证IP地址
生信周刊第37期
Chapter 0 Introduction and environment configuration
Experience of redis deepwater area -- Interview reference
Share the typora tool
3、 Implementation principle of MFC message mapping mechanism
6k+ star, a deep learning code base for Xiaobai! One line of code implements all attention mechanisms!
Detailed explanation of stat function
Import the data in MariaDB into columnstore
L1-064 AI core code valued at 100 million
Cgo+gsoap+onvif learning summary: 9. Go and C conduct socket communication and onvif protocol processing