当前位置:网站首页>[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()
边栏推荐
- 【Day_09 0427】 另类加法
- LeetCode 0152. 乘积最大子数组:dp + 原地滚动
- Leetcode72. 编辑距离
- 7月30号|来一场手把手助您打造智能视觉新爆款的技术动手实验
- How to build a CMDB driven by consumption scenarios?
- CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) Solution
- 基于flowable的upp(统一流程平台)运行性能优化
- WinRAR | 将多个安装程序生成一个安装程序
- 【Day_11 0506】 最近公共祖先
- How many steps does it take to convert an ENS domain name into music?
猜你喜欢
随机推荐
LeetCode 0152. Product Maximum Subarray: dp + Roll in Place
golang json returns null
Leetcode73. 矩阵置零
【神经网络】一文带你轻松解析神经网络(附实例恶搞女友)
Map by value
【翻译】CNCF培养的OpenMetrics成为一个孵化项目
QT basic functions, signals, slots
B005 - STC8 based single chip microcomputer intelligent street light control system
Leetcode75. Color Classification
The elder brother of the goldfish RHCA memoirs: CL210 experiment management it network - chapter
The XML configuration
顺序表的简单描述及代码的简单实现
2022,程序员应该如何找工作
How to solve the dynamic binding of el-form-item prop attribute does not take effect
The life cycle and scope
突破性能天花板!亚信数据库支撑 10 多亿用户,峰值每秒百万交易
【Day_08 0426】两种排序方法
公用函数----mfc
关于单应性矩阵的若干思考
打开微信客服