How to propertly use Gtk.TreeListModel

I wanted to have a simple list view with a possibility of expanding some of them, but I got stuck on implementing it. I know that there is an example in gtk4 with setting how to use it properly, but for some reason I can’t wrap my head around it.

password_entry.blp

template GtkListItem {
  child: TreeExpander expander {
    child: Label {
      label: bind (GtkListItem.item) as ($PasswordEntry).name;
    };
  };
}
...
ScrolledWindow {
  vexpand: true;

  ListView passwords_list {
    styles ["navigation-sidebar"]

    factory: BuilderListItemFactory {
      resource: "...password_entry.ui";
    };
  }
}
...
class PasswordEntry : GLib.Object {
    public string name {set;get;default = "NAME PLACEHOLDER";}
    public GLib.ListStore children_model {set;get;default = null;}
}

namespace ... {
    public GLib.ListStore get_example_passwords () {
        var list_model = new GLib.ListStore(typeof (PasswordEntry));

        var x = new PasswordEntry () {};
        x.children_model = new GLib.ListStore(typeof (PasswordEntry));
        x.children_model.append ( new PasswordEntry () {} );
        x.name = "X Password";
        list_model.append ( x );

        list_model.append ( new PasswordEntry () {} );
        list_model.append ( new PasswordEntry () {} );

        return list_model;
    }

    [GtkTemplate (ui = ".../window.ui")]
    public class Window : Adw.ApplicationWindow {
        [GtkChild] private unowned Gtk.ListView passwords_list;

        public Window (Gtk.Application app) {
            Object (application: app);

            var list_model = get_example_passwords ();

            var tree_model = new Gtk.TreeListModel(
                list_model, true, false, (entry) => {
                    return ((PasswordEntry) entry).children_model;
                }
            );

            passwords_list.set_model (new Gtk.NoSelection (tree_model));
        }
    }
}

If I run it, I will only get three elements, without any possibility of expanding the firs one.

image

I couldn’t find a way to make it work with blueprint, but I got a way by changing TreeListModel passthrow to false and using xml bellow.

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <requires lib="gtk" version="4.0"/>
  <template class="GtkListItem">
    <property name="child">
      <object class="GtkTreeExpander" id="expander">
        <binding name="list-row">
          <lookup name="item">GtkListItem</lookup>
        </binding>
        <property name="child">
          <object class="GtkLabel">
            <property name="xalign">0</property>
            <binding name="label">
              <lookup name="name" type="PasswordEntry">
                <lookup name="item">expander</lookup>
              </lookup>
            </binding>
          </object>
        </property>
      </object>
    </property>
  </template>
</interface>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.