QTableView 禁用某些列的排序 [英] QTableView disable sorting for some columns
问题描述
我正在使用包含 10 列的 QtableView(qt5.9),并希望在用户单击这些列的标题时禁用对第 2 和第 3(仅部分)列的排序.
I am using QtableView(qt5.9) with 10 columns and want to disable sorting for 2nd and 3rd (only some) columns when the user clicks on the header of these columns.
我使用 setsortingenabled 标志使我的 QtableView 允许排序
I use setsortingenabled flag to make my QtableView allow sorting
是否有任何我应该在点击标题时听到的信号,然后调用一些适当的方法或拒绝排序.
Is there any signal which I should listen to on clicking of header and then call some appropraite method or deny sorting.
推荐答案
一种更简单的方法(至少对我而言)是子类化过滤器代理并仅覆盖禁用列的排序.下面的代码是用 Python 编写的,但它是对 C++ 的简单翻译.
An easier way (for me, at least) is to subclass the filter proxy and override sorting for just the disabled columns. The below code is in Python, but it is a simple translation to 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)
这篇关于QTableView 禁用某些列的排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!