2

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

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

我还阅读了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_())
4

2 回答 2

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')
于 2017-12-03T22:40:46.180 回答
0

如果您在 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()
于 2017-12-08T23:42:47.880 回答