Search code examples
cgtkgtk3glade

How to get the data of an activated row GTK+ C


After the "row-activated" signal is activated , how to get the data of the selected row ? I'm using C language .

void on_treeview1_row_activated()
{
//I want to get the data here
}

Solution

  • The callback prototype for the row-activated signal should be:

    void user_function (GtkTreeView       *tree_view,
                        GtkTreePath       *path,
                        GtkTreeViewColumn *column,
                        gpointer           user_data)
    

    This means that you will have references to the treeview (and related model/store) and the selected path. This should be enough to extract data from the activated row.

    Supposing that your model/store has as first column some integers as ID and a second column with strings:

    | ID |  TEXT      |
    +----+------------+
    | 12 | John Doe   |
    ...
    | 35 | Whatever   |
    +----+------------|
    

    So your callback function should be something like this:

    void on_treeview1_row_activated(GtkTreeView       *treeview, 
                                    GtkTreePath       *path, 
                                    GtkTreeViewColumn *column,
                                    gpointer userdata) {
    
       gint int_data;
       gchar *str_data;
    
       GtkTreeIter iter;
       GtkTreeModel *model = gtk_tree_view_get_model(treeview);
    
       if (gtk_tree_model_get_iter(model, &iter, path)) {
          gtk_tree_model_get (GTK_LIST_STORE(model), &iter, 0, &int_data, 1, &str_data, -1));
          // Here the variables int_data and str_data should be filled with
          // relevant data
       }
    }