如何正确覆盖qscintilla mousePressEvent?

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

我有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,但无法正常工作,应用程序崩溃了

python pyqt5 qscintilla
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!"

© www.soinside.com 2019 - 2024. All rights reserved.