PyQt QPlainTextEdit:如何替换右键单击组合键

时间:2016-12-09 06:52:40

标签: python pyqt keyboard-shortcuts signals-slots qplaintextedit

如何替换"右键单击"在以下代码片段中使用键组合(例如Ctrl-S)?我搜索了谷歌和Qt手册,但仍然不知道如何做到这一点。我是Qt的新手。任何帮助将不胜感激。

(PS to @ekhumoro:在你对#34; PyQt: How to insert text at the cursor in QTableView"问题的回答中,我似乎无法解答。我在这里使用了你的想法。但是我' d喜欢使用组合键或按钮。)

class MyDelegate(QStyledItemDelegate):
    contextMenuRequested = pyqtSignal(object, QPoint)

    def __init__(self, parent=None):
        super(MyDelegate, self).__init__(parent)

    def createEditor(self, parent, option, index):
        editor = QPlainTextEdit(parent)
        editor.setContextMenuPolicy(Qt.CustomContextMenu)
        editor.customContextMenuRequested.connect(
            self.commitAndCloseEditor)  # !!! right-click

    def commitAndCloseEditor(self):
        pass

1 个答案:

答案 0 :(得分:2)

您可以使用QShortCut

class MyDelegate(QStyledItemDelegate):
    def __init__(self, parent=None):
        super(MyDelegate, self).__init__(parent)
        self.shortcut = QtGui.QShortcut(
            QtGui.QKeySequence('Ctrl+S'), parent)
        self.shortcut.activated.connect(self.commitAndCloseEditor)

    def createEditor(self, parent, option, index):
        editor = QPlainTextEdit(parent)
        return editor