gtk - Trying to populate a GtkComboBox with model in C -
i'm new c , gtk+ problem might painfully obvious. have tried follow examples , tutorials found on net.
i want combo box 3 values, middle 1 being default. can set using glade have decided rewrite in c. combobox drawn empty/blank. don't know doing wrong.
... gtktreeiter iter; gtkliststore *liststore; gtkwidget *combo; liststore = gtk_list_store_new(1, g_type_string); gtk_list_store_insert_with_values (liststore, &iter, 0, 0, "don't install.", -1); gtk_list_store_insert_with_values (liststore, &iter, 1, 0, "this user only.", -1); gtk_list_store_insert_with_values (liststore, &iter, 2, 0, "all users.", -1); combo = gtk_combo_box_new_with_model(gtk_tree_model(liststore)); gtk_combo_box_set_active (gtk_combo_box(combo), 1); ... gtk_grid_attach (gtk_grid(grid), combo, 2, 4, 1, 1); ...
in original code correctly bound data model combo box did not specified how model should presented (that view
part of whole model-view
concept). gtkcelllayout supposed provide.
to give idea on why added complexity useful, here example shows how use model have custom background (this bad ux and, depending on theme, background color can totally ignored). think difficult thing avoiding memory leaks, added comment on regard:
#include <gtk/gtk.h> int main(int argc, char **argv) { gtkwidget *window; gtkliststore *liststore; gtkwidget *combo; gtkcellrenderer *column; gtk_init(&argc, &argv); liststore = gtk_list_store_new(2, g_type_string, g_type_string); gtk_list_store_insert_with_values(liststore, null, -1, 0, "red", 1, "don't install.", -1); gtk_list_store_insert_with_values(liststore, null, -1, 0, "green", 1, "this user only.", -1); gtk_list_store_insert_with_values(liststore, null, -1, 0, "yellow", 1, "all users.", -1); combo = gtk_combo_box_new_with_model(gtk_tree_model(liststore)); /* liststore owned combo, initial reference can * dropped */ g_object_unref(liststore); column = gtk_cell_renderer_text_new(); gtk_cell_layout_pack_start(gtk_cell_layout(combo), column, true); /* column not need g_object_unref()ed because * ginitiallyunowned , floating reference has been * passed combo gtk_cell_layout_pack_start() call. */ gtk_cell_layout_set_attributes(gtk_cell_layout(combo), column, "cell-background", 0, "text", 1, null); gtk_combo_box_set_active(gtk_combo_box(combo), 1); window = gtk_window_new(gtk_window_toplevel); gtk_container_add(gtk_container(window), combo); /* combo ginitiallyunowned , owned window after gtk_container_add() call. */ gtk_widget_show_all(window); gtk_main(); return 0; }
but if intend use strings in combo box, code can stripped down leveraging gtkcomboboxtext:
#include <gtk/gtk.h> int main(int argc, char **argv) { gtkwidget *window, *combo; gtk_init(&argc, &argv); combo = gtk_combo_box_text_new(); gtk_combo_box_text_append(gtk_combo_box_text(combo), null, "don't install."); gtk_combo_box_text_append(gtk_combo_box_text(combo), null, "this user only."); gtk_combo_box_text_append(gtk_combo_box_text(combo), null, "all users"); gtk_combo_box_set_active(gtk_combo_box(combo), 1); window = gtk_window_new(gtk_window_toplevel); gtk_container_add(gtk_container(window), combo); gtk_widget_show_all(window); gtk_main(); return 0; }
Comments
Post a Comment