首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QSortFilterProxyModel和行号

QSortFilterProxyModel和行号
EN

Stack Overflow用户
提问于 2013-02-27 20:17:52
回答 1查看 1.3K关注 0票数 0

由于排序和/或过滤,我使用了一些带有QSortFilterProxyModel扩展的相关模型的表视图。一切正常,除了行号(我指的是垂直标题)。使用以下代码:

代码语言:javascript
复制
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

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-27 21:11:51

具有自定义headerDataQSortFilterProxyModel的一个简单子类可以做到这一点:

代码语言:javascript
复制
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)
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15111965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档