我和QGraphicsScene班一起工作。如何检测从QGraphicsScene中离开的鼠标?这里面有什么内部功能吗?
发布于 2019-08-12 04:35:13
如果要检测发布事件,必须重写mouseReleaseEvent()方法:
from PyQt5 import QtCore, QtGui, QtWidgets
class GraphicsScene(QtWidgets.QGraphicsScene):
def mouseReleaseEvent(self, event):
print(event.scenePos())
super().mouseReleaseEvent(event)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
scene = GraphicsScene()
w = QtWidgets.QGraphicsView(scene)
w.resize(640, 480)
w.show()
sys.exit(app.exec_())更新:
QGraphicsScene本身不是允许检测何时离开的元素,因为这可能是几个QGraphicsView的一部分,您必须做的是检测何时离开QGraphicsView覆盖leaveEvent()方法:
from PyQt5 import QtCore, QtGui, QtWidgets
class GraphicsView(QtWidgets.QGraphicsView):
def enterEvent(self, event):
print("enter")
super().enterEvent(event)
def leaveEvent(self, event):
print("leave")
super().leaveEvent(event)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
scene = QtWidgets.QGraphicsScene()
w = GraphicsView(scene)
w.resize(640, 480)
w.show()
sys.exit(app.exec_())https://stackoverflow.com/questions/57455636
复制相似问题