Adding GtkButton to GtkListStore with text in GTK+

最后都变了- 提交于 2019-12-14 02:27:33

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/43050975/adding-gtkbutton-to-gtkliststore-with-text-in-gtk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!