PyQt QPlainTextEdit:如何用组合键替换右键单击
PyQt QPlainTextEdit: How to replace right-click with key combination
如何用组合键(例如 Ctrl-S)替换以下代码段中的 "right-click"?我搜索了 google 和 Qt 手册,但仍然不知道该怎么做。我是 Qt 的新手。任何帮助将不胜感激。
(P.S。致@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
如何用组合键(例如 Ctrl-S)替换以下代码段中的 "right-click"?我搜索了 google 和 Qt 手册,但仍然不知道该怎么做。我是 Qt 的新手。任何帮助将不胜感激。
(P.S。致@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