要禁用 QGraphicsView 中的鼠标事件,您可以通过重写 QGraphicsView 的 mousePressEventmouseReleaseEventmouseMoveEvent 等方法,并在方法中不执行任何操作来实现禁用。

下面是一个示例:

from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QApplication
from PyQt5.QtCore import Qt


class CustomGraphicsView(QGraphicsView):
    def __init__(self):
        super().__init__()

    def mousePressEvent(self, event):
        # 禁用鼠标按下事件
        pass

    def mouseReleaseEvent(self, event):
        # 禁用鼠标释放事件
        pass

    def mouseMoveEvent(self, event):
        # 禁用鼠标移动事件
        pass


if __name__ == "__main__":
    app = QApplication([])
    view = CustomGraphicsView()
    scene = QGraphicsScene()
    view.setScene(scene)
    view.show()
    app.exec_()

在这个示例中,CustomGraphicsView 继承自 QGraphicsView,并重写了鼠标按下、释放和移动事件的方法。在这些方法中,我们只是简单地使用了 pass 语句,这样就不会执行任何操作,从而禁用了鼠标事件。

您可以根据自己的需求在这些方法中添加特定的逻辑来控制鼠标事件的行为。

禁用 QGraphicsView 鼠标事件:Python 示例

原文地址: https://www.cveoy.top/t/topic/qlGW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录