Reading gsettings from C++ program

2019-09-03 15:56发布

问题:

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?

回答1:

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.



回答2:

I can't post a comment to Wintermute answer because of low reputation so I post it here.

Newbe, like me, could have problem including <giomm/settings.h> (not found): a solution is to append to gcc compile command `pkg-config --cflags --libs glibmm-2.4 giomm-2.4` (with backticks)

If your source file is program.cc, you can compile it with:

g++ program.cc -o program `pkg-config --cflags --libs glibmm-2.4 giomm-2.4`

From here