Search code examples
qtrestartiniqsettings

QSettings IniFormat File gets empty after restart


I use QSettings to save and load parameters to/from an ini file using Linux:

write:

QSettings settings("setup.ini", QSettings::IniFormat);
settings.beginGroup("Setup_Parameter");
settings.setValue("Parameter1",parameter1_value);
settings.sync();
settings.endGroup();

read:

QSettings settings("setup.ini", QSettings::IniFormat);
settings.beginGroup("Setup_Parameter");
parameter1_value = settings.value("Parameter1","0").toInt();
settings.endGroup();

The setup.ini works fine, while the system is on. If i reboot my system by switching power off and on again, the setup.ini file gets completely empty sometimes. I would say in 3 out of 5 trys.

I already tryed saving the file in application and root/Settings path. As well as copying the file after writing it, but then also the copy is empty after power off and on.

Why does the setup.ini File looses its content? It needs to keep the parameters while restart.


Solution

  • A sync() was required after the write function:

    QSettings settings("setup.ini", QSettings::IniFormat);
    settings.beginGroup("Setup_Parameter");
    settings.setValue("Parameter1",parameter1_value);
    settings.endGroup();
    settings.sync();
    sync();