Re: gtkcombobox with self internal code to the options



Yes Tadej, you understand perfectly.

Thank you for the example!

2010/6/15 Tadej Borovšak <tadeboro gmail com>:
> Hello.
>
> If I understand you correctly, you want to store name and ID of your
> field inside combo box and retrieve it when needed. If this is true,
> have a look at this snippet of code that demonstrates how to do this:
>
> -------
> #include <gtk/gtk.h>
>
> enum
> {
>  COL_NAME,
>  COL_ID,
>  NO_COLS
> };
>
> static void
> cb_changed (GtkComboBox *combo)
> {
>  GtkTreeIter   iter;
>  GtkTreeModel *model;
>  gint          id;
>  gchar        *name;
>
>  gtk_combo_box_get_active_iter (combo, &iter);
>  model = gtk_combo_box_get_model (combo);
>  gtk_tree_model_get (model, &iter, COL_NAME, &name, COL_ID, &id, -1);
>
>  g_print ("Active selection: %s (id: %d)\n", name, id);
>  g_free (name);
> }
>
> int
> main (int    argc,
>      char **argv)
> {
>  GtkWidget       *window,
>                  *combo;
>  GtkCellRenderer *cell;
>  GtkListStore    *store;
>  gint             i;
>
>  gtk_init (&argc, &argv);
>
>  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
>  g_signal_connect (window, "destroy", gtk_main_quit, NULL);
>
>  store = gtk_list_store_new (NO_COLS, G_TYPE_STRING, G_TYPE_INT);
>  for (i = 0; i < 10; i++)
>    {
>      GtkTreeIter iter;
>      gchar       name[] = "Item  ";
>
>      name[5] = '0' + i;
>      gtk_list_store_append (store, &iter);
>      gtk_list_store_set (store, &iter, COL_NAME, name,
>                                        COL_ID, g_random_int (),
>                                        -1);
>    }
>
>  combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
>  g_signal_connect (combo, "changed", G_CALLBACK (cb_changed), NULL);
>  gtk_container_add (GTK_CONTAINER (window), combo);
>
>  cell = gtk_cell_renderer_text_new ();
>  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
>  gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo), cell,
>                                 "text", COL_NAME);
>
>  gtk_widget_show_all (window);
>
>  gtk_main();
>
>  return 0;
> }
> ----------
>
> Tadej
>
> --
> Tadej Borovšak
> tadeboro.blogspot.com
> tadeboro gmail com
> tadej borovsak gmail com
>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]