Re: getting wrong iters when using a TreeModelFilter



On Friday 28 January 2005 12:19, Stefan Kost wrote:

Hi Stefan,

This drives me mad. I use a TreeModelFilter to hide rows (to show only
every, every 2nd, every 4th,...). When the user presses a key I'd like to
change the content of the model (the real one). I use the code below. The
problem is that when I hide nothing it works, but when e.g. hing every
second row and entering something in the shown 2nd row (which is really the
4th row), the change ends up in the sencond row too.

Is that a bug or am I missing something. It happens on gtk 2.4.9 and 2.6.1.

GtkTreeModelFilter *filtered_store;
GtkTreeModel *store;
GtkTreePath *path;
GtkTreeViewColumn *column;
GtkTreeIter iter;

filtered_store = GTK_TREE_MODEL_FILTER (gtk_tree_view_get_model (treeview));
store = gtk_tree_model_filter_get_model (filtered_store);

gtk_tree_view_get_cursor (treeview, &path, &column);

if (path && column && gtk_tree_model_get_iter (store, &iter, path)) 
{ 
  glong  track = get_track_from_somewhere();
  glong  row;

  gtk_tree_model_get (store, &iter, SEQUENCE_TABLE_POS, &row, -1);
  // cursor is at track, row
  gtk_list_store_set (GTK_LIST_STORE (store), &iter,
    SEQUENCE_TABLE_PRE_CT+track,str, -1);
}

It looks like a "thinko" on your part.

The tree view doesn't know that whatever it displays is a filter model that 
has another child model. For the treeview, there is only one model, and that 
is the filter model. That means that all tree paths you get from the tree 
view refer to a position (tree path) in the _filter model_. If the tree view 
says 'the cursor is in the second row', it means 'in the second row of the 
filter model'. It doesn't know that the second row of the filter model is in 
reality the 4th row of your underlying list store, nor does it need to know.

The problem starts here:

  gtk_tree_model_get_iter (store, &iter, path)

The tree view gives you a path that refers to the row layout in the filter 
model, but you get an iter from the underlying list store using that path.

What you need to do is something like:

   GtkTreeModel *filtermodel;
   GtkTreeIter  filter_iter, iter;

   filtermodel = GTK_TREE_MODEL (filtered_store);
   if (gtk_tree_model_get_iter (filtermodel, &filter_iter, path))
   {
       if (gtk_tree_model_convert_iter_to_child_iter (filtermodel, &iter, 
&filter_iter))
       {
           gtk_tree_model_get (store, &iter, ...., -1);
           gtk_list_store_set (GTK_LIST_STORE (store), &iter, ... -1);
       }
   }

Cheers
 -Tim



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