0

Qt 场景中的拖动、平移和鼠标跟踪项目是我想在我的应用程序中同时使用的 3 个操作。然而,这比我想象的要难。

示例代码:

from PySide.QtCore import *
from PySide.QtGui import *

class View(QGraphicsView):
    """# HOW TO ATTACH MOUSE EVENTS PROGRAMMATICALLY ?
    def mouseMoveEvent(self, event):
        print "View MOVE", event.pos()
    """
class Scene(QGraphicsScene):
    pass

class CircleForTrackingSwitch(QGraphicsEllipseItem):
    def __init__(self, scene):
        super(CircleForTrackingSwitch, self).__init__()
        self.scene = scene
        self.view = self.scene.views()[0]
        self.setRect(QRect(20,20,20,20))
        self.scene.addItem(self)

    def mousePressEvent(self, event):
        if self.view.hasMouseTracking() :
            self.view.setMouseTracking(False)
        else :
            self.view.setMouseTracking(True)
        print "Circle View SetTrack", event.pos()

    def mouseMoveEvent(self, event):
        print "Circle MOVE", event.pos()

class DraggableRectangle(QGraphicsRectItem):
    def __init__(self, scene):
        super(DraggableRectangle, self).__init__()
        self.scene = scene
        self.setRect(QRect(-20,-20,40,40))
        self.scene.addItem(self)
        #self.setFlag(QGraphicsItem.ItemIsMovable, True)

    def mousePressEvent(self, event):
        print "Rectangle PRESS", event.pos()

    def mouseMoveEvent(self, event):
        print "Rectangle MOVE", event.pos()
        self.setPos(event.pos())

    def mouseReleaseEvent(self, event):
        print "Rectangle RELEASE", event.pos()

class Window(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.s = Scene()
        self.s.setSceneRect(-200,-100,300,300,)

        self.v = View(self.s)
        self.v.setDragMode(QGraphicsView.ScrollHandDrag)
        self.setCentralWidget(self.v)

        CircleForTrackingSwitch(self.s)
        DraggableRectangle(self.s)

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    window = Window()
    window.resize(300, 200)
    window.show()
    sys.exit(app.exec_())

主问题:如何以编程方式附加鼠标事件?非常感谢。

4

1 回答 1

1

对于可拖动的 Rectangle,由于 Rectangle 已经是可移动的,因此您只需对其进行子类化,以重载不同的鼠标功能(使用轨道的东西),然后调用父级的鼠标事件。基本上,你可以用这个矩形类得到你想要的:

class DraggableRectangle(QGraphicsRectItem):
    def __init__(self, scene, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.scene = scene
        self.setRect(QRect(-20,-20,40,40))
        self.scene.addItem(self)
        self.setFlag(QGraphicsItem.ItemIsMovable, True) # Keep that

    def mousePressEvent(self, event):
        print "Rectangle PRESS", event.pos()
        super().mousePressEvent(event) # Call parent

    def mouseMoveEvent(self, event):
        print "Rectangle MOVE", event.pos()
        # Do not move by yourself
        # Call parent that already handles the move
        super().mouseMoveEvent(event)

    def mouseReleaseEvent(self, event):
        print "Rectangle RELEASE", event.pos()
        super().mouseReleaseEvent(event) # Call parent

我希望这就是你要找的。

于 2013-04-26T18:13:15.120 回答