GTK4 in C: Widget templates nesting where the UI is spread in several XML *.ui files

,

Hi,

I am currently trying to do an application with GTK4, and I would like to use composite widgets, following guidance from the Widget Templates tutorial, which was already a big help.

I have an application window in the same manner as the one indicated in the GTK4 Getting started. It is associated with a first xml file “window.ui”.

In this first template, I would like to nest a custom widget, which should be built from another template “widget.ui”. The widget.ui contains the same description as the one used in the Widget templates tutorial.

The full code is available in this git repository

When the project is built and the application executed, it gives me the following error messages:

Gtk-CRITICAL **: Unable to retrieve child object 'examplewidget' from class template for type 'ExampleAppWindow' while building a 'ExampleAppWindow'

Gtk-WARNING **:  Trying to snapshot GtkEntry 0x5568976b63e0 without a current allocation

Gtk-WARNING **:  Trying to snapshot GtkButton 0x556897512300 without a current allocation

Here is some excerpts of relevant files:

window.ui (the custom widget is the child ExampleWidget)

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <template class="ExampleAppWindow" parent="GtkApplicationWindow">
    <property name="title" translatable="yes">Example Application</property>
    <property name="default-width">600</property>
    <property name="default-height">400</property>
    <child>
      <object class="GtkBox" id="content_box">
        <property name="orientation">vertical</property>
        <child>
            <object class="GtkLabel" id="io_channel_text">
              <property name="label">Input and output channels:</property>
            </object>
        </child>
        <child>
            <object class="ExampleWidget">
            </object>
        </child>
      </object>
    </child>
  </template>
</interface>

The code for the custom widget in examplewidget.c is identical to the one from the widget templates tutorial.

exampleappwin.c

#include <gtk/gtk.h>

#include "exampleapp.h"
#include "exampleappwin.h"
#include "examplewidget.h"

struct _ExampleAppWindow
{
  GtkApplicationWindow parent;

  GtkWidget* examplewidget;
};

G_DEFINE_TYPE(
  ExampleAppWindow,
  example_app_window,
  GTK_TYPE_APPLICATION_WINDOW);

static void
example_app_window_init(ExampleAppWindow* win) {
  g_type_ensure(EXAMPLE_TYPE_WIDGET);
  gtk_widget_init_template(GTK_WIDGET(win));
}

static void
example_app_window_class_init(ExampleAppWindowClass* class) {
  gtk_widget_class_set_template_from_resource(
    GTK_WIDGET_CLASS(class), "/org/gtk/exampleapp/window.ui");

  gtk_widget_class_bind_template_child(
    GTK_WIDGET_CLASS(class), ExampleAppWindow, examplewidget);
}

ExampleAppWindow*
example_app_window_new(ExampleApp* app) {
  return g_object_new(EXAMPLE_APP_WINDOW_TYPE, "application", app, NULL);
}

I suspect I would have to add something in example_app_window_init but I have unfortunately no clue. The Widget Templates tutorial is silent on this aspect.

Any help would be greatly appreciated. Thanks for having taken the time to read.

You are getting this error because there is no object with id ‘examplewidget’ in your window.ui file.

1 Like

Thanks @mazharhussain !

Indeed, this enabled to get rid of the critical error.
For the two remaining ones, I changed the parent of ExampleWidget from a generic GtkWidget to a more specific GtkBox. I can start to enjoy making my first callbacks now.

Cheers

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