问题
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.
回答1:
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();
回答2:
I am experiencing the same problem. My QSettings file gets wiped out sometimes after a power reset. The file remains, but it is zero bytes in size. The QT application is running on Debian Squeeze on an ARM processor. The filesystem, OS, and application are all located on a 4G SD Card.
I've modified the application to call the sync function after any change to the Settings file. But we had a power reset last night and one of the units (we have about 60 running) lost its settings. We're running QTEmbedded-4.8.2, and I am at a loss how to fix this.
回答3:
We have fixed the same problem calling 'sync' linux command after the 'sync' function of QSettings.
来源:https://stackoverflow.com/questions/15427095/qsettings-iniformat-file-gets-empty-after-restart