编辑时修改 QTableWidgetItem 上下文菜单
Modify QTableWidgetItem context menu when editing
我正在尝试向弹出的上下文菜单添加操作,当我在 QTableWidget
中编辑单元格的内容时右键单击时会弹出该菜单。我尝试从 QTableWidget
重新定义 contextMenuEvent()
方法,但从未在此上下文中调用它。我还尝试对项目调用 cellWidget()
方法,但正如我所料,它返回了 None
。我猜当单元格处于编辑模式时会创建一个临时 QLineEdit
小部件,但找不到任何东西来确认这一点。
有什么方法可以在编辑模式下访问 QTableWidgetItem
的上下文菜单吗?
我仔细查看了文档,但无济于事。
您必须处理的上下文菜单是委托提供的编辑器。所以在这种情况下,必须实现 QStyledItemDelegate:
from PyQt5 import QtCore, QtGui, QtWidgets
class StyledItemDelegate(QtWidgets.QStyledItemDelegate):
def createEditor(self, parent, option, index):
editor = super().createEditor(parent, option, index)
if isinstance(editor, QtWidgets.QLineEdit):
editor.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
editor.setProperty("index", QtCore.QPersistentModelIndex(index))
editor.customContextMenuRequested.connect(self.handle_context_menu)
return editor
def handle_context_menu(self, pos):
editor = self.sender()
if isinstance(editor, QtWidgets.QLineEdit):
index = editor.property("index")
menu = editor.createStandardContextMenu()
action = menu.addAction("New Action")
action.setProperty("index", index)
action.triggered.connect(self.handle_add_new_action_triggered)
menu.exec_(editor.mapToGlobal(pos))
def handle_add_new_action_triggered(self):
action = self.sender()
if isinstance(action, QtWidgets.QAction):
index = action.property("index")
print(index.row(), index.column(), index.data())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
view = QtWidgets.QTableWidget(3, 5)
delegate = StyledItemDelegate(view)
view.setItemDelegate(delegate)
view.resize(640, 480)
view.show()
sys.exit(app.exec_())
我正在尝试向弹出的上下文菜单添加操作,当我在 QTableWidget
中编辑单元格的内容时右键单击时会弹出该菜单。我尝试从 QTableWidget
重新定义 contextMenuEvent()
方法,但从未在此上下文中调用它。我还尝试对项目调用 cellWidget()
方法,但正如我所料,它返回了 None
。我猜当单元格处于编辑模式时会创建一个临时 QLineEdit
小部件,但找不到任何东西来确认这一点。
有什么方法可以在编辑模式下访问 QTableWidgetItem
的上下文菜单吗?
我仔细查看了文档,但无济于事。
您必须处理的上下文菜单是委托提供的编辑器。所以在这种情况下,必须实现 QStyledItemDelegate:
from PyQt5 import QtCore, QtGui, QtWidgets
class StyledItemDelegate(QtWidgets.QStyledItemDelegate):
def createEditor(self, parent, option, index):
editor = super().createEditor(parent, option, index)
if isinstance(editor, QtWidgets.QLineEdit):
editor.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
editor.setProperty("index", QtCore.QPersistentModelIndex(index))
editor.customContextMenuRequested.connect(self.handle_context_menu)
return editor
def handle_context_menu(self, pos):
editor = self.sender()
if isinstance(editor, QtWidgets.QLineEdit):
index = editor.property("index")
menu = editor.createStandardContextMenu()
action = menu.addAction("New Action")
action.setProperty("index", index)
action.triggered.connect(self.handle_add_new_action_triggered)
menu.exec_(editor.mapToGlobal(pos))
def handle_add_new_action_triggered(self):
action = self.sender()
if isinstance(action, QtWidgets.QAction):
index = action.property("index")
print(index.row(), index.column(), index.data())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
view = QtWidgets.QTableWidget(3, 5)
delegate = StyledItemDelegate(view)
view.setItemDelegate(delegate)
view.resize(640, 480)
view.show()
sys.exit(app.exec_())