由于排序和/或过滤,我使用了一些带有QSortFilterProxyModel扩展的相关模型的表视图。一切正常,除了行号(我指的是垂直标题)。使用以下代码:
def headerData(self, section, orientation, role):
if role == QtCore.Qt.DisplayRole:
if orientation == QtCore.Qt.Horizontal:
return self.__header[section]
elif orientation == QtCore.Qt.Vertical:
return section + 1为每一行分配固定的行号。这会在排序/过滤时产生问题。我想出了一个解决方案:覆盖默认的过滤和排序方法,将一些额外的参数(行号)放入数据中,并在每次排序或过滤时重写它。
问:有没有其他的解决方案?排序/过滤操作后显示真实项目位置的一些方法?
发布于 2013-02-27 21:11:51
具有自定义headerData的QSortFilterProxyModel的一个简单子类可以做到这一点:
class MyProxy(QtGui.QSortFilterProxyModel):
def headerData(self, section, orientation, role):
# if display role of vertical headers
if orientation == QtCore.Qt.Vertical and role == QtCore.Qt.DisplayRole:
# return the actual row number
return section + 1
# for other cases, rely on the base implementation
return super(MyProxy, self).headerData(section, orientation, role)https://stackoverflow.com/questions/15111965
复制相似问题