QTableWidget列标题的PyQt5上下文菜单

时间:2019-02-12 20:13:58

标签: python pyqt pyqt5 qtablewidget qmenu

有没有一种方法可以在表的列标题上获取上下文菜单。

在PyQt5的教程中什么都找不到。

表的上下文菜单很简单,但是列标题不起作用。

<MenuTrigger onPress={() => this._onPressItem()}>
              <Icon type="material" color="red" name="more-vert" />
          </MenuTrigger>

2 个答案:

答案 0 :(得分:2)

您必须使用QHeaderView中的QTableWidget

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))

答案 1 :(得分:2)

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

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

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

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