Firstly, you must enable mouse-tracking:
self.graphicsView.setMouseTracking(True)
Then you can either use a subclass of QGraphicsView
:
class GraphicsView(QtWidgets.QGraphicsView):
def mouseMoveEvent(self, event):
if event.buttons() == QtCore.Qt.NoButton:
print("Simple mouse motion")
elif event.buttons() == QtCore.Qt.LeftButton:
print("Left click drag")
elif event.buttons() == QtCore.Qt.RightButton:
print("Right click drag")
super(GraphicsView, self).mouseMoveEvent(event)
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
print("Press!")
super(GraphicsView, self).mousePressEvent(event)
Or install an event-filter:
self.graphicsView.viewport().installEventFilter(self)
...
def eventFilter(self, source, event):
if event.type() == QtCore.QEvent.MouseMove:
if event.buttons() == QtCore.Qt.NoButton:
print("Simple mouse motion")
elif event.buttons() == QtCore.Qt.LeftButton:
print("Left click drag")
elif event.buttons() == QtCore.Qt.RightButton:
print("Right click drag")
elif event.type() == QtCore.QEvent.MouseButtonPress:
if event.button() == QtCore.Qt.LeftButton:
print("Press!")
return super(Window, self).eventFilter(source, event)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…