Search code examples
cgtkgtk3

GTK combo box in cell


I'm writing GUI, and in one window I have a list each row of which consist 3-columns.

1st column is int - here is simple, I give attribute "text" and pass int value. I'm not sure if it correct but it works.

3rd column is icon - Attribute is "icon-name", passing string with a name of icon, it is work very well.

2nd column is a combo box - Attribute is a "model", passing is a GTKtreeModel, but it is not working I tried different variants but nothing work.

So, the idea is using gtk_cell_renderer_combo_new (); pass a model and make my second column as a combo box. However for right now I got this:

GLib-GObject-WARNING **: unable to set property 'model' of type 'GtkTreeModel' from value of type 'gchararray'

I didn't find any material or docs about how to make combo box in tree works. Any ideas?

GtkWidget *type_list = gtk_tree_view_new();  //creating a main list

                    GtkCellRenderer *render;

                    render = gtk_cell_renderer_text_new (); // first column is text  
                    GtkTreeViewColumn* row_n = gtk_tree_view_column_new_with_attributes("#",render,"text",0, NULL); // name and type
                    gtk_tree_view_append_column(GTK_TREE_VIEW(type_list), row_n); //insert attribute into list

                    render = gtk_cell_renderer_combo_new (); //second column is combo
                    GtkTreeViewColumn* type_colomn = gtk_tree_view_column_new_with_attributes("Type",render, "model" , 1, NULL); // name and type - model as GTK doc said it must be model
                    gtk_tree_view_append_column(GTK_TREE_VIEW(type_list), type_colomn); // insert attribute into list

                    render = gtk_cell_renderer_pixbuf_new(); // third column is icon
                    GtkTreeViewColumn* delete_raw = gtk_tree_view_column_new_with_attributes("Delete",render, "icon-name", 2, NULL); // name and type icon-name to pass image from stock
                    gtk_tree_view_append_column(GTK_TREE_VIEW(type_list), delete_raw); // insert attribute into list

                    GtkListStore *store = gtk_list_store_new(3,G_TYPE_INT,G_TYPE_STRING,G_TYPE_STRING); // describe list storage; 3 types, int, string, string, I'm not sure if it correct

              //creating list of options
                    GtkTreeIter itr;
                    gtk_list_store_append(store,&itr);
                    int num = 1;


                    const gchar *type[] = {"1 option", "2 option", "3 option", "4 option", "5 option"};
                    GtkListStore *list = gtk_list_store_new(1,G_TYPE_STRING); //creating list store to pass in combo
                    for (int i=0;i++<4;){   
                        gtk_list_store_insert_with_values(list,NULL,-1, 0,type[i-1],-1); // insert values into list
                    }
          //____________________________
                        //g_object_set (G_OBJECT (render_combo), "model",list,"editable", TRUE,NULL); // unsuccessful try with g_object_set

                    gtk_list_store_set(store, &itr, 0, num, 1,GTK_TREE_MODEL(list), 2, "edit-delete", -1); //insert data to the row

                    gtk_tree_view_set_model(GTK_TREE_VIEW(type_list),GTK_TREE_MODEL(store));

                    g_object_unref (G_OBJECT (store));  // free memory
                    g_object_unref (G_OBJECT (list)); // free memory
                gtk_container_add(GTK_CONTAINER(node_type),type_list);

Solution

  • Thanks @theGtknerd for explaining what exactly to pass in to the store model. I found the solution on the site that I included in the bottom of my answer if someone will have the same problem. The most important line to make combo box work is:

    g_object_set(render, "model", combo_list, "text-column", 0, "has-entry", FALSE, "editable", TRUE, (char *)NULL); 
    

    In result we have the following column:

                render = gtk_cell_renderer_combo_new ();
                //creating a list for combo box
                const gchar *type[] = {"option 1", "option 2", "option 3", "option 4", "option 5"};
                GtkListStore *combo_list = gtk_list_store_new(1,G_TYPE_STRING);   // list for  combo box
                for (int i=0;i++<=4;){  
                    gtk_list_store_insert_with_values(combo_list,NULL,-1, 0,type[i-1],-1);
                }
                //____returning list, free list at line 309
                g_object_set(render, "model", combo_list, "text-column", 0, "has-entry", FALSE, "editable", TRUE, (char *)NULL); 
                //g_signal_connect(render, "editing-started", G_CALLBACK(/*on_tree_cell_combo_editing_start*/), NULL); 
                GtkTreeViewColumn* type_colomn = gtk_tree_view_column_new_with_attributes("Type",render, "text" , 1, NULL);
                gtk_tree_view_append_column(GTK_TREE_VIEW(type_list), type_colomn);
    

    Example of inserting a new line is :

                GtkListStore *store = gtk_list_store_new(3,G_TYPE_UINT,G_TYPE_STRING,G_TYPE_STRING);
    
                GtkTreeIter itr;
                gtk_list_store_append(store,&itr);
                int num = 1;
    
                gtk_list_store_set(store, &itr, 0, num, 1,"char", 2, "edit-delete", -1);
    

    A very good example that helped me is here : GTK cell combo box example