QSortFilterProxyModel 返回人工行 [英] QSortFilterProxyModel returning artificial row

查看:38
本文介绍了QSortFilterProxyModel 返回人工行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 QSortFilterProxyModel 过滤来自 QAbstractListModel 的结果.但是,我想返回原始模型中不存在的第一个条目,也就是说,它在某种程度上是人为的.

I'm using a QSortFilterProxyModel to filter results from a QAbstractListModel. However, I'd like to return a first entry which is not present in the original model, that is, it's somehow artificial.

这是我目前所拥有的:

class ActivedAccountModel(QSortFilterProxyModel):                                                                                                                                  
    def __init__(self, model, parent=None):
        super(ActiveAccountModel, self).__init__(parent)
        self.setSourceModel(model)
        self.setDynamicSortFilter(True)

    def data(self, index, role=Qt.DisplayRole):
        account_info = super(ActiveAccountModel, self).data(index, Qt.UserRole).toPyObject()
        if role == Qt.DisplayRole:
            return account_info.name
        elif role == Qt.UserRole:
            return account_info
        return None

    def filterAcceptsRow(self, source_row, source_parent):
        source_model = self.sourceModel()
        source_index = source_model.index(source_row, 0, source_parent)
        account_info = source_model.data(source_index, Qt.UserRole)
        return isinstance(account_info.account, Account) and account_info.account.enabled

这将以以下形式返回一个列表:

This will return a list in the form of:

Account 1
Account 2
...

Id' 喜欢在返回的列表 f 个元素的开头返回一个额外的元素:

Id' like to return an extra element at the begining of the returned list f elements:

Extra Element
Account 1
Account 2
...

我试图重新实现 rowCount 以返回真正的 rowCount()+1,但不知何故我需要 shift 所有项目以返回索引 0 处的这个人工元素,并且我有点迷茫.

I tried to reimplement rowCount in order to return the real rowCount()+1, but somehow I'd need to shift all the items in order to return this artificial element at index 0, and I'm a bit lost there.

有什么线索吗?到目前为止我找不到任何相关的代码示例...谢谢!

Any clue? I couldn't find any related code example so far... Thanks!

推荐答案

我已经这样做了,只是在工作中,所以我不能给你很多代码.我可以给你一个大致的想法.

I've done this, only at work, so I can't give you much code. I can give you the general idea of what to do.

如果您将 QAbstractProxyModel 子类化,效果会更好,后者专为一般操作而设计,不排序或过滤.您需要覆盖 rowCount,并且还需要覆盖 columnCount(尽管这应该只返回来自源模型的信息).您需要覆盖数据函数并为第一行返回您自己的数据,或者再次调用源模型.

It works better if you subclass QAbstractProxyModel, which is designed for general manipulation, not sorting or filtering. You'll want to override rowCount, and also need to override columnCount (although that should just return the information from the source model). You'll need to override the data function and return your own data for the first row, or call the source model once more.

您需要覆盖 mapFromSource 和 mapToSource 函数,以允许在代理模型索引和源模型索引之间切换.

You will want to override the mapFromSource and mapToSource functions, to allow switching between the proxy model indexes and source model indexes.

要实现稳健的实现,您需要创建一些插槽并连接到源模型的信号以进行数据更改、模型重置以及即将插入/删除的行/列.然后,您应该发出自己的信号,适当调整它们以应对您的额外行.

To do a robust implementation, you'll need to create some slots and connect to the source model's signals for data changing, model reset, and rows/columns about to be inserted/removed. You should then emit your own signals, properly adapting them to account for your extra row.

在我们的类中,我们将第一行的文本设为可设置,因此我们可以在不同情况下使用相同的代理模型.值得为您进行调查,因为它增加了最少的工作量.

In our class, we made the text for the first row settable, so we could use the same proxy model in different situations. It's worth investigating for yours, since it adds minimal effort.

编辑

根据评论请求,粗略查看 mapToSource 和 mapFromSource.这大约是您需要考虑的.

Per commented request, a rough look at mapToSource and mapFromSource. This is approximately what you need to think about.

// Remember that this maps from the proxy's index to the source's index, 
// which is invalid for the extra row the proxy adds.
mapToSource( proxy_index ):
    if proxy_index isn't valid:
        return invalid QModelIndex
    else if proxy_index is for the first row:
        return invalid QModelIndex
    else
        return source model index for (proxy_index.row - 1, proxy_index.column)

mapFromSource( source_index ):
    if source_index isn't valid:
        return invalid QModelIndex
    else if source_index has a parent:
        // This would occur if you are adding an extra top-level 
        // row onto a tree model.
        // You would need to decide how to handle that condition
        return invalid QModelIndex
    else
        return proxy model index for (source_index.row + 1, source_index.column)

这篇关于QSortFilterProxyModel 返回人工行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆