放大鼠标位置 QGraphicsView
Zoom on mouse position QGraphicsView
我正在寻找如何在 QGraphicsView 中放大光标位置。目前我可以缩放,但它缩放到的位置不一致。
def wheelEvent(self, event):
'''Wheel event to zoom
'''
# Run default event
QtWidgets.QGraphicsView.wheelEvent(self, event)
# Define zoom factor
factor = 1.1
if event.delta() < 0:
factor = 0.9
self.scale(factor, factor)
我看过self.mapToScene()
的用法,但是没有成功
一个可能的解决方案是关注鼠标所在的点,缩放并重新计算新中心应该在的点:
def wheelEvent(self, event):
factor = 1.1
if event.delta() < 0:
factor = 0.9
view_pos = event.pos()
scene_pos = self.mapToScene(view_pos)
self.centerOn(scene_pos)
self.scale(factor, factor)
delta = self.mapToScene(view_pos) - self.mapToScene(self.viewport().rect().center())
self.centerOn(scene_pos - delta)
我正在寻找如何在 QGraphicsView 中放大光标位置。目前我可以缩放,但它缩放到的位置不一致。
def wheelEvent(self, event):
'''Wheel event to zoom
'''
# Run default event
QtWidgets.QGraphicsView.wheelEvent(self, event)
# Define zoom factor
factor = 1.1
if event.delta() < 0:
factor = 0.9
self.scale(factor, factor)
我看过self.mapToScene()
的用法,但是没有成功
一个可能的解决方案是关注鼠标所在的点,缩放并重新计算新中心应该在的点:
def wheelEvent(self, event):
factor = 1.1
if event.delta() < 0:
factor = 0.9
view_pos = event.pos()
scene_pos = self.mapToScene(view_pos)
self.centerOn(scene_pos)
self.scale(factor, factor)
delta = self.mapToScene(view_pos) - self.mapToScene(self.viewport().rect().center())
self.centerOn(scene_pos - delta)