Last active
January 30, 2022 20:21
-
-
Save peterholzer/9872eb6ea731b84b70b0199376fad6fd to your computer and use it in GitHub Desktop.
Gtk.ListView with Vala and GTK4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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