0

我有 MainWindow 类,它有 qscintilla 编辑器,我想将监听器添加到编辑器 mousePressEvent

class MainWindow(QtWidgets.QMainWindow, gui.Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.editor.mousePressEvent = self.on_editor_click

    def on_editor_click(self, QMouseEvent):
        // here i want add my code
        return QsciScintilla.mousePressEvent(self, QMouseEvent)

如果我覆盖 mousePressEvent - 编辑器将损坏(鼠标单击将不起作用)。我尝试调用初始 mousePressEvent,但它不起作用,应用程序崩溃

4

1 回答 1

0

将 mousePressEvent 方法分配给另一个函数是不正确的,mousePressEvent 不是信号,它是 QsciScintilla 的一部分的函数。一个可能的解决方案是创建一个个性化的 QsciScintilla,它会发出如下所示的信号:

class ClickQsciScintilla(QsciScintilla):
    clicked = QtCore.pyqtSignal()

    def mousePressEvent(self, event):
        self.clicked.emit()
        QsciScintilla.mousePressEvent(self, event)

然后创建一个 ClickQsciScintilla 实例并连接到该信号:

self.__editor = ClickQsciScintilla()
self.__editor.clicked.connect(self.on_editor_click)

您的处理程序:

 def on_editor_click(self):
        print "Editor was clicked!"
于 2019-12-23T20:20:19.987 回答