我正在使用QSortFilterProxyModel来过滤来自QAbstractListModel的结果。但是,我想返回第一个条目,该条目在原始模型中不存在,也就是说,它某种程度上是人为的。
这是我到目前为止的内容:
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
Run Code Online (Sandbox Code Playgroud)
这将以以下形式返回列表:
Account 1
Account 2
...
Run Code Online (Sandbox Code Playgroud)
我想在返回的列表f元素的开头返回一个额外的元素:
Extra Element
Account 1
Account 2
...
Run Code Online (Sandbox Code Playgroud)
我试图重新实现rowCount以便返回真实的rowCount()+ 1,但是以某种方式我需要移动所有项才能返回索引0处的该人造元素,而我在那里有点迷失了。
有什么线索吗?到目前为止,我找不到任何相关的代码示例...谢谢!