GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Jul 22, 2014 3:31 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: gtk combobox with a model, retrieve the content of a cell
PostPosted: Fri Jan 11, 2013 11:33 am 
Offline
Familiar Face

Joined: Fri Oct 26, 2012 10:49 am
Posts: 35
gtk-2.0
Hello!
I have a combobox that I made with a model, 2 columns, the first one contains text, the second one a icon.
I want to retrieve the text
I connected the change signal with cb_on_cboxChuck_changed()
My problem is to get the value of the text-cell of the active combobox items.
the function "gtk_combo_box_get_active_text" doesn't work, may be because there are two columns.

How could I get the value of the first column?
Thanks a lot for your help in advance
M.Boerner
callback of the changed signal
Code:
static gboolean cb_on_cboxChuck_changed(GtkComboBox *widget, gpointer  user_data)
{
   char *p_newChuckIcon;

   p_newChuckIcon = gtk_combo_box_get_active_text(widget);  //==>  doesn't work
   strcpy(recipe_editBuffer.header.chuck_type, p_newChuckIcon);

   return FALSE;
}

code to connect the changed signal:
Code:
g_signal_connect(rech_local.widgets.cboxChucks, "changed", G_CALLBACK ( cb_on_cboxChuck_changed), NULL);


code to make the model of the combobox:
Code:
static void set_up_cbo_chuckname(GtkWidget *combo)
{
    GtkTreeViewColumn *col;
    GtkWidget *view;
    GtkListStore *liststore;
    GtkCellLayout *layout;
    GtkCellRenderer *renderer;

    /* Create a new GtkListStore with 1 string column and 1 pixbuf column... */

    liststore = gtk_list_store_new( 2, G_TYPE_STRING, GDK_TYPE_PIXBUF );
    rech_local.recipeNrModel = fill_cboChuck_model (liststore);
    /* ...and set it as the combo box's model */
    gtk_combo_box_set_model(GTK_COMBO_BOX(combo), GTK_TREE_MODEL(liststore));
    layout = GTK_CELL_LAYOUT(combo);

    /* text cell */
    view = gtk_tree_view_new();
    col = gtk_tree_view_column_new();
    gtk_tree_view_column_set_min_width  (col,100);
    gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);

    renderer = gtk_cell_renderer_text_new();
    gtk_cell_layout_pack_start(layout, renderer,FALSE);
    gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo),renderer,"text",COL_CHUCKNAME,NULL);

    /* icon cell */
    view = gtk_tree_view_new();
    col = gtk_tree_view_column_new();
    gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);

    renderer = gtk_cell_renderer_pixbuf_new();
    gtk_cell_layout_pack_start( GTK_CELL_LAYOUT(combo), renderer, FALSE );
    gtk_cell_layout_set_attributes( GTK_CELL_LAYOUT(combo), renderer, "pixbuf", COL_CHUCKICON, NULL );

    gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(view)),GTK_SELECTION_NONE);
}

I filled the combo box with the following command:
Code:
gtk_list_store_set(liststore,&toplevel,   0, name,     1,pixbuf,-1);// 1 coulmn is a nam, the second one is a picture


Top
 Profile  
 
 Post subject: Re: gtk combobox with a model, retrieve the content of a cel
PostPosted: Fri Jan 11, 2013 8:46 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
From the documentation for gtk_combo_box_get_active_text:
Quote:
Note that you can only use this function with combo boxes constructed with gtk_combo_box_new_text() and with GtkComboBoxEntrys.

This function was done away with in gtk3 (likely for this reason, i.e. frequent buggy code) so it's something to avoid anyway.
An alternative (untested so you may have to play around a bit) is:
Code:
GtkTreeIter iter;
gchar* text=NULL;
GtkTreeModel* model=gtk_combo_box_get_model(combo_box);

if (gtk_combo_box_get_active_iter(combo_box, &iter)) gtk_tree_model_get(model, &iter, 0, &text, -1);


Top
 Profile  
 
 Post subject: Re: gtk combobox with a model, retrieve the content of a cel
PostPosted: Mon Jan 14, 2013 6:50 am 
Offline
Familiar Face

Joined: Fri Oct 26, 2012 10:49 am
Posts: 35
works wonderful, thank you!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group