Reading gsettings from C++ program

末鹿安然 提交于 2019-12-25 16:42:47

问题


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



来源:https://stackoverflow.com/questions/28582082/reading-gsettings-from-c-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!