尝试使用C中的模型填充GtkComboBox

bea*_*roo 3 c gtk

我是C和GTK +的新手,所以我的问题可能很明显.我试图按照网络上的示例和教程进行操作.

我想要一个包含三个值的组合框,中间一个是默认值.我可以使用Glade成功设置它,但我决定用C重写所有内容.组合框被绘制但它是空的/空白.我不知道我做错了什么.

...
GtkTreeIter iter;
GtkListStore *liststore;
GtkWidget *combo;

liststore = gtk_list_store_new(1, G_TYPE_STRING);
gtk_list_store_insert_with_values (liststore, &iter, 0, 0, "Don't install.", -1);
gtk_list_store_insert_with_values (liststore, &iter, 1, 0, "This user only.", -1);
gtk_list_store_insert_with_values (liststore, &iter, 2, 0, "All users.", -1);

combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(liststore));
gtk_combo_box_set_active (GTK_COMBO_BOX(combo), 1);
...
gtk_grid_attach (GTK_GRID(grid), combo, 2, 4, 1, 1);
...
Run Code Online (Sandbox Code Playgroud)

ntd*_*ntd 9

在你原来的代码,你正确绑定数据模型组合框,但你并没有规定如何模型应提交(即是view整体的一部分model-view概念).这就是GtkCellLayout应该提供的内容.

为了让您了解为什么这种增加的复杂性很有用,这里有一个示例,说明如何使用模型来获得自定义背景(这是糟糕的用户体验,根据您的主题,可以完全忽略背景颜色).我认为最困难的事情是避免内存泄漏,所以我在这方面添加了一些评论:

#include <gtk/gtk.h>

int main(int argc, char **argv)
{
    GtkWidget *window;
    GtkListStore *liststore;
    GtkWidget *combo;
    GtkCellRenderer *column;

    gtk_init(&argc, &argv);

    liststore = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING);
    gtk_list_store_insert_with_values(liststore, NULL, -1,
                                      0, "red",
                                      1, "Don't install.",
                                      -1);
    gtk_list_store_insert_with_values(liststore, NULL, -1,
                                      0, "green",
                                      1, "This user only.",
                                      -1);
    gtk_list_store_insert_with_values(liststore, NULL, -1,
                                      0, "yellow",
                                      1, "All users.",
                                      -1);

    combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(liststore));

    /* liststore is now owned by combo, so the initial reference can
     * be dropped */
    g_object_unref(liststore);

    column = gtk_cell_renderer_text_new();
    gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), column, TRUE);

    /* column does not need to be g_object_unref()ed because it
     * is GInitiallyUnowned and the floating reference has been
     * passed to combo by the gtk_cell_layout_pack_start() call. */

    gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), column,
                                   "cell-background", 0,
                                   "text", 1,
                                   NULL);

    gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 1);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_container_add(GTK_CONTAINER(window), combo);

    /* Also combo is GInitiallyUnowned and it is now owned
       by window after the gtk_container_add() call. */

    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,如果您打算在组合框中仅使用字符串,则可以通过利用GtkComboBoxText来删除代码:

#include <gtk/gtk.h>

int main(int argc, char **argv)
{
    GtkWidget *window, *combo;

    gtk_init(&argc, &argv);

    combo = gtk_combo_box_text_new();
    gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(combo), NULL, "Don't install.");
    gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(combo), NULL, "This user only.");
    gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(combo), NULL, "All users");
    gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 1);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_container_add(GTK_CONTAINER(window), combo);

    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)