Skip to main content
 首页 » 编程设计

python之PyQt QPlainTextEdit : How to replace right-click with key combination

2024年10月17日8cloudgamer

如何用组合键(例如 Ctrl-S)替换以下片段中的“右键单击”?我搜索了谷歌和 Qt 手册,但仍然不知道该怎么做。我是 Qt 的新手。任何帮助将不胜感激。

(附言@ekhumoro:在你对“PyQt: How to insert text at the cursor in QTableView”问题的回答中,我似乎无法@你。我在这里使用了你的想法。但我想使用组合键或按钮。)

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 

请您参考如下方法:

您可以使用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