c++linuxgsettings

Reading gsettings from C++ program


I need to programmatically get the value of com.ubuntu.user-interface scale-factor from gsettings in my C++ program. Is there any elegant way to do this, instead of calling gsettings binary and parsing it's output?


Solution

  • There is a C++ binding to gsettings in glibmm. With it, reading a value from a schema can be done as shown below. Note that I do not have an Ubuntu system on which to test this, so specifics rely on a short look into the documentation that told me scale-factor is an integral value. With this in mind:

    #include <giomm/settings.h>
    #include <iostream>
    
    int main() {
      Glib::RefPtr<Gio::Settings> s = Gio::Settings::create("com.ubuntu.user-interface");
      int i = s->get_int("scale-factor");
    
      std::cout << i << std::endl;
    }
    

    See also here.