Add widget from UI (gtk 4)

Hello
I’m very new at GTK development and I’ve chosen the Vala language.
I have a Gtk.ApplicationWindow containing a Gtk.Box where I want to dynamically add widgets created from template coming from a ui file.
My problem is that I don’t find how to load the UI and create gtk objects from the template inside the ui. I searched in many placed but with no success.

My code is

namespace Gw3t {
    [GtkTemplate (ui = "/fr/ghorin/g3wt/gtk/window.ui")]
    public class Window : Gtk.ApplicationWindow {
        [GtkChild]
        private unowned Gtk.Box box_list_downloads;            // The Gtk.Box where I want to add widgets

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

            var file = File.new_for_path ("widget-download.ui");

            for(int cpt=1; cpt<10; cpt++) {
                box_list_downloads.append( ???? );
            }
        }
    }
}

If somebody could give me some hint, that would be very nice. And sorry if the answer is very easy, I’m very new at programming in Vala + Gtk.

Finally I found the answer by looking at the code of Gnome Boxes and ggitg. I have created a class (in a vala file) that corresponds to the ui template and that implements the gtk objects and related methods. Now I start understanding the way it’s working.

2 Likes

Gtk.Builder is your choice:

  1. Gtk.Builder.Builder.from_file — gtk4 — Valadoc

  2. Gtk.Builder.get_object — gtk4 — Valadoc

  3. gtk-builder-tool could help you validate ui file

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