0
votes

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?

1

1 Answers

2
votes

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.