【问题标题】:How to assign QScintilla shortcuts如何分配 QScintilla 快捷方式
【发布时间】:2018-05-17 06:57:29
【问题描述】:

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

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

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

说清楚:

  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_())

【问题讨论】:

  • 显示您的代码。

标签: python pyqt keyboard-shortcuts pyqt5 qscintilla


【解决方案1】:

QScintilla 已经提供了QsciCommandSetQsciCommand 类来处理内部编辑器命令的快捷方式。您也可以使用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')

【讨论】:

    【解决方案2】:

    如果您在 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-28
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      • 2021-08-09
      相关资源
      最近更新 更多