Having trouble getting my schema to work in GTK4 tutorial example

I’ve been working through the GTK4 tutorial. I’ve been typing the code examples and cross-referencing against the source repo.

The only major change I’ve made has been using CMake to build. However, for this step I’m doing what the guide says and using glib-compile-schemas on the command line directly. This gives me a gschemas.compiled file in my source directory.

The app builds but when I run it I get the following error:

(example-5:552127): GLib-GIO-ERROR **: 20:48:37.808: Settings schema 'org.gtk.exampleapp' is not installed

I’ve tried copying the compiled schema to the actual build directory but that doesn’t help. I’ve been good at trouble shooting this stuff up until now but I really just don’t understand the schema system well enough to know what try do next.

I’m on Ubuntu 21.10 and my XDG_DATA_DIRS has the following:

/usr/share/ubuntu:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop

I’m happy to dig for my solutions but at this point I don’t know enough about the GTK/GLib environment to proceed.

For conventional applications, the schema XML file is installed to /usr/share/glib-2.0/schemas/ and the package manager rebuilds all schemas there into a combined gschemas.compiled file.

Schemas compiled at this location can be automatically found by GIO, but otherwise you will have to tell your application where to look for this file. For that you will want g_settings_schema_source_new_from_directory(). You will then pass that GSettingsSchemaSource to g_settings_new_full(), which can lookup your schema by ID.

Okay! Thank you!

I’ve copied my org.gtk.exampleapp.gschema.xml file to that folder. This is something I’ve already tried but I did not run glib-compile-schemas in THAT folder because I needed elevated permissions and that seemed like a heavy-handed way to make this work.

But I’ve done that now and it’s working! Is this really how the system is supposed to work? I guess we install new apps with sudo privileges… but if I’m iterating while developing I would need to copy and re-compile all the schemas for every change to that xml file?

Yes. The XML schemas are the definition of your settings, but GSettings loads the “compiled” blob with all the definitions to avoid having to read many small files all the time.

The alternative to installing the schemas and then running glib-compile-schemas on the installation path is to recompile it inside the build directory, and then run your binary with a wrapper that sets the GSETTINGS_SCHEMAS_DIR environment variable to the place where the compiled schema is.

There’s also the option of building things using a Flatpak manifest; the build will be sandboxed, and you won’t have to deal with dependencies or changing your base system.

1 Like

I appreciate this. In my mind the glib-compile commands need an install/remove option to handle this but it would be a dev-tool, not for everyday users.

Got the preferences working, the transitions are sweet but the font chooser is a pig (slow bordering on unusable). I see there’s been some investigating done into this already so I’ll come back to this problem when I have a better grasp on things.

The alternative to installing the schemas and then running glib-compile-schemas on the installation path is to recompile it inside the build directory, and then run your binary with a wrapper that sets the GSETTINGS_SCHEMAS_DIR environment variable to the place where the compiled schema is.

Oof! Working ahead I happened to peek into main.c and there it is:

int
main (int argc, char *argv[])
{
  /* Since this example is running uninstalled,
   * we have to help it find its schema. This
   * is *not* necessary in properly installed
   * application.
   */
  g_setenv ("GSETTINGS_SCHEMA_DIR", ".", FALSE);

  return g_application_run (G_APPLICATION (example_app_new ()), argc, argv);
}

I missed this change at that step and this is in line with what I expected to find. Maybe make a note of this in the GTK tutorial.

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