[英]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. 我需要以编程方式从C ++程序中的gsettings获取com.ubuntu.user-interface比例因子的值。 Is there any elegant way to do this, instead of calling gsettings binary and parsing it's output?
有什么优雅的方法可以执行此操作,而不是调用gsettings二进制文件并解析其输出吗?
There is a C++ binding to gsettings in glibmm. glibmm中有一个C ++绑定到gsettings。 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. 请注意,我没有可在其上进行测试的Ubuntu系统,因此细节依赖于对文档的简短了解,该文档告诉我
scale-factor
是不可或缺的价值。 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;
}
I can't post a comment to Wintermute answer because of low reputation so I post it here. 由于声誉低下,我无法对Wintermute答案发表评论,所以我在这里发表。
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) 像我一样,Newbe可能会
<giomm/settings.h>
包括<giomm/settings.h>
在内的问题(未找到):一种解决方案是在gcc编译命令`pkg-config --cflags --libs glibmm-2.4 giomm-2.4`
加上反引号)
If your source file is
program.cc
, you can compile it with:如果您的源文件是
program.cc
,则可以使用以下命令进行编译:
g++ program.cc -o program `pkg-config --cflags --libs glibmm-2.4 giomm-2.4`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.