How to bind Adw.ComboRow with Gtk.StringList as its model to a GSettings key?

I have an Adw.ComboRow and I am using Gtk.StringList as its model. I want to bind its selection to a GSettings key so that when I select something from the ComboRow, the GSettings key also gets updated and when the GSettings key changes, appropriate item in the ComboRow gets selected.

How do I do that?

Use g_settings_bind() with AdwComboRow:selected

Or you may need to use bind_with_mapping() depending on how you store your settings. then you can translate uint->whatever you store in gsettings and back.

1 Like

Sorry! I forgot to mention, the GSettings key is an enum. g_settings_bind takes it as a string. So, g_settings_bind doesn’t work with ‘selected’ property (which is of int type) of Adw.ComboRow.

I tried to read the docs but I could not understand how to use g_settings_bind_with_mapping in python or otherwise.

It will be very helpful if someone could provide an example.

I don’t have Python examples, but you provide functions that translate uint->string and string->uint. Then they will be automatically called every time the property and/or gsettings key change.

A C example (this is likely much simpler in Python though):

static gboolean
reader_font_style_get_mapping (GValue   *value,
                               GVariant *variant,
                               gpointer  user_data)
{
  const char *reader_colors = g_variant_get_string (variant, NULL);

  if (g_strcmp0 (reader_colors, "sans") == 0)
    g_value_set_uint (value, EPHY_PREFS_READER_FONT_STYLE_SANS);
  else if (g_strcmp0 (reader_colors, "serif") == 0)
    g_value_set_uint (value, EPHY_PREFS_READER_FONT_STYLE_SERIF);

  return TRUE;
}

static GVariant *
reader_font_style_set_mapping (const GValue       *value,
                               const GVariantType *expected_type,
                               gpointer            user_data)
{
  switch (g_value_get_uint (value)) {
    case EPHY_PREFS_READER_FONT_STYLE_SANS:
      return g_variant_new_string ("sans");
    case EPHY_PREFS_READER_FONT_STYLE_SERIF:
      return g_variant_new_string ("serif");
    default:
      return g_variant_new_string ("crashed");
  }
}

  ...

  g_settings_bind_with_mapping (reader_settings,
                                EPHY_PREFS_READER_FONT_STYLE,
                                appearance_page->reader_mode_font_style,
                                "selected",
                                G_SETTINGS_BIND_DEFAULT,
                                reader_font_style_get_mapping,
                                reader_font_style_set_mapping,
                                NULL, NULL);
1 Like

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