2
for(int i=0; i<2; i++) {
    types[i] = G_TYPE_STRING;
}
types[2] = G_TYPE_BOOLEAN;

tree_store = gtk_list_store_newv(3, types);

tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(tree_store));

gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window), tree_view);

GtkTreeViewColumn *column;
GtkCellRenderer *renderer;
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes ("Level",renderer,"text",0,NULL);
gtk_tree_view_column_set_sort_column_id (column, 0);
gtk_tree_view_column_set_resizable (column, true);
gtk_tree_view_append_column (GTK_TREE_VIEW(tree_view), column);

renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes ("URL",renderer,"text",1,NULL);
gtk_tree_view_column_set_sort_column_id (column, 1);
gtk_tree_view_column_set_resizable (column, true);
gtk_tree_view_append_column (GTK_TREE_VIEW(tree_view), column);

renderer = gtk_cell_renderer_toggle_new();
column = gtk_tree_view_column_new_with_attributes ("Image",renderer,"active",2,NULL);
gtk_tree_view_column_set_sort_column_id (column, 2);
gtk_tree_view_column_set_resizable (column, true);
gtk_tree_view_append_column (GTK_TREE_VIEW(tree_view), column);

for(int i=0; i<mapTiles.size(); i++) {
    GtkTreeIter iter;   
    gtk_list_store_append(tree_store, &iter);
    gtk_list_store_set(tree_store, &iter, 0, "A", -1);
    gtk_list_store_set(tree_store, &iter, 1, "B", -1);
    gtk_list_store_set(tree_store, &iter, 2, FALSE, -1);
}

Why is this? I am trying to have a tree view with two columns with strings and a checkbox at the end. Right now, the checkbox shows up but I can't toggle it.

1 Answers1

1

The cell renderer (toggle) just reflects the value, to which it's 'binded', on the model. You must handle the toggled signal with a callback that updates the model from which the cell renderer will read it's state. Check the CellRendererToggle 'toggled' signal reference:

void user_function (GtkCellRendererToggle *cell_renderer, gchar *path, gpointer user_data)

It is the responsibility of the application to update the model with the correct value to store at path . Often this is simply the opposite of the value currently stored at path .

EDIT using the last renderer pointer, set the toggle callback and on the callback change the model to update the value:

[your code]
...
renderer = gtk_cell_renderer_toggle_new();
...
g_signal_connect(G_OBJECT(renderer), "toggled", G_CALLBACK(on_toggle_renderer_toggled), tree_view);
...

void on_toggle_renderer_toggled (GtkCellRendererToggle *cell_renderer, gchar *path, gpointer user_data) {
   gboolean val;
   GtkTreeIter iter;
   GtkTreeModel *model;
   model = gtk_tree_view_get_model (GTK_TREE_VIEW(user_data));
   if (gtk_tree_model_get_iter (model, &iter, path) == false) return;
   gtk_tree_model_get(model, &iter, 2, &val, -1);
   gtk_list_store_set(GTK_LIST_STORE(model), &iter, 2, !val, -1);
}

PS: The above code was not tested, so maybe there are some typos or missing bits.

José Fonte
  • 4,016
  • 2
  • 16
  • 28
  • 1
    Thank you! I can now get a callback function to be called. However, doing cell_renderer->active=true does not cause the checkbox to be clicked. Do you know how to get it to be checked? –  May 05 '17 at 16:22
  • You must change the model, not the cell_renderer. In your callback, set column 2 to the opposite value. The callback gives you the path, from the path the get iter, with the iter get the column 2 actual value (gtk_tree_model_get(...)), then set the value to the opposite (negate it) value with gtk_list_store_set. Remember, the toggle is binded to the column 2 and you can use **user_data** to pass the tree_view (from which you can get the model). – José Fonte May 05 '17 at 16:36
  • 1
    Thanks for the edit guys. I was working and tried to reply quickly. I knew there were some mistakes but didn't have time to fix them. Thanks for contribution. – José Fonte May 06 '17 at 10:41