[GTK4] Children are not displayed in a custom widget

I am testing with GTK4 creating Custom Widget, but if I make it derived from GtkWidget the content is not displayed.

For example, I create a Widget derived from GtkBox and everything works fine:

public class BoxSample : Gtk.Box {
        public BoxSample()
        {
            set_orientation (Gtk.Orientation.VERTICAL);
            set_spacing (6);
            for (int i = 0; i < 3; i++)
                this.append (new Gtk.Label("Label #%d".printf(i)));
        }
    }

But if you put a GtkWidget as parent, the content is not displayed

 public class WidgetSample : Gtk.Widget {
        private Gtk.Box box;
     
        public WidgetSample()
        {
            box = new Gtk.Box(Gtk.Orientation.VERTICAL, 6);           
            for (int i = 0; i < 3; i++)
                box.append (new Gtk.Label("Label #%d".printf(i)));                        
            box.set_parent (this);        
        }
    }   

I don’t know if I’m missing a line.
According to the documentation, there is no function to set the child of a widget and in GTK4 the “show” function is no longer needed.

Thank you.

You need to implement measure() and size_allocate() or use an appropriate layout manager.

2 Likes

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