【问题标题】:How to correctly override qscintilla mousePressEvent?如何正确覆盖 qscintilla mousePressEvent?
【发布时间】:2019-06-14 05:16:34
【问题描述】:

我有 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,但它不起作用,应用程序崩溃

【问题讨论】:

  • QsciScintilla.mousePressEvent(self.editor, QMouseEvent)
  • 谢谢,我测试过了,它崩溃了

标签: python pyqt5 qscintilla


【解决方案1】:

将 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!"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-27
    • 2015-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多