Populating the window

In this step, we use a Gtk::Builder instance to associate a Gtk::Builder ui file with our application window class.

Our simple ui file gives the window a title, and puts a Gtk::Stack widget as the main content.

To make use of this file in our application, we revisit our Gtk::ApplicationWindow subclass, and call Gtk::Builder::create_from_resource() and Gtk::Builder::get_widget_derived() from the ExampleAppWindow::create() method to get an instance of our subclassed Gtk::ApplicationWindow. See the Using derived widgets section for more information about get_widget_derived().

You may have noticed that we use the _from_resource() variant of the method that reads the ui file. Now we need to use GLib's resource functionality to include the ui file in the binary. This is commonly done by listing all resources in a .gresource.xml file. This file has to be converted into a C source file that will be compiled and linked into the application together with the other source files. To do so, we use the glib-compile-resources utility:

$ glib-compile-resources --target=resources.c --generate-source exampleapp.gresource.xml
The Gio::Resource and glib-compile-resources section contains more information about resource files. If you build with Meson, use the compile_resources() function in Meson's GNOME module.

Vårt program ser nu ut så här:

Populating the window

Källkod