Issue
I have a searchable tree view that works very well for all root nodes. However, I have no idea how to stop it from removing the parent to search in the children. It only works if the parent and the child match the search criteria. For intance if I search for "fresh" in the example from the image it will not display the third line as the parent will be hidden.
in the filterAcceptsRow I only have access to the proxy model and I cannot check if something is expanded or not. At least I have no idea how to do it to simply ignore all expanded items from the filter to allow searching in their children.
the newer versions of QT have this functionality built in setRecursiveFilteringEnabled
but unfortunately I'm stuck with an old one for a while.
def filterAcceptsRow(self, source_row, source_parent_index):
model = self.sourceModel()
source_index = model.index(source_row, 0, source_parent_index)
# my naive attempt that only works for views that dynamically populate the children
# that totally fails on statically popluated ones as it thinks that everythign is expanded
# if model.hasChildren(source_index) and not model.canFetchMore(source_index):
# return True
d = model.searchableData(source_index) #this simply returns a string that I can regex in the filter
return self.isVisible(d) #some custom regex magic not important here
ideally, I would love to keep the parent if the filter matches anything in the children (or in the parent itself)
Solution
You need to call the filter function recursively and return True
if any of the children also returns True
:
def filterAcceptsRow(self, source_row, source_parent_index):
model = self.sourceModel()
source_index = model.index(source_row, 0, source_parent_index)
for child_row in range(model.rowCount(source_index)):
if self.filterAcceptsRow(child_row, source_index):
return True
d = model.searchableData(source_index)
return self.isVisible(d)
Obviously, if the model is very extended, you should consider some caching, but you probably would need to use QPersistentModelIndex as keys, and override whatever functions you use to update the filter (assuming they're being called programmatically, since they are not virtual and overriding them for an internal Qt implementation, such as a QCompleter, will not work).
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.