我正在使用QSortFilterProxyModel來過濾來自QAbstractListModel的結果。但是,我想返回原始模型中不存在的第一個條目,即它是某種人造的。QSortFilterProxyModel返回虛假行
這是我到目前爲止有:
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
這將在形式返回一個列表:
Account 1
Account 2
...
ID」喜歡在返回的開頭返回一個額外的元素清單f元素:
Extra Element
Account 1
Account 2
...
我試圖重新實現rowCount時才能返回真實rowCount時()+ 1,但不知何故,我會需要轉移所有項目才能返回索引爲0的這個人造元素,我在那裏有點失落。
任何線索?到目前爲止我找不到任何相關的代碼示例...謝謝!
我不確定QSortFilterProxyModel是最好的地方嘗試做到這一點。操作術語是_sort_和_filter_。我認爲在定製模型中這樣做會更好。 – 2010-09-16 21:26:51