当前位置:网站首页>【pyqt5】自定义控件 实现能够保持长宽比地缩放子控件
【pyqt5】自定义控件 实现能够保持长宽比地缩放子控件
2022-08-01 18:22:00 【__Watson__】
需求
在窗口缩放过程中,实现控件按照一定的长宽比缩放
实现思路
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)
# 计算ui初始宽长比
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
# 更新子控件位置尺寸
self.childWidget.setGeometry(child_x, child_y, child_w, child_h)
2. 在qt designer里设计ui时使用frame提升为上面自定义的类
将要保持长宽比的控件(需要设定初始的长宽)放入该自定义的frame中(不要添加布局)
小节
QResizeEvent事件的传入,可以通过event访问resieze前后的widget的size
findChild可以找到子控件- 注意控件的
geometry属性的x,y是相对于父级控件里的 - 移动控件可以用
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()
边栏推荐
- Industry Salon Phase II丨How to enable chemical companies to reduce costs and increase efficiency through supply chain digital business collaboration?
- 亚马逊云科技Build On2022技能提升计划第二季——揭秘出海爆款新物种背后的黑科技
- QT_事件类
- opencv基本的图像处理
- 金鱼哥RHCA回忆录:CL210管理OPENSTACK网络--章节实验
- OpenCV installation, QT, VS configuration project settings
- 加州大学|通过图抽象从不同的第三人称视频中进行逆强化学习
- C#/VB.NET:从 PDF 文档中提取所有表格
- explain 各字段介绍
- Break the performance ceiling!AsiaInfo database supports more than 1 billion users, with a peak of one million transactions per second
猜你喜欢

计算IoU(D2L)

JVM运行时数据区与JMM内存模型是什么

Leetcode74. 搜索二维矩阵

opencv如何实现图像倾斜校正

el-form-item prop属性动态绑定不生效如何解决

C language theory--a solid foundation for the written test and interview

How to use the Golang coroutine scheduler scheduler

Three solutions: npm WARN config global --global, --local are deprecated. Use --location=global instead.

MySQL数据库————存储过程和函数

483-82(23、239、450、113)
随机推荐
Flowable-based upp (unified process platform) running performance optimization
odoo 编码规范(编程规范、编码指南)
直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
When compiling a program with boost library with VS2013, it prompts fatal error C1001: An internal error occurred in the compiler
483-82(23、239、450、113)
亚马逊云科技Build On2022技能提升计划第二季——揭秘出海爆款新物种背后的黑科技
Summer vacation second week wrap-up blog
生命周期和作用域
QT常用全局宏定义
Solve the problem that MySQL cannot insert Chinese data
【Day_08 0426】两种排序方法
Clip-on multimeter use method, how to measure the voltage, current, resistance?
odoo+物联网
MySQL Lock wait timeout exceeded; try restarting transaction 锁等待
计算IoU(D2L)
2022,程序员应该如何找工作
解决MySQL插入不了中文数据问题
QPalette调色板、框架色彩填充
How to build a CMDB driven by consumption scenarios?
想随时、随地、随心使用数据库的朋友们,全体注意!