Search code examples
ccomboboxgtkglade

c gtk+-2 glade3: adding text to a combobox loaded from Gtk Builder


I'm learning GTK+2 and glade 3. I'm trying to add a text to a combobox component i loaded from Gtk Builder.

i use the following code:

output_right_combobox = GTK_WIDGET(gtk_builder_get_object(builder,"output_right_combobox"));
gtk_combo_box_append_text (GTK_COMBO_BOX(output_left_combobox),"aaa");  

I get the following critical error on the gtk_combo_box_append_text line:

Gtk-CRITICAL **: IA__gtk_combo_box_append_text: assertion `GTK_IS_LIST_STORE (combo_box->priv->model)' failed

It semes that I need to use GtkListStore for that, but I cannot find a way to use it and add it to the combobox. any ideas ?

update

I tried doing the following:

    GtkTreeIter iter;
GtkListStore *store = gtk_list_store_new (1, G_TYPE_STRING);
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, 0, "foo", -1);

gtk_combo_box_set_model (GTK_COMBO_BOX(output_right_combobox), (GtkTreeModel *)store);

but the combobox list in the application is still empty.

thanks!


Solution

  • You need to add a GtkCellRenderer to your combobox to get it to render the text:

    gtk_combo_box_set_model (GTK_COMBO_BOX (output_right_combobox), GTK_TREE_MODEL(store));
    
    GtkCellRenderer * cell = gtk_cell_renderer_text_new();
    gtk_cell_layout_pack_start( GTK_CELL_LAYOUT( output_right_combobox ), cell, TRUE );
    gtk_cell_layout_set_attributes( GTK_CELL_LAYOUT( output_right_combobox ), cell, "text", 0, NULL );