如何使用QMainWindow中的GraphicsView的mouseDoubleClickEvent调用并运行具有给定数量输入的函数?

问题描述 投票:0回答:1

如何使用QMainWindow中的GraphicsView的mouseDoubleClickEvent调用并运行具有给定数量输入的函数?

My schematic code:

class MainWindow(QMainWindow):
    ...

    def __setUI(self, appTitle="[default title]"):
        ...

        self.graphicsView = GraphicsView(self)

        self.graphicsView.mouseDoubleClickEvent = self.MyFunc(self.in_1, self.in_2)

     def MyFunc(self, event, input_1, input_2):
        ...

我使用了这段代码,但是没有用。请帮助我知道如何使用MainWindow中的graphicsView的mouseDoubleClickEvent来调用和运行MyFunc。

非常感谢

python pyqt pyqt5
1个回答
0
投票

一种解决方案是传递lambda,例如:

self.graphicsView.mouseDoubleClickEvent = lambda event : self.MyFunc(self.in_1, self.in_2)

它工作但它会产生问题,因为mouseDoubleClickEvent具有删除前一代码的实现。在这种情况下,最好的解决方案是使用eventFilter,但是对于视口,因为它接收鼠标事件。

from PyQt5 import QtCore, QtWidgets

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.graphicsView = QtWidgets.QGraphicsView()
        self.setCentralWidget(self.graphicsView)
        self.graphicsView.viewport().installEventFilter(self)

        self.in_1 = 10
        self.in_2 = 20

    def eventFilter(self, obj, event):
        if obj is self.graphicsView.viewport():
            if event.type() == QtCore.QEvent.MouseButtonDblClick:
                self.func(event)
        return super(MainWindow, self).eventFilter(obj, event)

    def func(self, event):
        print(event.pos(), self.in_1, self.in_2)


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())
© www.soinside.com 2019 - 2024. All rights reserved.