当前位置:网站首页>[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)
小节
QResizeEventincoming event,可以通过event访问resieze前后的widget的size
findChildChild controls can be found- Pay attention to the control
geometry属性的x,yis 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()
边栏推荐
- 基于flowable的upp(统一流程平台)运行性能优化
- How many steps does it take to convert an ENS domain name into music?
- explain 各字段介绍
- 123123123123
- Friends who want to use the database anytime, anywhere and as you like, all attention!
- QLineEdit学习与使用
- explain each field introduction
- The XML configuration
- SQL函数 TO_DATE(二)
- 【无标题】setInterval和setTimeout详解
猜你喜欢

What is the JVM runtime data area and the JMM memory model

How many steps does it take to convert an ENS domain name into music?

B005 - STC8 based single chip microcomputer intelligent street light control system

No need to crack, install Visual Studio 2013 Community Edition on the official website

将ENS域名转化为音乐需要几步?

C#/VB.NET:从 PDF 文档中提取所有表格

在Map传值与对象传值中模糊查询

LeetCode 0152. 乘积最大子数组:dp + 原地滚动

OpenCV installation, QT, VS configuration project settings

Leetcode72. Edit Distance
随机推荐
B011 - 51-based multifunctional fingerprint smart lock
Break the performance ceiling!AsiaInfo database supports more than 1 billion users, with a peak of one million transactions per second
483-82 (23, 239, 450, 113)
[National Programming] "Software Programming - Lecture Video" [Zero Basic Introduction to Practical Application]
【Day_09 0427】 另类加法
How to make the fixed-point monitoring equipment display the geographic location on the EasyCVR platform GIS electronic map?
将ENS域名转化为音乐需要几步?
483-82(23、239、450、113)
SQL函数 TO_DATE(一)
Live chat system technology (8) : vivo live IM message module architecture practice in the system
golang json returns null
How to solve the dynamic binding of el-form-item prop attribute does not take effect
OpenCV安装、QT、VS配置项目设置
金鱼哥RHCA回忆录:CL210管理OPENSTACK网络--网络配置选项
B001 - Intelligent ecological fish tank based on STM32
C language theory--a solid foundation for the written test and interview
Goldfish Brother RHCA Memoirs: CL210 manages OPENSTACK network -- network configuration options
A simple Flask PIN
顺序表的简单描述及代码的简单实现
【神经网络】一文带你轻松解析神经网络(附实例恶搞女友)