0

由于排序和/或过滤,我正在使用一些带有 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

固定的行号分配给每一行。这在排序/过滤时会导致问题。我想出了一个解决方案:覆盖默认的过滤和排序方法,并将一些额外的参数(行号)放入数据中,并在每次排序或过滤期间重写它。

问题:还有其他解决方案吗?一些在排序/过滤操作后向我显示真实项目位置的方法?

4

1 回答 1

4

一个简单的QSortFilterProxyModelwith custom子类headerData可以做到这一点:

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)
于 2013-02-27T13:11:51.227 回答