当前位置:网站首页>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
边栏推荐
- Guys, do you need to configure anything to use rocksdb when using flinksql? Or do you need any jar packages
- 一周精彩内容分享(第13期)
- L1-059 敲笨钟
- L1-043 reading room
- Skillfully using command line parameters in Delphi to realize the trigger function of dragging files onto program icons
- Linked list - 142. Ring linked list II
- [TA frost wolf umay - "hundred people plan] Figure 3.3 surface subdivision and geometric shader large-scale grass rendering
- Please ask whether Oracle CDC does not support checkpointing. When the task is suspended and restarted during the real-time collection process, is the data changed
- 【C和指针第11章】动态内存分配
- Common formulas and application scenarios of discrete distribution
猜你喜欢
![Operational amplifier - Notes on rapid recovery [II] (application)](/img/fd/e12f43e23e6ec76c2b44ce7813e204.png)
Operational amplifier - Notes on rapid recovery [II] (application)

Three small knowledge points about data product managers

One of his birds sold for 60million -- the collection of eight mountain people in the Ming and Qing Dynasties

字符串——344.反转字符串

【网络空间安全数学基础第9章】有限域

Linked list - Sword finger offer interview question 02.07. linked list intersection

链表——142. 环形链表 II

Dynamic memory management

[TA frost wolf umay - "hundred people plan] Figure 3.3 surface subdivision and geometric shader large-scale grass rendering

生信周刊第37期
随机推荐
The difference between where and having
[C and pointer Chapter 11] dynamic memory allocation
Using huggingface model to translate English
Microsoft SQL Server database language and function usage (XII)
【C和指针第14章】预处理器
Please ask whether Oracle CDC does not support checkpointing. When the task is suspended and restarted during the real-time collection process, is the data changed
Basic syntax of MySQL DDL and DML and DQL
Top and bottom of stack
三、MFC消息映射机制实现原理
String - Sword finger offer 05. replace spaces
Convergence rules for 4 * 4 image weights
Recommended SSH cross platform terminal tool tabby
JMeter while controller
Detailed explanation of stat function
Hash - 18. Sum of four numbers
离散分布常用公式及应用场景
MOS tube - Notes on rapid recovery application (I) [principle]
[I also want to brush through leetcode] 468. Verify the IP address
leetcode:51. N 皇后
Nacos permissions and databases