Can we subclass Labels?

I have a label with some constraints on the content so I’m managing a separate text buffer. I found most functions needing both the label and the buffer so I decided to combine them into a struct:

struct nums_display {
    GtkLabel* display;
    char buffer[NUMS_DISPLAY_LEN];

That worked but, since I’m trying to learn the GTK ecosystem I figured I’d try to subclass the label and add the buffer as a child. When I convert this to a minimal g_object class it fails to compile complaining that there is no GtkLabelClass:

../src/nums_app_display.h:25:17: error: unknown type name ‘GtkLabelClass’
   25 |                 GtkLabel);
      |                 ^~~~~~~~
/usr/include/glib-2.0/gobject/gtype.h:1405:20: note: in definition of macro ‘G_DECLARE_FINAL_TYPE’
 1405 |   typedef struct { ParentName##Class parent_class; } ModuleObjName##Class;                               \
      |                    ^~~~~~~~~~
../src/nums_app_display.c:26:18: error: field ‘parent’ has incomplete type
   26 |         GtkLabel parent;
      |                  ^~~~~~

I found this topic from a year ago that encourages composition which would be my first, working attempt. Am I reading this correct that subclassing most specialized widgets (even labels) is discouraged and this is why my compilation fails?

Yes. In cases such as this, it is even impossible.

Thank you! This saves me some head-to-wall time.

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