gpt4 book ai didi

python - QGraphicsScene 鼠标事件

转载 作者:行者123 更新时间:2023-12-01 07:25:54 33 4
gpt4 key购买 nike

我正在使用 QGraphicsScene 类。如何检测鼠标离开 QGraphicsScene ?是否有任何内部函数可以实现此目的?

最佳答案

如果您想检测释放事件,您必须覆盖 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_())

关于python - QGraphicsScene 鼠标事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57455636/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com