简体   繁体   English

在C ++中编辑gsettings

[英]Edit gsettings in a C++

I'm trying to edit a gsetting through a C++ program. 我正在尝试通过C ++程序编辑gsetting。 I've read this question and I'm able to get the value. 我已经阅读了这个问题,并且能够获得价值。 If I try to set it (using set_uint method), change seems to be made (if I re-read it shows the new value) but, if I check manually, it is not so. 如果我尝试设置它(使用set_uint方法),则似乎已做出更改(如果我重新读取它,则显示新值),但是,如果我手动检查,事实并非如此。 Do I have to apply the edits? 我必须应用修改吗? Or what else? 或者还有什么?

Example code: 示例代码:

#include <giomm/settings.h>
#include <iostream>

int main() {
    Glib::RefPtr<Gio::Settings> colorSetting = 
                  Gio::Settings::create("org.gnome.settings-daemon.plugins.color");
    Glib::ustring tempKey = "night-light-temperature";

    //Works!
    cout<<colorSetting->get_uint(tempKey)<<endl;

    //Seems to work
    colorSetting->set_uint(tempKey, (unsigned) 2300);

    //Reads new value correctly
    cout<<colorSetting->get_uint(tempKey)<<endl;

    return 0;
}

Thanks in advance. 提前致谢。

Since your program is exiting almost immediately after setting the value, it's likely that the asynchronous write machinery in GSettings has not written the new value to disk by the time your program exits. 由于您的程序在设置该值后几乎立即退出,因此GSettings中的异步写入机制很可能在程序退出时尚未将新值写入磁盘。

Try adding a g_settings_sync() call before exiting (I don't know how it's bound in giomm , but that's what the call is in C). 尝试在退出之前添加g_settings_sync()调用(我不知道giomm是如何绑定的,但这就是C中的调用)。 From the documentation for g_settings_sync() : g_settings_sync()的文档中

Writes made to a GSettings are handled asynchronously. GSettings写入是异步处理的。 For this reason, it is very unlikely that the changes have it to disk by the time g_settings_set() returns. 因此,在g_settings_set()返回之前,更改几乎不可能将更改保存到磁盘。

To be clear, a g_settings_sync() call should not normally be necessary; 需要明确的是,通常不需要g_settings_sync()调用; it's only necessary here because you're not running a main loop. 这仅在此处是必需的,因为您没有运行主循环。

See also: G_Settings apply changes and Can't change dconf-entry with GSettings , which cover the same issue but from the perspective of C and JavaScript. 另请参阅: G_Settings应用更改,并且不能使用GSettings更改dconf-entry ,这从C和JavaScript的角度涵盖了相同的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM