QTableWidget 列头的 PyQt5 上下文菜单



有没有办法在表列头上获取上下文菜单。

在 PyQt5 的 tuts 中找不到任何关于这一点的信息。

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

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

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

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

并连接到"QHeaderView::customContextMenuRequest...

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

你必须使用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))

相关内容

  • 没有找到相关文章

最新更新