1

I'm writing an application in C++ that uses both Qt and GIO. It happens to be an embedded Linux platform, but I don't know if that matters much. I have a function that sets a setting that another program uses:

void setCityName(const QString &cityName) 
{
    const Glib::RefPtr<Gio::Settings> settings = Gio::Settings::create("org.example.city");
    settings->set_string("city-name", cityName.toUtf8().data()); 
}

This is done at the very end of my program appears to work, but only if I add a delay:

void finish(QString* cityname) {
    qDebug() << "Cityname: " << *cityname;
    setCityName(*cityname);
    // TODO: this is an ugly hack.  Without it, the settings 
    // do not seem to take effect, but I can find no reason
    // for this.
    std::this_thread::sleep_for(200ms);
    emit done();
}

I have instrumented the code and verified that cityname which belongs to another object, is not destroyed until after this code runs, either with or without the delay. Without the delay, everything appears to run normally, but the settings are not actually changed.

Can someone explain why the delay is necessary, and how I can replace it with something more elegant?

Edward
  • 6,964
  • 2
  • 29
  • 55

1 Answers1

2

As the documentation says:

Writes made to a GSettings are handled asynchronously.

Call Gio::Settings.sync: https://docs.gtk.org/gio/type_func.Settings.sync.html

Homer512
  • 9,144
  • 2
  • 8
  • 25
  • 1
    One confusing thing is that there doesn't actually appear to be any way to call that using the C++ bindings. The only thing I found (which works, so thank you!) was to call the plain `g_settings_sync()`. If there's an alternative, I haven't discovered it. – Edward Oct 08 '22 at 00:08
  • @Edward it also appears weird that there is no signal for when the settings are sync'ed asynchronously. – Homer512 Oct 08 '22 at 08:32