Search code examples
gtkvalagtk4

error: field ‘parent_instance’ has incomplete type GtkLabel parent_instance;


Problem:

I want to make a Gtk.Label child but it seems I cannot make a child of that type. I checked here to find a possible fix, but I cannot get my head around it. What should I do in my case to fix that problem. I basically only want a button with an extra property. Thanks you!

Error:

error: field ‘parent_instance’ has incomplete type
   64 |         GtkLabel parent_instance;

error: unknown type name ‘GtkLabelClass’
   69 |         GtkLabelClass parent_class;

Snippet:

namespace Redacted {
    public class StyleLabel : Gtk.Widget {
        public StyleLabel(Redacted.Preferences.Styles style) {
            Object (label: style.ToString ());
            this.style = style;
        }

        public Redacted.Preferences.Styles style { get; set; }
    }
}

Solution

  • A lot of widgets in GTK4 have been marked as "final", which means you can't actually derive a subclass from them anymore. You can also see this in the original GtkLabel documentation, where it mentions "final class Gtk.Label".

    GTK4 prefers composition over inheritance (for several reasons) so when you're writing a UI and you write a custom widget, the way to do this in GTK 4 is to put a Gtk.Label inside your widget (using Gtk.Widget.set_parent()). You can find some more explanation and an example at this topic in the GNOME discourse forum.

    (as a side note: technically, you could also write your own version of Gtk.Label, but that would be a lot of work for very little gain)