一、注意Item、Scene、View各自含义
在对图片进行缩放的时候,注意缩放的应该是Item,是图元,而不是Scene或者View。
所以当为了满足以鼠标为中心放大,而需要进行平移时,移动的是图元,图元才是可移动的。同时需要注意moveBy函数的含义,它移动的距离指的是相对item左上角移动距离,原始值为(0,0),向上向右移动为负数。
二、下面举例代码
创建了QgraphicsPixmapItemNew类,继承自QgraphicsPixmapItem,重构了鼠标滚动事件,完整代码如下
from PyQt5.QtWidgets import QGraphicsPixmapItem
class QGraphicsPixmapItemNew(QGraphicsPixmapItem):
def get_zoomscale(self, zoom_scale):
self.zoom_scale = zoom_scale
self.scale_vale = zoom_scale
def wheelEvent(self, event: 'QGraphicsSceneWheelEvent'):
if event.delta() > 0 and self.scale_vale >= 50: # 只能放大50倍
return
if event.delta() < 0 and self.scale_vale <= self.zoom_scale:
self.scale_vale = self.zoom_scale
else:
scale = sel

本文介绍了使用PyQT5进行图像处理时,如何实现以鼠标为中心对图片进行缩放。重点在于理解Item、Scene和View的区别,以及在缩放过程中移动图元的重要性。给出了一个自定义的QgraphicsPixmapItemNew类,该类重构了鼠标滚动事件以实现所需功能,但详细的代码分析尚未提供。
最低0.47元/天 解锁文章
5188





