As the document said, TreeView in GTK4 has been deprecated since 4.10. The recommended replacement for it is ColumnView. However, I can't find clear documentation or example about how to build a treeView-like widget in GTK4 by ColumnView. I found maybe TreeExpander related to it but I still don't know how to deal with it.
Asked
Active
Viewed 623 times
5
-
It's a bit weird, in your link, the GTK library version is set to 4.9.2, but some things are said to be deprecated in GTK 4.10. I found no GTK 4.10 version in their repo, it seems they are not yet there... – BobMorane Nov 26 '22 at 17:19
-
It seems that 4.10 is a future release. So they are planning on making these deprecated in next releases. – BobMorane Nov 26 '22 at 18:17
-
Haha, so gtk is planning to deprecate this feature. But it is kind of weird that I can't find function `row_activaed`. – Dorbmon Nov 28 '22 at 02:21
-
What do you mean by "can't find"? It's [here](https://docs.gtk.org/gtk4/method.TreeView.row_activated.html) in the docs. Do you mean in the code? – BobMorane Nov 28 '22 at 13:56
-
Yes. I can find it in the docs but unable to find it in the gtkmm library.... So strange lol – Dorbmon Nov 29 '22 at 14:05
1 Answers
3
A very brief outline of what I did:
- Familiarize yourself with how the new dynamic list widgets (Gtk.GridView and Gtk.ColumnView) work for a flat model (GLib.ListStore). There is example code in the Demo app.
- Create a Gtk.TreeListModelCreateModelFunc function that takes a model item as a parameter and, if that item has children (e.g. is a folder in a filesystem model), creates and returns a Gtk.ListStore to hold the children. If the child items are already available then they can also be appended to the child model here. This means the child items are preloaded whether or not the row is expanded. For startup performance reasons, you might want to arrange to load them when the row is actually expanded which is more difficult - in this case you have to add a dummy child to the model else the expander will not be displayed.
- Create a Gtk.SignalListItemFactory.
- In the
setup
handler, create a display widget as usual then instead of using your display widget as child of the Gtk.ListItem, you create a Gtk.TreeExpander, and make that the child the Gtk.ListItem and make the display widget a child of the expander. - In the
bind
handler, get the display widget and model item from the supplied object. A complication here is that the object may be a Gtk.TreeListRow whose item might be another Gtk.TreeListRow so getting to the model item is harder. Bind the widget to the model item as usual. Then get the Gtk.TreeListRow from the Gtk.ListItem position and set the expander'slist-row
property to it. - If you delayed loading the child items then you would need to detect when the row is first expanded. You can connect to the
notify
signal for theGtk.TreeListRow.expanded
property to do this. - Use a Gtk.TreeListModel with a GLib.ListStore as its
root_store
as the model of a Gtk.ColumnView (after wrapping in Gtk.SelectionModel as usual). You must set thepassthrough
property tofalse
else no expanders appear. If you are loading child items on demand thenautoexpand
should also befalse
. - If you are using a Gtk.ColumnView with multiple columns, you only need add the expander to the first column. Otherwise the factory signal handlers or similar to the flat model except the
bind
handler needs to be adapted for Gtk.TreeListRow parameter.
Simplifications and performance improvements may well be possible or later versions of Gtk4 may introduce some conveniences to make it less fiddly.
I am working on a file browser implementation using the above strategy, currently at https://github.com/elementary/files/tree/gtk4-treelistmodel but this may be merged into other branches (or abandoned!). Note that this branch is under development and contains unresolved bugs but the display of items works.

Jeremy Wootten
- 101
- 3