pyQt4 QGraphicsView需要鼠标事件帮助

arc*_*ne9 5 python qt pyqt

我已经做了相当多的搜索这个问题,这可能是微不足道的.但是我是pyQT的新手并且完全卡住了.任何帮助,将不胜感激.

我只想使用QGraphicsScene在QGraphicsView小部件上放置,移动和绘制对象.以下用于处理鼠标按下事件的代码可以正常工作,但是当在表单中的任何地方单击鼠标时它会触发,而不仅仅是在QGraphicViewer中(此外,对象随后被放置在错误的位置).这是我现在使用的代码的摘录

def mousePressEvent(self, ev): #QGraphicsSceneMouseEvent
    if ev.button()==Qt.LeftButton:
        item = QGraphicsTextItem("CLICK")
        item.setPos(ev.x(), ev.y())
        #item.setPos(ev.scenePos())
        self.scene.addItem(item)
Run Code Online (Sandbox Code Playgroud)

我知道我应该使用QGraphicsSceneMouseEvent,我可以看到它是如何在C++中实现的; 但我不知道如何在Python中使用它.

谢谢

Ery*_*Sun 6

尝试扩展QtGui.QGraphicsScene并使用其mousePressEvent和scenePos()中的坐标.就像是:

class QScene(QtGui.QGraphicsScene):
    def __init__(self, *args, **kwds):
        QtGui.QGraphicsScene.__init__(self, *args, **kwds)

    def mousePressEvent(self, ev):
        if ev.button() == QtCore.Qt.LeftButton:
            item = QtGui.QGraphicsTextItem("CLICK")
            item.setPos(ev.scenePos())
            self.addItem(item)
Run Code Online (Sandbox Code Playgroud)