Re: Proper Handling of GtkTreeModelFilters



On Fri, Jun 22, 2007 at 02:47:30PM -0400, Matthew Yaconis wrote:
I have an underlying model for a TreeView in my application that I am 
filtering based on certain user selections.  I maintain a global reference 
to the "base" TreeModel (which is sometimes attached to the TreeView used to 
display the information.)  The mechanism I am using goes something like 
this:

1.  User selects a new filter criteria
2.  (Depending on the selection) Either a) gtk_tree_model_filter_new to 
create a new filter based on the user's criteria from the global "base" 
model or b) use the base model.
3.  gtk_tree_view_set_model( [selected model])

GtkTreeModelFilter does not contain much data to preserve.

So, to get a different filter, just take the current
filter and call gtk_tree_model_filter_set_visible_func() or
gtk_tree_model_filter_set_modify_func() on it -- or change
the private data the filter uses and call
gtk_tree_model_filter_refilter().

And instead of switching to the base model you can set the
visible func to one that always returns TRUE (for some
reason it is not possible to set the filter functions back
to NULL).  This saves you testing whether to convert iters
and paths or not as you convert them always.

I only physically replace the filter if the virtual root has
to change as it is construction only property.  Sometimes
one has a handful of well-defined (semi)fixed filters that
it makes sense to keep around, but usually not.

I was thinking this is probably leaving a bunch of filtered tree models 
around that aren't doing anything but taking up space...do I need to be 
g_object_unref'ing the old filter models?  I attempted a "replace" function 
that looks something like:

void replaceTreeModel(GtkTreeModel *model)
{
    GtkTreeView *treeview = [get the widget];
     GtkTreeModel *currentModel = gtk_tree_view_get_model(treeview);

    gtk_tree_view_set_model( treeview, model );

    // The following makes the application unstable and crash in the filter 
function so I've commented it out for now
    //if (GTK_IS_TREE_MODEL_FILTER( currentModel))
    //{
    //   g_object_unref( G_OBJECT(currentModel));
    //}
}

What holds references to currentModel after the
gtk_tree_view_set_model() call?  GtkTreeModelFilter is
created with reference count 1, so you should be left with
this inital reference at the end -- but only if you didn't
released it earlier.  And if you did, then currentModel is
already destroyed when you try GTK_IS_TREE_MODEL_FILTER().

It's hard to deduce anything from such code fragment.

If you don't need to keep the filters around, it's probably
easiest to release the inital reference immediately after
you do gtk_tree_view_set_model() and do not care about them
any more as they will be destroyed automatically once
nothing needs them.

Yeti

--
http://gwyddion.net/



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]