当前位置:网站首页>[pyqt5] Custom controls to achieve scaling sub-controls that maintain the aspect ratio
[pyqt5] Custom controls to achieve scaling sub-controls that maintain the aspect ratio
2022-08-01 18:32:00 【__Watson__】
需求
during window scaling,The control is scaled according to a certain aspect ratio
实现思路
1. 继承QFrame类, 重写resizeEvent方法
代码
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QFrame
class KeepRatioFrame(QFrame):
def __init__(self, parent, flags=QtCore.Qt.WindowType.Widget) -> None:
super().__init__(parent, flags)
self.resize_init = False
self.childWidget = None
self.wh_ratio = 1
def resizeEvent(self, event: QtGui.QResizeEvent) -> None:
if not self.resize_init:
self.resize_init = True
# 获取子控件
self.childWidget: QtWidgets.QLabel = self.findChild(QtWidgets.QLabel)
# 计算uiInitial aspect ratio
self.wh_ratio = self.childWidget.width() / self.childWidget.height()
# 获取当前frame的宽和长
frame_w, frame_h = event.size().width(), event.size().height()
# frame宽度不够 以此来计算child的高度
if frame_w / frame_h <= self.wh_ratio:
child_w, child_h = frame_w, round(frame_w / self.wh_ratio)
child_x, child_y = 0, round(frame_h/2 - child_h/2)
# frame高度不够 以此来计算child的宽度
else:
child_w, child_h = round(frame_h * self.wh_ratio), frame_h
child_x, child_y = round(frame_w/2 - child_w/2), 0
# Update child control position size
self.childWidget.setGeometry(child_x, child_y, child_w, child_h)
2. 在qt designer里设计ui时使用framePromoted to the above custom class
The control that will maintain the aspect ratio(The initial length and width need to be set)Put that custom oneframe中(Don't add layouts)
小节
QResizeEvent
incoming event,可以通过event访问resieze前后的widget的sizefindChild
Child controls can be found- Pay attention to the control
geometry属性的x,y
is relative to the parent control - Mobile controls are available
setGeometry(x, y, w, h)
或者move(x, y)和resize(w, h)
- 关于控件的geometry
包含Window Title
的
widget.x()、widget.y()
widget.pos().x()
、widget.pos().y()
widget.frameGeometry().width()
、widget.frameGeometry().height()
不包含Window Title
的(Client Area)
widget.geometry()
、
-widget.geometry().x()
、widget.geometry().y()
、widget.geometry().width()
、widget.geometry().height()
widget.width()
、widget.height()
边栏推荐
- MySQL database - stored procedures and functions
- 关于单应性矩阵的若干思考
- MySQL 45 Talk | 09 How to choose common index and unique index?
- 如何让固定点的监控设备在EasyCVR平台GIS电子地图上显示地理位置?
- 国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现
- Solve the problem that MySQL cannot insert Chinese data
- QT基础功能,信号、槽
- 暑假第二周总结博客
- Break the performance ceiling!AsiaInfo database supports more than 1 billion users, with a peak of one million transactions per second
- The life cycle and scope
猜你喜欢
【Day_10 0428】井字棋
Solve the problem that MySQL cannot insert Chinese data
Prometheus's Recording rules practice
No need to crack, install Visual Studio 2013 Community Edition on the official website
explain 各字段介绍
电商库存系统的防超卖和高并发扣减方案
How to use the Golang coroutine scheduler scheduler
7月30号|来一场手把手助您打造智能视觉新爆款的技术动手实验
深入浅出Flask PIN
Leetcode74. 搜索二维矩阵
随机推荐
用VS2013编译带boost库程序时提示 fatal error C1001: 编译器中发生内部错误
B005 - STC8 based single chip microcomputer intelligent street light control system
Basic image processing in opencv
Break the performance ceiling!AsiaInfo database supports more than 1 billion users, with a peak of one million transactions per second
【Translation】OpenMetrics cultivated by CNCF becomes an incubation project
关于单应性矩阵的若干思考
【Error】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat’)
Map by value
WinRAR | Generate multiple installers into one installer
日志工厂(详细)
AntDB database appeared in the 24th high-speed exhibition, helping smart high-speed innovative applications
顺序表的简单描述及代码的简单实现
LeetCode 0151.颠倒字符串中的单词
云原生全景图详解
Leetcode71. 简化路径
三种方案解决:npm WARN config global --global, --local are deprecated. Use --location=global instead.
COS 用户实践征文
如何让固定点的监控设备在EasyCVR平台GIS电子地图上显示地理位置?
A simple Flask PIN
ACID Characteristics and Implementation Methods of MySQL Relational Database Transactions