【发布时间】:2015-06-16 05:53:55
【问题描述】:
我有一棵这样的树:
标题1 标题2 标题3
节点1
--node2
----node3 value1 value2
----node4 value3 value4
等等……
我需要为同一行中的不同值过滤模型。 也就是说,如果 value1 与 value2 相同(等于),则跳过此行,否则 - 显示。
有一些示例代码:
class FindFilterProxyModel(QtCore.QSortFilterProxyModel):
def filterAcceptsRow(self, source_row, source_parent):
if (self.filterAcceptsRowItself(source_row, source_parent)):
return True
if (self.hasAcceptedChildren(source_row, source_parent)):
return True
return False
def filterAcceptsRowItself(self, source_row, source_parent):
return super(FindFilterProxyModel, self).\
filterAcceptsRow(source_row, source_parent)
def hasAcceptedChildren(self, source_row, source_parent):
model = self.sourceModel()
sourceIndex = model.index(source_row, 0, source_parent)
if not (sourceIndex.isValid()):
return False
childCount = model.rowCount(sourceIndex)
if (childCount == 0):
return False
for i in range (childCount):
if (self.filterAcceptsRowItself(i, sourceIndex)):
return True
# recursive call -> NOTICE that this is depth-first searching,
# you're probably better off with breadth first search...
if (self.hasAcceptedChildren(i, sourceIndex)):
return True
return False
它递归地比较第一列中的值(用于搜索)。 我想将它与除第一列之外的所有列进行比较。
【问题讨论】:
-
你已经说了你想做什么并提供了一些代码。但是你的问题是什么?您需要什么帮助?
-
请注意,一旦修改了子项,这种通过子项属性过滤父项的方法很脆弱;据我记得,当子级发生变化时,不会再次为父级调用 filterAcceptsRow,因此不会更新其过滤器状态。
-
@three_pineapples "我需要为同一行中的不同值过滤模型。也就是说,如果 value1 与 value2 相同(等于),则跳过此行,如果不 - 显示。"
-
@FrankOsterfeld 谢谢!我知道这一点,我有这个案例的“刷新按钮”。
-
@Max_Tar 引用我的部分问题并不能帮助我理解。我确实已经阅读了你的整个问题。我仍然不明白你在寻找什么答案。我知道您需要什么,但是 您的问题是什么?您在帖子中似乎没有真正提出问题。您在使用
QSortFilterProxyModel实施解决方案的哪一部分遇到问题?