【问题标题】:Getting Header Column on Right Click for QTableWidget右键单击 QTableWidget 获取标题列
【发布时间】:2012-08-06 23:40:46
【问题描述】:

我有一个 QTableWidget,其中有许多列只是复选框(有些不是)。我正在尝试实现一项功能,以便当用户右键单击与“仅复选框”列相关的标题项时,他们会看到“取消选中全部”或“全部选中”的选项。

到目前为止,我已经设法通过以下信号实现了customContextMenu

self.headers = self.tblData.horizontalHeader()
self.headers.setContextMenuPolicy(Qt.CustomContextMenu)
self.headers.customContextMenuRequested.connect(self.show_header_context_menu)
self.headers.setSelectionMode(QAbstractItemView.SingleSelection)

这会导致以下上下文菜单调用:

def show_header_context_menu(self, position):
    menu = QMenu()
    deselect = menu.addAction("Clear Checked")
    ac = menu.exec_(self.tblData.mapToGlobal(position))
    if ac == deselect:
        pass
        #Actually do stuff here, of course

这会弹出一个上下文菜单,但是我找不到任何方法来获取右键单击的标题的索引,我尝试过 self.headers.selectedIndexes()self.headers.currentIndex() 但这些似乎只与实际的表格选择,而不是标题。

一旦我设法获得右键单击的标题索引,我就可以轻松地将菜单限制为仅在选择正确的索引时才显示(那些只有复选框的列),所以这真的是额外的事情。

我错过了什么?提前感谢您的帮助。

【问题讨论】:

    标签: python qt pyqt pyqt4 qtablewidget


    【解决方案1】:

    customContextMenuRequested 信号将上下文菜单事件的位置作为QPoint 发送。方便的是,表的标题有logicalIndexAt 的重载,可以直接利用它,所以你可以简单地这样做:

    def show_header_context_menu(self, position):
        column = self.headers.logicalIndexAt(position)
    

    【讨论】:

    • 完美。我知道我错过了一些东西!非常感谢!
    【解决方案2】:

    我看到您正在使用 python,但我认为这应该仍然有效。

    尝试创建一个QHeaderView-派生类并尝试覆盖

    void QHeaderView::mousePressEvent ( QMouseEvent * e )
    

    获取鼠标右键事件,然后使用

    int QHeaderView::logicalIndexAt ( int x, int y ) const
    

    获取被右键单击的列的逻辑索引。最后显示您的上下文菜单。

    http://doc.qt.nokia.com/4.7-snapshot/qheaderview.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-11
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多