QSettings doesn't save data
-
When I click on the logout button and then "Yes" it should save some data in my file and then restart the application, but I don't get why it doesn't save it.
This is the function that should do the work:void home::on_logout_BTN_clicked() { reply = QMessageBox::question(this, tr("Logout"), tr("Exit the actual session?"),QMessageBox::Yes|QMessageBox::No); if ((reply == QMessageBox::Yes) && (p_ciclo_macchina->cicloFermo==true)) { seconds->stop(); // home page timer qDebug("Prova"); // Restart application qApp->quit(); QProcess::startDetached(qApp->arguments()[0], qApp->arguments()); }
My setOnPowerFail( ) function:
void storage::setOnPowerFail() { storageData->beginGroup("powerFail"); storageData->setValue("pianiFatti",QString::number(lastPianoFatto)); storageData->setValue("sacchiCaricati",QString::number(lastSaccoCaricato)); storageData->setValue("palletProdotti",QString::number(lastPalletProdotti)); storageData->setValue("numTotaleSacchi",QString::number(numTotaleSacchi)); storageData->endGroup(); }
But if I check the file ini nothing changed and after the restart the field where I change value doesnt show the new values.
I noticed that if I move thedataStorage->setOnPowerFail();
before the reply message box it works just fine, but inside the if statement. What could it be the problem? -
There is no class 'QStorage' in Qt. When it's QSettings - they're only saved when you explicitly ask to do them so or during destruction.
-
@Christian-Ehrlicher Yes, sorry you are right, it's QSetting no Qstorage.
they're only saved when you explicitly ask to do them so or during destruction
Okay? So how can I save during destruction?
What's wrong in the code? -
@aim0d said in QSettings doesn't save data:
What's wrong in the code?
You don't destruct your QSettings object nor tell them that it should sync the data to disk as I already wrote.
-
Hi,
Unless doing something special, QSettings is usually allocated on the stack of the method that uses it. Most of the time, there's no need for a member variable of that type.
-
@aim0d said in QSettings doesn't save data:
I noticed that if I move the dataStorage->setOnPowerFail(); before the reply message box it works just fine, but inside the if statement. What could it be the problem?
To expand on what @Christian-Ehrlicher has said. void QSettings::sync() tells you:
Writes any unsaved changes to permanent storage, and reloads any settings that have been changed in the meantime by another application.
This function is called automatically from QSettings's destructor and by the event loop at regular intervals, so you normally don't need to call it yourself.
If you call
setOnPowerFail()
beforeQMessageBox::question()
that allows the Qt event loop to run and so presumably syncs (writes) your changes to registry/disk. But if you call it afterwards and immediately quit/restart the application I guess the event loop does not get to run, nor doesQSettings
object get destructed, so the changes never get committed?Put in an explicit
QSettings::sync()
aftersetOnPowerFail()
before restart, does that fix? -
@JonB said in QSettings doesn't save data:
I guess the event loop does not get to run, nor does QSettings object get destructed,
I thought this could be the problem.
@JonB said in QSettings doesn't save data:
Put in an explicit QSettings::sync() after setOnPowerFail() before restart, does that fix?
Actually yes, it did it!
-