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

时间:2013-05-19 01:09:59

标签: 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);
...

2 个答案:

答案 0 :(得分:9)

在原始代码中,您正确地将数据模型绑定到组合框,但未指定如何模型应该呈现(即整个view部分#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; } 3}}概念)。这就是model-view应该提供的内容。

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

#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;
}

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

{{1}}

答案 1 :(得分:2)

我想我找到了解决方案。

我必须在单元格框中插入一个单元格渲染器。我在创建组合框和设置默认活动值之间添加了此代码。

GtkCellRenderer *combocell = gtk_cell_renderer_text_new ();
gtk_cell_layout_pack_start( GTK_CELL_LAYOUT(combo), combocell, TRUE );
gtk_cell_layout_set_attributes( GTK_CELL_LAYOUT(combo), combocell, "text", 0, NULL );

我仍然不明白这一点,我不知道与GtkCellLayout一起使用什么属性或者它们是如何工作的。任何补充阅读将非常感激。