How to expose custom widget class to .ui inside a template widget?

,

I have read the documentation for GtkWidget and GtkBuilder, GObject tutorial, GObject Type System Concepts, “Widget Templates” tutorial on GNOME Development website, and source code of gnome-console. I still cannot figure out why a custom widget class I created cannot be used inside a template widget’s ui file.

The specific error I get is

(skipper:48484): Gtk-CRITICAL **: 11:30:00.993: Error building template class 'SkipperWindow' for an instance of type 'SkipperWindow': .:0:0 Invalid object type 'SkipperHeaderBar'

Edit: SkipperHeaderBar can be instantiated and used from inside the init function of SkipperWindow but just not from its .ui file.

By the way, both SkipperWindow and SkipperHeaderBar are template widgets and all source (.c) files (plus resources) are compiled into a single executable file.

What am I missing?

Source Code

GtkBuilder can only construct objects of known types, that is, after a type has been registered with the type system.

Calling

  g_type_ensure (SKIPPER_TYPE_HEADER_BAR);

early enough (before g_application_run() or when handling startup) should fix it.

3 Likes

It works.

But other projects (e.g. gnome-console) work fine without it. What is going on there?

Every project needs to do this, since it’s fundamentally how the type system and GtkBuilder work. Some language bindings might do the type registration behind the scenes, but it has to happen.

Yes, but it is done automatically. Isn’t it?

I don’t see any code in gnome-console calling g_type_ensure() explicitly.

It is only done “automatically” for types provided by GTK and by libadwaita, or any library that has an initialisation function and that registers types in that function.

I just checked, and gnome-console definitely calls g_type_ensure():

kgx-window.c
490:  g_type_ensure (KGX_TYPE_TAB_BUTTON);
491:  g_type_ensure (KGX_TYPE_TAB_SWITCHER);
492:  g_type_ensure (KGX_TYPE_THEME_SWITCHER);

kgx-application.c
103:  g_type_ensure (KGX_TYPE_TERMINAL);
104:  g_type_ensure (KGX_TYPE_PAGES);
1 Like

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