c: GtkListStore row-changed event - how can i fetch the data of the selected row?

2k views Asked by At

Hello I use glade to write a small gtk application. the application includes a list store. and whenever a user clicks on a row, i want to be able to fetch the information that this row contains.

using glade under row-changed event i added the following function:

void gtk_tree_view_row_activated_event_handler(GtkWidget *widget, gpointer data) {

    GtkTreeSelection *selection;
    GList *rows_list;

    selection = gtk_tree_view_get_selection(widget);
    rows_list = gtk_tree_selection_get_selected_rows (selection, widget);       
}

how do i continue from here?

1

There are 1 answers

2
serge_gubenko On BEST ANSWER

pls, check if the code below would work for you:

enum
{
    LIST_ITEM = 0,
    N_COLUMNS
};

void view_selected_foreach_func(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer userdata)
{
    char *value;
    gtk_tree_model_get(model, iter, LIST_ITEM, &value, -1);
    g_print("%s is selected\n", value);
    g_free(value);
}

void gtk_tree_view_row_activated_event_handler(GtkWidget *widget, gpointer data)
{
    GtkTreeSelection *selection;
    selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
    gtk_tree_selection_selected_foreach(selection, view_selected_foreach_func, NULL);
}

I guess another way to achieve what you're trying to do would be defining a handler to the GtkTreeSelection changed signal. Below is a small example:

setting up tree view:

list = gtk_tree_view_new();
...
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
g_signal_connect(selection, "changed", G_CALLBACK(on_changed), label);

changed signal handler:

void on_changed(GtkWidget *widget, gpointer label)
{
    GtkTreeIter iter;
    GtkTreeModel *model;
    char *value;

    if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(widget), &model, &iter))
    {
        gtk_tree_model_get(model, &iter, LIST_ITEM, &value,  -1);
        g_print("%s is selected\n", value);
        g_free(value);
    }
}

hope this helps, regards