ListView 显示未定义的字符、GTK4 和 c

问题描述 投票:0回答:1

GTK4 中的新列表模型是一个真正的挑战。不幸的是我目前 卡住。 下面的列表显示了我目前的状态。

#include <gtk/gtk.h>

static GListModel* create_model()
{

        GListStore *store;
        store = g_list_store_new(G_TYPE_OBJECT);
        g_list_store_append(store, gtk_string_object_new("London"));
        g_list_store_append(store, gtk_string_object_new("Paris"));
        g_list_store_append(store, gtk_string_object_new("Berlin"));

        return G_LIST_MODEL (store);
}

static void setup_list_item_cb (GtkListItemFactory *factory, GtkListItem *list_item)
{
        GtkWidget *label = gtk_label_new (NULL);
        gtk_list_item_set_child (GTK_LIST_ITEM(list_item), label);
}

static void bind_list_item_cb (GtkListItemFactory *factory, GtkListItem  *list_item)
{
        GtkWidget *label = gtk_list_item_get_child (list_item);
        const char *string = gtk_list_item_get_item(list_item);
        gtk_label_set_text (GTK_LABEL (label), string);
}

static void activate (GtkApplication *app, gpointer user_data)
{
        GtkWidget *window; GtkWidget *scrolled_window;
        GtkWidget *list_view;
        GtkListItemFactory *factory;
        GListModel *model;

        model = create_model();
        GtkSingleSelection *selection;
        selection = gtk_single_selection_new(G_LIST_MODEL(model));
        gtk_single_selection_set_autoselect(selection,TRUE);

        window = gtk_application_window_new (app);
        gtk_window_set_title (GTK_WINDOW (window), "ListView Example");
        gtk_window_set_default_size (GTK_WINDOW (window), 300, 200);
        scrolled_window = gtk_scrolled_window_new ();
        gtk_window_set_child (GTK_WINDOW (window), scrolled_window);
                                                                                                                                                                     
        list_view = gtk_list_view_new (GTK_SELECTION_MODEL(selection), factory);
        gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scrolled_window), list_view);

        factory = gtk_signal_list_item_factory_new ();
        g_signal_connect (factory, "setup", G_CALLBACK (setup_list_item_cb), NULL);
        g_signal_connect (factory, "bind", G_CALLBACK (bind_list_item_cb), NULL);
        gtk_list_view_set_factory (GTK_LIST_VIEW (list_view), factory);
        gtk_window_present (GTK_WINDOW (window));
}

int main (int argc, char **argv)
{
        GtkApplication *app; int status;

        app = gtk_application_new ("org.gtk.listview", G_APPLICATION_DEFAULT_FLAGS);
        g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
        status = g_application_run (G_APPLICATION (app), argc, argv); g_object_unref (app);
        return status;
}
                                                                                                                                                                      

[The poor resultat](https://i.stack.imgur.com/P8wKQ.png)

程序编译没有任何问题,但随后显示未定义的字符。 错误信息是:

a.out:13295): Pango-警告 **: 19:22:58.287: 传递给 pango_layout_set_text() 的 UTF-8 字符串无效

有人知道我在这里犯了什么错误吗?

c gtk gtk4
1个回答
0
投票

问题是您正在尝试显示

GtkStringObject
与在
bind_list_item_cb
回调方法中从中获取值。

static void bind_list_item_cb (GtkListItemFactory *factory, GtkListItem  *list_item)
{
        GtkWidget *label = gtk_list_item_get_child (list_item);
        // get the string object from the model
        GtkStringObject *str = gtk_list_item_get_item(list_item);
        // extract the string value from the object
        const char *string = gtk_string_object_get_string(str);
        g_printerr("Item: %s \n", string);
        gtk_label_set_text (GTK_LABEL (label), string);
}


© www.soinside.com 2019 - 2024. All rights reserved.