Filtering treeview content recursively
I have GUI application with gtk.Treeview component. It's model is set to gtk.Treestore, which I fill with a hierarchical structure. Everything is working fine - the treeview is what I expect it to be.
Now I'd like to filter the leaf nodes to contain only a given string. I tried creating model filter like this:
self.modelfilter = treestore.filter_new()
self.modelfilter.set_visible_func(self.visible_cb, self.txt)
and define filtering function like the one below (self.txt is the text i'm filtering):
def visible_cb(self, model, iter, data):
return self.txt.l开发者_运维百科ower() in model.get_value(iter, 0).lower()
Unfortunately this approach is not a good one because filtering is done on all nodes, and not only leafs.
Is there an elegant solution for this problem in GTK?
I've never used the toolkit, but after browsing through the api docs... wouldn't the following work?
def visible_cb(self, model, iter, data):
return model.iter_has_child(iter) or data.lower() in model.get_value(iter, 0).lower()
Not sure why you're passing self.txt to set_visible_func
and not using the corresponding data
argument to visible_cb
.
精彩评论