在 GraphicsView/GraphicsScene 中放大光标

Zooming into cursor in GraphicsView/GraphicsScene

我有一个 QGraphicsView 小部件,我将其用作绘图 canvas。

我希望能够放大 ctrl+mwheel 上的鼠标光标位置。就我的代码而言,它只滚动到 canvas 的中心,而不是光标位置。貌似翻译逻辑不对,但是没看出来:

def zoomCanvas(self, event):
    zoomInFactor = 1.25
    zoomOutFactor = 1 / zoomInFactor
    oldPos = event.scenePos()
    if event.delta() > 0:
        zoomFactor = zoomInFactor
    else:
        zoomFactor = zoomOutFactor
    self.graphicsView.scale(zoomFactor, zoomFactor)
    newPos = event.scenePos()
    delta = newPos - oldPos
    self.graphicsView.translate(delta.x(), delta.y())

将这 2 个添加到 graphicsView init

    self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
    self.setResizeAnchor(QGraphicsView.AnchorUnderMouse)