GSettings schema for a(sv)

I’m trying to save print and page settings using gsettings.
( from gtk_print_settings_to_gvariant() )
In my program.gschema.xml file I have …

        <key name="print-settings" type="a(sv)">
            <summary>GTK print settings</summary>
            <description>The print settings that were last used</description>
        <key name="page-settings" type="a(sv)">
            <summary>GTK page settings</summary>
            <description>The page settings that were last used</description>

but when I compile this I get the error:

😼 glib-compile-schemas --dry-run .
./HPGLplotter.gschema.xml:8:1  Failed to parse <default> value of type “a(sv)”: 0:expected value.  This entire file has been ignored.

What do you use as the default value for an array like this?

… to answer my own question…


If you’re trying to serialise a key/value store, you might want the GVariant type a{sv} instead of a(sv) as it better represents one.

Just a suggestion to consider; I don’t know what your particular use case is.

What is the difference ?
… actually when I checked the type that gtk_print_settings_to_gvariant() returned it was a{sv} !

{} is a string dictionary element. () is a tuple.

See GLib.VariantType

1 Like

I still have a problem. I’m getting an error when I use g_settings_set_value().
I get this error on either print or page settings

Print Settings is of type a{sv}
Print Setup is of type a{sv}
Bail out! GLib:ERROR:../glib/gvarianttypeinfo.c:190:g_variant_type_info_check: assertion failed: (0 <= index && index < 24)

FWICT it should be correct…

My schema is …

  <schema id="us.heterodyne.HPGLplotter" path="/us/heterodyne/HPGLplotter/">
    <key name="print-settings" type="a{sv}">
      <summary>GTK print settings</summary>
        GTK print settings
    <key name="page-setup" type="a{sv}">
      <summary>GTK page setup</summary>
        GTK page setup
    <key name="pen-colors" type="a(dddd)">
      <summary>HPGL pen colors</summary>
        HPGL pen colors (RGBA)

and my code is

	GVariant *gvPrintSettings = NULL, *gvPageSetup = NULL, *gvPenColors;
    GVariantBuilder *builder;

    GSettings *gs;

    if( !g_settings_schema_exist( GSETTINGS_SCHEMA ) )
    	return FALSE;

    gs = g_settings_new( GSETTINGS_SCHEMA );
    if( pGlobal->printSettings )
    	gvPrintSettings = gtk_print_settings_to_gvariant( pGlobal->printSettings );
    if( pGlobal->pageSetup )
    	gvPageSetup  = gtk_page_setup_to_gvariant( pGlobal->pageSetup );

    g_print( "Print Settings is of type %s\n", g_variant_get_type_string( gvPrintSettings ));
    if( gvPrintSettings )
    	g_settings_set_value( gs, "print-settings", gvPrintSettings );

    g_print( "Print Setup is of type %s\n", g_variant_get_type_string( gvPageSetup ));
    if( gvPageSetup )
    	g_settings_set_value( gs, "page-setup", gvPageSetup );

OK … it was just my (very) thin understanding…

I was freeing the gvariant’s later on with

    g_variant_unref (gvPrintSettings);
    g_variant_unref (gvPageSetup);

On looking at the documentation I see that g_settings_set_value says …

If value is floating then this function consumes the reference.

… and indeed the gtk_print_settings_to_gvariant() and gtk_page_setup_to_gvariant() functions give floating references.

1 Like

Yes, floating references are something you have to be aware of when working with GVariants. They can make using them in C code a lot simpler, but can be a footgun if you’re not aware of them, unfortunately.

Glad you got it working

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