QTableWidget 列标题的 PyQt5 上下文菜单

PyQt5 context menu for QTableWidget column head

有没有办法在 table 的列标题上获得上下文菜单。

在 PyQt5 的 tuts 中找不到任何相关内容。

table 的上下文菜单很简单,但列标题不受影响。

# dlg is a QDialog object
self.tbl = QtWidgets.QTableWidget(dlg)
self.tbl.setContextMenuPolicy( Qt.CustomContextMenu )

您必须使用 QTableWidgetQHeaderView:

from PyQt5 import QtCore, QtWidgets

class Dialog(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)
        self.tbl = QtWidgets.QTableWidget(10, 10, self)

        for w in (self.tbl.horizontalHeader(), self.tbl.verticalHeader(), self.tbl):
            w.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
            w.customContextMenuRequested.connect(self.on_customContextMenuRequested)

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(self.tbl)

    @QtCore.pyqtSlot(QtCore.QPoint)
    def on_customContextMenuRequested(self, pos):
        widget = self.sender()
        if isinstance(widget, QtWidgets.QAbstractItemView):
            widget = widget.viewport()
        menu = QtWidgets.QMenu()
        menu.addAction("Foo Action")
        menu.exec_(widget.mapToGlobal(pos))

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Dialog()
    w.show()
    sys.exit(app.exec_())

更新:

class Dialog(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)
        self.tbl = QtWidgets.QTableWidget(10, 10, self)

        self.tbl.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.tbl.customContextMenuRequested.connect(self.on_customContextMenuRequested_tw)

        self.tbl.verticalHeader().setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.tbl.verticalHeader().customContextMenuRequested.connect(self.on_customContextMenuRequested_vh)

        self.tbl.horizontalHeader().setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.tbl.horizontalHeader().customContextMenuRequested.connect(self.on_customContextMenuRequested_hh)

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(self.tbl)

    @QtCore.pyqtSlot(QtCore.QPoint)
    def on_customContextMenuRequested_tw(self, pos):
        menu = QtWidgets.QMenu()
        menu.addAction("Foo Action TW")
        menu.exec_(self.tbl.viewport().mapToGlobal(pos))

    @QtCore.pyqtSlot(QtCore.QPoint)
    def on_customContextMenuRequested_vh(self, pos):
        menu = QtWidgets.QMenu()
        menu.addAction("Foo Action VH")
        menu.exec_(self.tbl.verticalHeader().mapToGlobal(pos))

    @QtCore.pyqtSlot(QtCore.QPoint)
    def on_customContextMenuRequested_hh(self, pos):
        menu = QtWidgets.QMenu()
        menu.addAction("Foo Action HH")
        menu.exec_(self.tbl.horizontalHeader().mapToGlobal(pos))

您需要在 header 本身上设置上下文菜单策略(如果我理解正确的话),所以...

self.tbl = QtWidgets.QTableWidget(dlg)
self.tbl.horizontalHeader().setContextMenuPolicy(Qt.CustomContextMenu)

并连接到 `QHeaderView::customContextMenuRequested 信号...

self.tbl.horizontalHeader().customContextMenuRequested.connect(self.handle_context_menu_request)