How to use GListStore

Documentation:
https://docs.gtk.org/gio/ctor.ListStore.new.html

Hello,
I’m migrating a application from GTK3 to GTK4.

my old GTK3 Code uses as data storage model (to save character strings) the following code:

list_articles = gtk_list_store_new(17, G_TYPE_STRING, G_TYPE_STRING, ...);

in GTK4 i have to use the GListStore-Object and i want to create it with the following function, but that’s not working:

GListStore *list = g_list_store_new(G_TYPE_STRING);

error message:

g_list_store_new: assertion 'g_type_is_a (item_type, G_TYPE_OBJECT)' failed

Can anyone tell me a working example for creating a new GListStore with items, which can save character strings? What kind of GType item_type must i use?

Thanks in advance.

You can only use GObject as the data inside a store.

If your old GtkListStore used multiple columns, the appropriate way to migrate to GListStore and to the new list view widgets in GTK4, is to create an object that represents the row data, with properties representing the state and data of the row. For instance, something like:

enum { COL_NAME, COL_SURNAME, COL_AGE, COL_ADDRESS, N_COLUMNS };

GtkListStore *people_model =
  gtk_list_store_new (N_COLUMNS,
                      G_TYPE_STRING, /* COL_NAME */
                      G_TYPE_STRING, /* COL_SURNAME */
                      G_TYPE_UINT, /* COL_AGE */
                      G_TYPE_STRING /* COL_ADDRESS */);

Becomes:

G_DECLARE_FINAL_TYPE (FooPerson, foo_person, FOO, PERSON, G_TYPE_OBJECT)

// ... GObject code to define the following properties:
// - name (string)
// - surname (string)
// - age (uint)
// - address (string

GListStore *people_model = g_list_store_new (FOO_TYPE_PERSON);

Now every row in the model will be an object of the same type, and you can use the type to access the relevant data.

The next step is creating a row widget, a widget that acts as a view to the row object’s model. You can use composite templates to build a composite widget with UI definition files and your custom widget type; and property bindings to bind properties on the row object to properties on the row widget, so that changing the row object in the model will automatically update the row widget in the view.

3 Likes

Hello, thank you for your fast reply.

Currently i’m trying to understand the new way to get it running - however it looks for me totally different and i haven’t found a GTK4-tutorial like the GTK3-treeview-“hello-world”-tutorial , which helped me a lot to get a minimal code example to work.

(1) Which is the correct ParentName in G_DECLARE_FINAL_TYPE?

G_TYPE_OBJECT creates a compiler error, but GObject works:

G_DECLARE_FINAL_TYPE (FooPerson, foo_person, FOO, PERSON, GObject)

documentation: https://docs.gtk.org/gobject/func.DECLARE_FINAL_TYPE.html

Ah, I made a mistake indeed; I mixed G_DECLARE_FINAL_TYPE with G_DEFINE_TYPE. The appropriate declaration is:

G_DECLARE_FINAL_TYPE (FooPerson, foo_person, FOO, PERSON, GObject)

in the header file, coupled with:

G_DEFINE_FINAL_TYPE (FooPerson, foo_person, G_TYPE_OBJECT)

in the source file.

For more information, see the GObject tutorial.

1 Like

Ok, that’s how far i’m at now.

Next is to find the solution how to define the properties for the GObject. I will try :face_with_monocle:

The GObject tutorial also shows you how to do that.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.