Results 1 to 2 of 2

Thread: [GTK#]Filter ListStore of objects

  1. #1
    Join Date
    Apr 2007
    Location
    Perth, Australia
    Beans
    61

    [GTK#]Filter ListStore of objects

    Hi, I'm having problems with filtering a ListStore of custom objects in c#/mono.

    I've got it working to add and display the objects with their data in two columns (title and path).
    What I would like to do is use a text entry field to filter the TreeView.
    I've followed the logic from the TreeView examples but it doesn't work for me.
    I've got my objects in the ListStore. I then set up the filter with my ListStore as what is to be filtered. Now here is where the problems start. If I connect the ListStore to the TreeView as the model, the filter function works. I know this won't affect what is being displayed, but it shows that the function logic is correct.
    When I connect the filter as the TreeView's model I start getting errors in the filter function.
    Code:
    : Gtk-CRITICAL **: gtk_list_store_get_value: assertion `VALID_ITER (iter, list_store)' failed
    
    : GLib-GObject-CRITICAL **: g_value_unset: assertion `G_IS_VALUE (value)' failed
    Exception in Gtk# callback delegate
      Note: Applications can use GLib.ExceptionManager.UnhandledException to handle the exception.
    System.NullReferenceException: Object reference not set to an instance of an object
    From what I understand when I try and get an object from the local reference to the model it goes crazy with NullReferenceExceptions. It doesn't give me a chance to catch them, it seems to be coming from GetValue().

    So to the actual question, how can I filter my objects using a TreeModelFilter that is connected to a ListStore and TreeView?

    Here are the relevant parts of code
    Code:
    movieList = new ListStore(typeof (Movie));
    
    ...
    
    filter = new TreeModelFilter(movieList, null);
    		filter.VisibleFunc = new TreeModelFilterVisibleFunc(FilterTitle);
    		
    		movieView.Model = movieList;
    
    ...
    
    private bool FilterTitle (Gtk.TreeModel model, Gtk.TreeIter iter)
    	{
    		Movie tempMovie = (Movie)model.GetValue(iter, 0);
    		if(tempMovie == null)
    		{
    			return false;
    		}
    		else
    		{
    			string title = tempMovie.Title;
    			if(searchbar.Text == "")
    			{
    				movieList = (ListStore)model;
    				return true;
    			}
    			if(title.IndexOf(searchbar.Text) > -1)
    			{
    				Console.WriteLine("{0} is in {1}", searchbar.Text, title);
    				movieList = (ListStore)model;
    				return true;
    			}
    			else
    			{
    				Console.WriteLine("{0} is NOT in {1}", searchbar.Text, title);
    				movieList = (ListStore)model;
    				return false;
    			}
    		}
    	}

  2. #2
    Join Date
    Apr 2007
    Location
    Perth, Australia
    Beans
    61

    Re: [GTK#]Filter ListStore of objects

    I figured out how to solve it.

    Assign the ListStore as the model to the TreeView.
    Then once the application has started, assign the filter to the model of the TreeView.
    For some reason it stops throwing errors and works as expected.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •