如何分配 QScintilla 快捷方式

How to assign QScintilla shortcuts

我正在尝试为我的编辑器实现快捷方式,但到目前为止我还没有成功。

我想覆盖一些默认的 QScintilla 快捷方式。我已阅读此 answer,但我不确定这是否有助于解决我的问题。

我也阅读了 Scintilla (SCI_ASSIGNCMDKEY) 文档,但我不知道我应该如何以 pythonic 方式使用它。

要说清楚:

  1. 我想覆盖 QScintilla 快捷方式 Ctrl+L 并使用我的自定义解决方案(将其分配给我的功能之一)。

  2. 我想将命令 SCI_LINEDELETE 分配给快捷方式 Ctrl+D

这是我的想法:

    from PyQt5.Qsci import QsciScintilla
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *
    import sys

    class mainWindow(QMainWindow):
        def __init__(self, parent = None):
            super(mainWindow, self).__init__(parent)
            self.initUI()

        def initUI(self):
            self.center_window = centerWindow(parent=self)
            self.setCentralWidget(self.center_window) 

    class centerWindow(QWidget):
        def __init__(self, parent=None):
            super(centerWindow, self).__init__(parent)

            self.hhEditor_te = QsciScintilla()

            vbox = QVBoxLayout(self)
            vbox.addWidget(self.hhEditor_te)
            self.setLayout(vbox)

            # 1)
            # assign a key binding to this function
            # self.my_shortcut

            # 2)
            # assign a key binding to the QScintilla command
            # SCI_LINEDELETE

        def my_shortcut(self):
            pass
            # my custom shortcut function

    if __name__ == '__main__':
        app = QApplication.instance()
        if app is None:
            app = QApplication(sys.argv)
        else:
            print('QApplication instance already exists: %s' % str(app))

        ex = mainWindow()
        ex.setGeometry(0,100,1500,600)
        ex.show()
        sys.exit(app.exec_())

QScintilla 已经提供了 QsciCommandSet and QsciCommand classes for handling shortcuts for the internal editor commands. You can also use QShortcut 来为您自己的方法创建快捷方式。

class centerWindow(QWidget):
    def __init__(self, parent=None):
        ...
        commands = self.hhEditor_te.standardCommands()

        command = commands.boundTo(Qt.ControlModifier | Qt.Key_L)
        if command is not None:
            command.setKey(0) # clear the default
        command = commands.boundTo(Qt.ControlModifier | Qt.Key_D)
        if command is not None:
            command.setKey(0) # clear the default

        command = commands.find(QsciCommand.LineDelete)
        if command is not None:
            command.setKey(Qt.ControlModifier | Qt.Key_D)

        shortcut = QShortcut(Qt.ControlModifier | Qt.Key_L, self.hhEditor_te)
        shortcut.activated.connect(self.my_shortcut)
        ...

    def my_shortcut(self):
        print('Ctrl+L')

如果您在 QScintilla 中使用工具栏,操作快捷方式如下所示

self.toolBar.newAction = QtWidgets.QAction(QtGui.QIcon(":/ico/new.png"),"New",self.toolBar)
self.toolBar.newAction.setStatusTip("Clear TextBox or make new document.")
self.toolBar.newAction.setShortcut("Ctrl+N")
self.toolBar.newAction.triggered.connect(self.newfile)

    #actions
self.toolBar.addAction(self.toolBar.newAction)
self.toolBar.addSeparator()