2013-04-25 66 views
0

拖動Qt中的場景平移和鼠標跟蹤項目,我想在我的應用程序同時使用3個行動。但是,這比我想象的要困難得多。如何處理Qt中的鼠標事件?

示例代碼:

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

MASTER問題: 如何連接鼠標事件編程?非常感謝。

回答

1

可拖動矩形,因爲矩形已經是可移動的,你只需要繼承它,重載不同的鼠標功能(與軌道的東西),然後調用父類的鼠標事件。基本上,你可以有你想要這個矩形類的內容:

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 

我希望這是你所期待的。

+0

+1。我很感謝你的回答,它肯定會澄清一些鼠標事件方面的問題,但問題更深入。此外,更重要的是讓父母將事件傳播給子項目。 – Alex 2013-04-27 06:32:44