本文介绍了MousePressEvent,QGraphicsView 中的位置偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 QGraphicsViewQGraphicsScene 时遇到了一些困难.当我在场景中缩放/取消缩放并使用 mousePressEvent 创建项目时,我的位置有一个偏移量.如何避免这种情况?

I've some difficulties with QGraphicsView and QGraphicsScene.When I zoom/unzoom in the scene and create items with mousePressEvent, I have an offset in the position. How can this be avoided?

event.pos() 似乎是问题..

from PyQt4 import QtCore, QtGui

class graphicsItem (QtGui.QGraphicsItem):
    def __init__ (self):
        super(graphicsItem, self).__init__()
        self.rectF = QtCore.QRectF(0,0,10,10)
    def boundingRect (self):
        return self.rectF
    def paint (self, painter=None, style=None, widget=None):
        painter.fillRect(self.rectF, QtCore.Qt.red)

class graphicsScene (QtGui.QGraphicsScene):
    def __init__ (self, parent=None):
        super (graphicsScene, self).__init__ (parent)

class graphicsView (QtGui.QGraphicsView):
    def __init__ (self, parent = None):
        super (graphicsView, self).__init__ (parent)
        self.parent = parent
    def mousePressEvent(self, event):
        super (graphicsView, self).mousePressEvent(event)
        item = graphicsItem()
        position = QtCore.QPointF(event.pos()) - item.rectF.center()
        item.setPos(position.x() , position.y())
        self.parent.scene.addItem(item)
    def wheelEvent (self, event):
        super (graphicsView, self).wheelEvent(event)
        factor = 1.2
        if event.delta() < 0 :
            factor = 1.0 / factor
        self.scale(factor, factor)

class window (QtGui.QMainWindow):
    def __init__ (self, parent = None ) :
        super (window, self).__init__(parent)
        self.width = 800
        self.height = 600

        self.resize(self.width,self.height)
        self.mainLayout = QtGui.QVBoxLayout(self)

        self.view = graphicsView(self)
        self.scene = graphicsScene(self)
        self.view.setScene (self.scene)

        factor = 1
        self.scene.setSceneRect(0, 0, self.width * factor, self.height * factor)
        self.view.setMinimumSize(self.width, self.height)

        self.mainLayout.addWidget(self.view)

    def show (self):
        super (window, self).show()

推荐答案

在场景而不是视图上重新实现 mousePressEvent.

Reimplement mousePressEvent on the scene, rather than the view.

这样,event 参数将是一个 QGraphicsSceneMouseEvent,它有几个有用的附加功能 - 包括 scenePos,这正是你想要的:

That way, the event argument will be a QGraphicsSceneMouseEvent, which has several useful additional functions - including scenePos, which does exactly what you want:

class graphicsScene(QtGui.QGraphicsScene):
    def __init__ (self, parent=None):
        super(graphicsScene, self).__init__ (parent)

    def mousePressEvent(self, event):
        super(graphicsScene, self).mousePressEvent(event)
        item = graphicsItem()
        position = QtCore.QPointF(event.scenePos()) - item.rectF.center()
        item.setPos(position.x() , position.y())
        self.addItem(item)

这篇关于MousePressEvent,QGraphicsView 中的位置偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-25 16:34