[pygtk] Advanced Treeview Filtering Trouble

John Finlay finlay at moeraki.com
Fri Apr 28 04:06:29 WST 2006


JUAN ERNESTO FLORES BELTRAN wrote:
> Hi you all,
>
> I am developping a python application which connects to a database 
> (postresql) and displays the query results on a treeview. In adittion 
> to displaying the info i do need to implement filtering facility for 
> all the columns of the treestore/liststore model in order to allow the 
> user an easy search method to find the desired information.
>
> The treestore is created with information related to cars, the columns 
> are:
>
> car_model   car_year   car_color   car_type  car_price
> chevrolet      1998       white       sedan     5.000 US$
> ford             1996        blue         sedan     3.000 US$
>  -                 -             -                -               -
>  -                 -             -                -               -
>  -                 -             -                -               -
>
> I have been able to allow filtering to only one column,  an extract of 
> my code as follows:
>
>
> -------------------------------------------------------------------------------------------------------------------------------- 
>
>   #treestore creation
>   self.treestore = gtk.TreeStore(str, str, str, str, str)
>   self.modelfilter = self.treestore.filter_new()
>   self.treeview=gtk.TreeView()
>
>   #append treestore columns
>   self.treestore.append(None, [self.model, self.year, self.color, 
> self.type,  self.price]
>
>   #set treestore model to allow filtering by car_model column
>   self.modelfilter.set_visible_func(self.visible_cd, self.car_model)
>
> #the function to filter the treestore
> def visible_cb(self, treestore, iter, x)
>  return treestore.get_value(iter, 0) in x
>
> #self.car_model is a list of items wich change according to user needs 
> and can be controlled by  a #secundary treeview or a button  this 
> function is not explained.
> ---------------------------------------------------------------------------------------------------------------------------------- 
>
>
> The code mentioned above does work but i can only fllter  by defining 
> criterias in the first column. To allow filtering to all the columns i 
> do need the following code to work:
>
> ---------------------------------------------------------------------------------------------------------------------------------- 
>
> treemodelfilter.set_modify_func(types, func, data=None)
> def func(model, iter, column, user_data)
> ----------------------------------------------------------------------------------------------------------------------------------- 
>
>
> where types should be:
> types = (str, str, str, str, str)
>
> the function to allow filtering:
> def visible_cb(self, treestore, column, iter, x)
>  return treestore.get_value(iter, column) in x
>
> and the rest of the code never changes...however it is not woking. Any 
> suggestion about the code mention?? am i making mistakes?? where?? do 
> i have to pass the column number someway to the visible_cb function??? 
> how??
>
> can any of you suggest a code example to follow and find out how the 
> treeview must be coded in order to allow "multicolumn filtering"???
>
If I understand your question I don't think you need to use the 
set_modify_func() method. The set_visible_func() method should be 
sufficient but you have to pass in an object that has mutable contents 
corresponding to the columns that you want to use for filtering. For 
example you could create a Python object that has one attribute that 
contains a list of car models and another attribute that has a maximum 
price. Pass this object as the data arg to the set_visible_func() method 
and change the attributes as needed. Your visible_cb() function could 
then do something like:

def visible_cb(self, treestore, iter, x):
  return treestore[iter][0] in x.models and treestore[iter][4] <= x.maxprice

John




More information about the pygtk mailing list