简体   繁体   English

如何在 GTK4 (>= 4.10) 中构建树

[英]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:我所做的一个非常简短的概述:

  1. Familiarize yourself with how the new dynamic list widgets (Gtk.GridView and Gtk.ColumnView) work for a flat model (GLib.ListStore).熟悉新的动态列表小部件(Gtk.GridView 和 Gtk.ColumnView)如何为平面 model (GLib.ListStore) 工作。 There is example code in the Demo app.演示应用程序中有示例代码。
  2. Create a Gtk.TreeListModelCreateModelFunc function that takes a model item as a parameter and, if that item has children (eg is a folder in a filesystem model), creates and returns a Gtk.ListStore to hold the children.创建一个 Gtk.TreeListModelCreateModelFunc function,它将 model 项作为参数,如果该项有子项(例如,是文件系统模型中的文件夹),则创建并返回一个 Gtk.ListStore 来保存子项。 If the child items are already available then they can also be appended to the child model here.如果子项已经可用,那么也可以在此处将它们附加到子项 model。 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.出于启动性能的原因,您可能希望安排在行实际展开时加载它们,这更加困难 - 在这种情况下,您必须向 model 添加一个虚拟子项,否则将不会显示展开器。
  3. Create a Gtk.SignalListItemFactory.创建一个 Gtk.SignalListItemFactory。
  4. 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.setup处理程序中,像往常一样创建一个显示小部件,而不是使用您的显示小部件作为 Gtk.ListItem 的子项,而是创建一个 Gtk.TreeExpander,并使该子项成为 Gtk.ListItem 并使显示小部件成为扩张器。
  5. 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处理程序中,从提供的 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属性设置为它。
  6. 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 the Gtk.TreeListRow.expanded property to do this.您可以连接到Gtk.TreeListRow.expanded属性的notify信号来执行此操作。
  7. 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).使用带有 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
  8. If you are using a Gtk.ColumnView with multiple columns, you only need add the expander to the first column.如果您使用的是 Gtk.ColumnView 多列,则只需将扩展器添加到第一列。 Otherwise the factory signal handlers or similar to the flat model except the 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM