1

I want a button with text on it for a GtkListStore. I read another answer using an image as a button, but I really need the title to be text. How can I do this? I'd be fine with a solution that renders text onto a GdkPixbuf as well.

I've tried this:

GType *types;

types = g_new0 (GType, num_fields);

for(int i=0; i<num_fields; i++) {
    types[i] = GTK_TYPE_BUTTON;
}

tree_store = gtk_list_store_newv(num_fields, types);
tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(tree_store));

GtkTreeViewColumn *column;
    GtkCellRenderer *renderer;
    GdkPixbuf     *icon;
    renderer = gtk_cell_renderer_pixbuf_new();
    column = gtk_tree_view_column_new_with_attributes (name.c_str(),renderer,"pixbuf",i,NULL);

button = gtk_button_new_with_label ("Quit");
        g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
        gtk_widget_set_can_default (button, TRUE);
        gtk_list_store_set(tree_store, &iter, j, button, -1);

There are no errors, but nothing shows up.

I want to view the selection in a new window.

  • Are you trying to add a button to the TreeViewColumn header or something? Your question is hard to understand. – theGtknerd Mar 29 '17 at 02:58
  • @theGtknerd, not to the header, but to one of the columns contents. Comparable to having a table which contains buttons. –  Mar 31 '17 at 16:08
  • 1
    This question comes up regularly. Gtk has no CellRendererButton for reasons [here](https://ubuntuforums.org/archive/index.php/t-1009065.html). Basically, you should put buttons outside the treeview. Say the user wants to cancel 10 items. He doesn't want to click a cancel button 10 times. He wants to select 10 items and click cancel. – theGtknerd Mar 31 '17 at 21:30
  • 1
    @theGtknerd what about using gtk_cell_renderer_activate? I found it in [this] (http://stackoverflow.com/questions/43053242/how-to-connect-gtkcellrenderer-to-callback-function) question but couldn't find an example of how to use it. –  Apr 03 '17 at 13:01
  • There is a good reason you can't figure out how to use it. According to your question, you don't seem to have a specific purpose in mind. And it does not help to cross post two times. Please edit your question to clarify what you want to do. Say `cancel the selection` or maybe `view the selection in a new window`. – theGtknerd Apr 03 '17 at 20:10
  • @theGtknerd updated. –  May 01 '17 at 14:38

1 Answers1

0

You have two options (actually you have more than two options, but you need to try the first two before I continue).

Hook up to the treeview "row_activated" signal, which can be set to single or double click. This will pass in the path to the selected row.

Put a button below/outside the treeview that gets a treeview "get_selected_row". You can then use this to get the content of the row you wish to open in a new window. Example. Hint: this is how Gtk recommends using buttons with a treeview.

Community
  • 1
  • 1
theGtknerd
  • 3,647
  • 1
  • 13
  • 34