tags:

views:

23

answers:

1

Hi

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.lower() 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?

+1  A: 

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.

MattH
You are right, self.txt shouldn't be passed to this callback.However the thing You've found - that *model.iter_has_child(iter)*might be useful. I'll try this out.
Marcin Cylke
This actually works quite good, now I have to figure out how to nodes with only visible nodes. Could You point me to the documentation page, where you've found that code snippet?
Marcin Cylke
A google for "gtk set_visible_func" brought me to http://www.pygtk.org/docs/pygtk/class-gtktreemodelfilter.html#method-gtktreemodelfilter--set-visible-func and then I clicked through the docs looking at the methods available on the input arguments to `visible_cb`, `gtk.TreeModel` and `gtk.TreeIter`. The snippet I made up based on yours.
MattH