我正在尝试单击并拖动以放大QgraphicsView,就像您在Maya和Nuke这样的图形应用程序中看到的那样。有很多有关使用鼠标轮的信息,但我没有发现与拖动变焦有关的任何信息。
是否有一种简单的方法可以做到这一点,或者我需要自己对"锚"效果的实现?
以下内容将起作用,但是当我拖动变焦时,视图遵循鼠标,而不是出现在空间中的固定点(单击鼠标开始启动drag-Zoom)。p> (这是我更复杂的源代码中的一堆复制和粘贴。它的目的是说明性,尽管应该运行)
def mousePressEvent(self, event):
self.press_mouse_pos = event.pos()
transform = self.transform()
self.press_translate = [transform.m31(), transform.m32()]
self.press_scale = transform.m11()
if event.button() == QtCore.Qt.RightButton and
event.modifiers() == QtCore.Qt.AltModifier:
self.scaling = True
event.accept()
else:
super(GraphView, self).mousePressEvent(event)
def mouseMoveEvent(self, event):
if self.scaling:
delta_pos = event.pos() - self.press_mouse_pos
amount = delta_pos.x() + delta_pos.y()
speed = 0.001
scl = self.press_scale - (amount * speed)
scl = min(1.0, max(scl, 0.1)) # Clamp so we don't go to far in or out
transform = QtGui.QTransform(
scl, 0, 0,
0, scl, 0,
self.press_translate[0], self.press_translate[1], 1
)
# If interactive is True then some double calculations are triggered
prev_interactive_state = self.isInteractive()
prev_anchor_mode = self.transformationAnchor()
self.setInteractive(False)
self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
self.setTransform(transform)
self.setInteractive(prev_interactive_state)
self.setTransformationAnchor(prev_anchor_mode)
else:
super(GraphView, self).mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
self.scaling = False
super(GraphView, self).mouseReleaseEvent(event)
更改以下内容:
self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
:
self.setTransformationAnchor(QtGui.QGraphicsView.NoAnchor)
setTransformation()
是转换锚的主题;AnchorUnderMouse
确保场景点对应电流(在应用转换时)鼠标位置保持未翻译。