【问题标题】:QTableView disable sorting for some columnsQTableView 禁用某些列的排序
【发布时间】:2017-11-09 06:29:02
【问题描述】:

我正在使用具有 10 列的 QtableView(qt5.9),并且希望在用户单击这些列的标题时禁用对第 2 列和第 3 列(仅部分)的排序。

  • 我使用 setsortingenabled 标志使我的 QtableView 允许排序

  • 是否有任何我应该在点击标题时收听的信号,然后调用一些适当的方法或拒绝排序。

【问题讨论】:

  • 您尝试使用QSortFilterProxyModel 吗?
  • @simon 是的,我尝试了 QSortFilterProxyModel,但我看到的方法只有小于那里有用,还单击标题调用 sortbycolum of view 所以如何在 QSortFilterProxyModel 中防止对任何建议进行排序
  • 也许可以试试标头信号sortIndicatorChanged

标签: qt qtableview qtcore qt-signals


【解决方案1】:

您可以使用标头信号sortIndicatorChanged 来恢复当前的排序指示器。

例子:

    connect(m_poTableView->header(), &QHeaderView::sortIndicatorChanged,
            this, &MyClass::HandleIndicatorChanged);


    MyClass::HandleIndicatorChanged(int logicalIndex, Qt::SortOrder eSort)
    {
       if (logicalIndex != 0)
       {
             this->m_poTableView->horizontalHeader()->setSortIndicator(
                0, this->m_poTableView->model()->sortOrder());
       }
    }

【讨论】:

  • @mapuna 您在这个问题上需要更多帮助吗?
【解决方案2】:

一种更简单的方法(至少对我而言)是对过滤器代理进行子类化,并仅对禁用的列覆盖排序。下面的代码是用 Python 编写的,但它是对 C++ 的简单翻译。

def CustomSorter(QtCor.QSortFilterProxyModel):
  def sort(self, column: int, order: QtCore.Qt.SortOrder) -> None:
    if column == 2 or column == 3:
      # Do nothing instead of sorting
      return
    else:
      # Sort as usual
      super().sort(column, order)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    • 2015-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    相关资源
    最近更新 更多