Search code examples
cgtkgtk4

ListView shows undefined characters, GTK4 and c


The new List-Model in GTK4 is a real challenge. Unfortunately I'm currently stuck. The listing below shows my current status.

#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.sstatic.net/P8wKQ.png)

The program compiles without any problems, but then displays undefined characters. The error message is:

a.out:13295): Pango-WARNING **: 19:22:58.287: Invalid UTF-8 string passed to pango_layout_set_text()

Dos anyone have any idea, what mistake I'm making here?


Solution

  • The issue is you are trying to display a GtkStringObject vs getting the value from it in your bind_list_item_cb callback method.

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