当前位置:网站首页>【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()
边栏推荐
- QPalette调色板、框架色彩填充
- LeetCode 0152. Product Maximum Subarray: dp + Roll in Place
- Live chat system technology (8) : vivo live IM message module architecture practice in the system
- Leetcode73. Matrix Zeroing
- MySQL database - stored procedures and functions
- The elder brother of the goldfish RHCA memoirs: CL210 experiment management it network - chapter
- 1065 A+B and C (64bit)
- odoo 编码规范(编程规范、编码指南)
- 成都理工大学&电子科技大学|用于强化学习的域自适应状态表示对齐
- opencv语法Mat类型总结
猜你喜欢

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

【Day_12 0507】二进制插入

OpenCV installation, QT, VS configuration project settings

【Day_11 0506】 最近公共祖先

QT_QThread thread

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

B011 - 51-based multifunctional fingerprint smart lock

Flowable-based upp (unified process platform) running performance optimization

【Day_09 0427】走方格的方案数

opencv如何实现图像倾斜校正
随机推荐
Basic image processing in opencv
Golang协程调度器scheduler怎么使用
Leetcode75. 颜色分类
亚马逊云科技Build On2022技能提升计划第二季——揭秘出海爆款新物种背后的黑科技
【Day_11 0506】求最大连续bit数
C#/VB.NET: extracted from the PDF document all form
B001 - Intelligent ecological fish tank based on STM32
Leetcode72. 编辑距离
【全民编程】《软件编程-讲课视频》【零基础入门到实战应用】
QT_QDialog 对话框
The XML configuration
成都理工大学&电子科技大学|用于强化学习的域自适应状态表示对齐
【Translation】OpenMetrics cultivated by CNCF becomes an incubation project
QPalette调色板、框架色彩填充
LeetCode 1374.生成每种字符都是奇数个的字符串
opencv语法Mat类型总结
Prometheus的Recording rules实践
LeetCode 1374. Generate an odd number of each character string
【Day_08 0426】两种排序方法
Leetcode73. 矩阵置零