【问题标题】:Edit gsettings in a C++在 C++ 中编辑 gsettings
【发布时间】:2017-04-30 15:48:17
【问题描述】:

我正在尝试通过 C++ 程序编辑 gsetting。 我读过这个question 并且我能够获得价值。如果我尝试设置它(使用set_uint 方法),似乎进行了更改(如果我重新读取它会显示新值)但是,如果我手动检查,情况并非如此。我必须应用编辑吗?或者还有什么?

示例代码:

#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;
}

提前致谢。

【问题讨论】:

  • 显示代码 - 我们不是心灵感应的。 MVCE 最好。
  • @tambre 抱歉,现在添加!
  • 好多了,干得好。
  • @tambre 有一个 MCVE 的魔术标签,[MCVE],结果是:minimal reproducible example
  • @Jonas 谢谢!我没有意识到这一点。一直以来,我一直在手动链接 minimal reproducible example 页面。

标签: c++ glib gnome gio gsettings


【解决方案1】:

由于您的程序在设置值后几乎立即退出,很可能GSettings 中的异步写入机制在您的程序退出时尚未将新值写入磁盘。

尝试在退出之前添加一个g_settings_sync() 调用(我不知道它在giomm 中是如何绑定的,但这就是C 中的调用)。来自the documentation for g_settings_sync()

GSettings 的写入是异步处理的。出于这个原因,在g_settings_set() 返回时,这些更改不太可能将其写入磁盘。

需要明确的是,通常不需要调用g_settings_sync();仅在此处是必需的,因为您没有运行主循环。

另请参阅:G_Settings apply changesCan't change dconf-entry with GSettings,它们涵盖了相同的问题,但从 C 和 JavaScript 的角度来看。

【讨论】:

  • 非常感谢,它成功了!我只是在返回和更改之前添加了那个(确切的)调用。
猜你喜欢
  • 1970-01-01
  • 2012-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多