[英]How to build a tree in GTK4 (>= 4.10)
As the document said, TreeView in GTK4 has been deprecated since 4.10.如文档所述,自 4.10 以来,GTK4 中的 TreeView已被弃用。 The recommended replacement for it is ColumnView.推荐的替代品是 ColumnView。 However, I can't find clear documentation or example about how to build a treeView-like widget in GTK4 by ColumnView.但是,我找不到关于如何通过 ColumnView 在 GTK4 中构建类似 treeView 的小部件的明确文档或示例。 I found maybe TreeExpander related to it but I still don't know how to deal with it.我发现可能与它相关的TreeExpander但我仍然不知道如何处理它。
A very brief outline of what I did:我所做的一个非常简短的概述:
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.在setup
处理程序中,像往常一样创建一个显示小部件,而不是使用您的显示小部件作为 Gtk.ListItem 的子项,而是创建一个 Gtk.TreeExpander,并使该子项成为 Gtk.ListItem 并使显示小部件成为扩张器。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
处理程序中,从提供的 object 中获取显示小部件和 model 项。这里的一个复杂问题是 object 可能是一个 Gtk.TreeListRow,其项可能是另一个 Gtk.TreeListRow,因此更难到达 895361。 Bind the widget to the model item as usual.像往常一样将小部件绑定到 model 项目。 Then get the Gtk.TreeListRow from the Gtk.ListItem position and set the expander's list-row
property to it.然后从 Gtk.ListItem position 中获取 Gtk.TreeListRow 并将扩展器的list-row
属性设置为它。notify
signal for the Gtk.TreeListRow.expanded
property to do this.您可以连接到Gtk.TreeListRow.expanded
属性的notify
信号来执行此操作。root_store
as the model of a Gtk.ColumnView (after wrapping in Gtk.SelectionModel as usual).使用带有 GLib.ListStore 的 Gtk.TreeListModel 作为其root_store
作为 Gtk.ColumnView 的 model(像往常一样包装在 Gtk.SelectionModel 之后)。 You must set the passthrough
property to false
else no expanders appear.您必须将passthrough
属性设置为false
,否则不会出现扩展器。 If you are loading child items on demand then autoexpand
should also be false
.如果您按需加载子项目,那么autoexpand
也应该是false
。bind
handler needs to be adapted for Gtk.TreeListRow parameter.否则工厂信号处理程序或类似于平面 model 除了bind
处理程序需要适应 Gtk.TreeListRow 参数。Simplifications and performance improvements may well be possible or later versions of Gtk4 may introduce some conveniences to make it less fiddly.简化和性能改进很可能是可能的,或者 Gtk4 的更高版本可能会引入一些便利以使其不那么繁琐。
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.).我正在使用上述策略开发文件浏览器实现,目前位于https://github.com/elementary/files/tree/gtk4-treelistmodel但这可能会合并到其他分支(或放弃)。 Note that this branch is under development and contains unresolved bugs but the display of items works.请注意,此分支正在开发中并且包含未解决的错误,但项目的显示有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.