当前位置:网站首页>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
边栏推荐
- Install JMeter
- 4*4图片权重的收敛规则
- Share the typora tool
- 容错、熔断的使用与扩展
- 栈顶与栈底
- Chapter 1 Introduction
- Hash - 1. Sum of two numbers - some people fall in love during the day, some people watch the sea at night, and some people can't do the first question
- Detailed explanation of stat function
- In kuborad graphical interface, operate kubernetes cluster to realize master-slave replication in MySQL
- PM's alarm: "NPM warn config global --global, --local are deprecated
猜你喜欢

Shell script "< < EOF" my purpose and problems

三、MFC消息映射机制实现原理

Leetcode:51. queen n

Convergence rules for 4 * 4 image weights

Install JMeter

生信周刊第37期

链表——剑指offer面试题 02.07. 链表相交

leetcode:51. N 皇后

What is prescaler in STM32

6k+ star, a deep learning code base for Xiaobai! One line of code implements all attention mechanisms!
随机推荐
L1-064 估值一亿的AI核心代码
Mysql database
Collision, removal and cleaning
4*4图片权重的收敛规则
Agile? DevOps ?
Cgo+gsoap+onvif learning summary: 9. Go and C conduct socket communication and onvif protocol processing
Is there any charge for PDF processing? impossible!
MySql的DDL和DML和DQL的基本语法
在kuborad图形化界面中,操作Kubernetes 集群,实现mysql中的主从复制
链表——剑指offer面试题 02.07. 链表相交
Differences between JS map and foreach
String - 541. Reverse string II
Skillfully using command line parameters in Delphi to realize the trigger function of dragging files onto program icons
Use and expansion of fault tolerance and fusing
Hash - 242. valid alphabetic ectopic words
哈希——202. 快乐数
Dry goods sharing - taking over a new data team as a lead - Problem Inventory and insights findings
Blue team resource collection
Svn server and client installation (Chinese package) and simple use
一周精彩内容分享(第13期)