如何在Qt中处理鼠标事件?

时间:2013-04-25 18:14:36

标签: qt event-handling pyqt pyside

在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_())

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

1 个答案:

答案 0 :(得分: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

我希望这就是你要找的东西。