Skip to content

Instantly share code, notes, and snippets.

@peterholzer
Last active January 30, 2022 20:21
Show Gist options
  • Save peterholzer/9872eb6ea731b84b70b0199376fad6fd to your computer and use it in GitHub Desktop.
Save peterholzer/9872eb6ea731b84b70b0199376fad6fd to your computer and use it in GitHub Desktop.
Gtk.ListView with Vala and GTK4
// This example shows how to use the new Gtk.ListView in GTK4.
// Example object, wraps a string inside a GObject
class MyObject : Object {
public string s;
public MyObject(string s) {
this.s = s;
}
}
// Create model and fill with data
var store = new GLib.ListStore(typeof(MyObject));
store.append(new MyObject("bla1"));
store.append(new MyObject("bla2"));
store.append(new MyObject("bla3"));
var selection_model = new Gtk.SingleSelection(store as GLib.ListModel);
// ListItemFactory creates a pool of widgets and fills widgets with data when visible.
// This implementation does not require us to write a dedicated class.
var item_factory = new Gtk.SignalListItemFactory();
item_factory.setup.connect((listitem) => {
listitem.child = new Gtk.Label("MISSINGTEXT");
});
item_factory.bind.connect((listitem) => {
(listitem.child as Gtk.Label).label = (listitem.item as MyObject).s;
});
// Create the main widget
var view = new Gtk.ListView(selection_model, item_factory);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment