【发布时间】:2010-08-13 05:18:31
【问题描述】:
我有一个重新实现的 QSortFilterProxyModel acceptRows 来实现自定义行为,我希望它不会过滤掉具有有效子项的项目。
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:这个类基本上是我在KDE websvn 中找到的 KSysGuard 的翻译
【问题讨论】: