单元格中的GTK组合框

时间:2018-04-10 04:04:02

标签: c gtk gtk3

我正在编写GUI,在一个窗口中我有一个列表,每行包含3列。

第一列是int - 这里很简单,我给出属性" text"并传递int值。我不确定它是否正确但是有效。

第3列是图标 - 属性是" icon-name",传递带有图标名称的字符串,它工作得非常好。

第二列是一个组合框 - 属性是"模型",传递是一个GTKtreeModel,但它无法正常工作我尝试了不同的变种,但没有任何效果。

所以,想法是使用gtk_cell_renderer_combo_new();传递一个模型并将我的第二列作为组合框。但是现在我得到了这个:

  

GLib-GObject-WARNING **:无法设置属性' model'类型   ' GtkTreeModel'从类型的价值' gchararray'

我没有找到任何关于如何在树作品中制作组合框的材料或文档。有什么想法吗?

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);

2 个答案:

答案 0 :(得分:1)

您需要为组合框模型创建一个新商店。与树视图完全不同的是。然后将此存储设置为组合框模型属性。然后将此行更改为:

GtkTreeViewColumn* type_colomn = gtk_tree_view_column_new_with_attributes("Type",render, "text" , 1, NULL); // name and type - model as GTK doc said it must be model

因为您没有在模型中存储模型。您正在树视图列中呈现树视图模型的文本。

组合框是一个完全独立的对象,在编辑单元格时弹出(两次单击)。

我会发布一些C示例,但我最了解Python并且从未在C中做到这一点。

答案 1 :(得分:0)

感谢@theGtknerd解释究竟要传递给商店模型的内容。 如果有人遇到同样的问题,我在网站上找到了解决方案。使组合框工作的最重要的一行是:

resources/android/icon/drawable-hdpi-icon.png

结果我们有以下专栏:

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

插入新行的示例是:

            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);

帮助我的一个非常好的例子是: GTK cell combo box example