2

How do I substitute "right-click" in the following snippet with a key combo (for example Ctrl-S)? I searched google and Qt manuals but still have no idea how to do it. I am new to Qt. Any help will be greatly appreciated.

(P.S. to @ekhumoro: I can't seem to @you in your answer to the "PyQt: How to insert text at the cursor in QTableView" question. I used your idea here. But I'd like to use key combination or a button.)

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
Community
  • 1
  • 1
mike
  • 113
  • 2
  • 10
  • If you comment on someone's answer/question, they will be automatically notified (i.e. no need to use @whatever). – ekhumoro Dec 09 '16 at 16:43

1 Answers1

2

You can use a 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
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Thanks very much for the hint. I leared a bit about QKeySequence again. – mike Dec 10 '16 at 01:26
  • Done. Thanks again. I tried to accept your answer previously but had trouble locating the Accept button. I have been around SO for a while now but mostly just reading posts. – mike Dec 11 '16 at 02:19