I am using a Gtk.TreeView with a Gtk.TreeStore as a model for hierarchical data. As an example, let's take a music database organized into three levels: artist/album/title. I would like to filter this tree using a textual search field. For example, typing "Five" into the search field should give a result along the path "Hank Marvin/Heartbeat/Take Five".
My understanding is that I need to create a callback function and register it using Gtk.TreeModelFilter.set_visible_func(). The problem is that making the line for "Take Five" visible is not enough to make it appear, I also have to set all of its parents visible as well. However, that would require me to traverse the tree up to its root and actively make each node visible along that path, which does not fit into the callback pattern.
One way I see to make this logic work with the callback pattern is to check the whole subtree in the callback function, but that way each leaf node would get checked three times. Even though the performance penalty would be acceptable with such a shallow tree, this hack gives me the goosebumps and I would like to refrain from using it:
def visible_callback(self, model, iter, _data=None):
search_query = self.entry.get_text().lower()
if search_query == "":
return True
text = model.get_value(iter, 0).lower()
if search_query in text:
return True
# Horrible hack
for i in range(model.iter_n_children(iter)):
if self.visible_callback(model, model.iter_nth_child(iter, i)):
return True
return False
What is the intended way to filter a tree view in GTK? (My example is written in Python, but a solution for any language binding of GTK would be fine.)
Finally I came up with a solution and since I haven't found any treeview filtering examples on the internet that uses a
TreeStoreand not aListStore, I'm posting my solution here as an example: