我有一个重新实现QSortFilterProxyModel acceptRows来实现自定义行为,我希望它不会筛选出有一个有效的孩子的项目。如何优化PyQt QSortFilterProxyModel过滤器重新实现?
class KSortFilterProxyModel(QSortFilterProxyModel):
#FIXME: Funciona pero es endemoniadamente lento
def __init__(self, parent=None):
super(KSortFilterProxyModel, self).__init__(parent)
self.__showAllChildren = False
def showAllChildren(self):
return self.__showAllChildren;
def setShowAllChildren(self, showAllChildren):
if showAllChildren == self.__showAllChildren:
return
self.__showAllChildren = showAllChildren
self.invalidateFilter()
def filterAcceptsRow (self, source_row, source_parent):
if self.filterRegExp() == "" :
return True #Shortcut for common case
if super(KSortFilterProxyModel, self).filterAcceptsRow(source_row, source_parent) :
return True
#one of our children might be accepted, so accept this row if one of our children are accepted.
source_index = self.sourceModel().index(source_row, 0, source_parent)
for i in range(self.sourceModel().rowCount(source_index)):
if self.filterAcceptsRow(i, source_index):
return True
return False
但是这种形式给出不似乎是有效的,因为有300项需要差不多3秒更新视图,我想知道,如果那里有这样做的更好的方法。
PD:该类基本上是KSysGuard一个的翻译我在KDE websvn
非常感谢,我宁愿不使用C++,但我认为这是解决这个问题的方法。 – armonge 2010-08-24 22:19:22