Why does gtk_tree_selection_select_iter() does not select an child row of another row even when iter is valid?

StackOverflow https://stackoverflow.com/questions/11543716

  •  21-06-2021
  •  | 
  •  

Domanda

I have an application (sscce code that works here) with a GtkTreeView. Every time a line is selectd, a callback is invoked. This callback should retrieve the selected row and process its data someway.

My application has to select some lines of this tree view itself sometimes:

// Selecting a line
gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(model), &iter, "0");
GtkTreeSelection *selection =
        gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
gtk_tree_selection_select_iter(selection, &iter);

This works well when I have to select a toplevel line, that is, a line that descends directly from the root. However, if I try to select a child line (e.g. the first child line from the third line) I got an error at the callback function (sscce code here, only change at line 42):

// Selecting a line
gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(model), &iter, "2:0");
GtkTreeSelection *selection =
        gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
gtk_tree_selection_select_iter(selection, &iter);

The error is the following warning followed by a segfault:

(treetest:1843): Gtk-CRITICAL **: gtk_tree_store_get_path: assertion `iter->user_data != NULL' failed

(treetest:1843): Gtk-CRITICAL **: IA__gtk_tree_model_get_string_from_iter: assertion `path != NULL' failed

Also, this error never happens when I select the same line with the mouse.

How could it be? The selected path "2:0" is valid, but the iter returned from the selection is not, so I presume the row is not being selected. Why?

È stato utile?

Soluzione

The problem is that the toplevel line is collapsed. Its children rows are hidden and so they cannot be selected.

A solution is to progamatically expand the parent row before selecting one of its children (sscce code here):

// Expanding
GtkTreePath *path = gtk_tree_path_new_from_string("2");
gtk_tree_view_expand_row(GTK_TREE_VIEW(tree_view), path, TRUE);
// Selecting a line
gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(model), &iter, "2:0");
GtkTreeSelection *selection =
        gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
gtk_tree_selection_select_iter(selection, &iter);

Of course, this error just happens when I select the line programatically because it is not possible to select a child of a collapsed row clicking on it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top