PDA

View Full Version : QSettings and closeEvent()



vito49
9th October 2008, 12:26
Hello,
Sorry for my bad English ...

My program uses a QSettings status file to store program settings, not size and window data, but only specific program related stuff like countdown times.

The statusfile is made like this:


statusFile=new QSettings(QApplication::applicationDirPath () + "/.status.ini", QSettings::IniFormat);


There are 2 countdown timers with a 1 seconds tick and whenever the program is closed (with the X button) the remaining seconds must be saved to the status file to resume the program when it is restarted.

Previously I did it with writing the countdowns every second to the status file using:


sessionTime -=1;
worElapsed -=1;
statusFile->setValue("countdown_1/timeLeft", sessionTime);
statusFile->setValue("countdown_2/busyTime", worElapsed);

This works fine, but I have the impression that QSettings is writing the settings to often to the statusFile, to my opeinion a bit overkill.
Therefore I like to use the closeEvent to write these settings to the file when the program closes.


sessionTime -=1;
worElapsed -=1;
///// statusFile->setValue("countdown_1/timeLeft", sessionTime); -> no longer used
/////statusFile->setValue("countdown_2/busyTime", worElapsed); -> no longer used

// but using the closeEvent this way:
...
void MyWidget::closeEvent(QCloseEvent *event)
{
QMessageBox warning;
warning.warning(this, tr("closeEvent"), tr("closeEvent works"), QMessageBox::Ok);

writeStatus();

event->accept();

}
/////////////////////////////////////////////////////////////////////
void MyWidget::writeStatus()
{
statusFile->setValue("countdown_1/timeLeft", sessionTime);
statusFile->setValue("countdown_2/busyTime", worElapsed);

QMessageBox warning;
warning.warning(this, tr("writeStatus"), tr("writeStatus should work"), QMessageBox::Ok);

}


Both message boxes are only for test purposes, so ignore, and they both show, so this means the closeEvent and the writeStatus() work, but ... the data of the count down timers is NOT written to the statusFile.

And that's something I don't understand :o

Can somebody please explain what is going on exactly? I read the docs of QSettings and closeEvent() but I can't figure out what is wrong.

Many thanks for your time.

jpn
9th October 2008, 15:43
The settings object is not properly destructed. Consider creating it on the stack when needed.


{
QSettings settings(...);
...
} // settings goes out of scope and unsaved changes will eventually be written to permanent storage

vito49
13th October 2008, 16:18
The settings object is not properly destructed. Consider creating it on the stack when needed.


{
QSettings settings(...);
...
} // settings goes out of scope and unsaved changes will eventually be written to permanent storage


Sorry, but I can't figure out how to do it ... but because I added a messageBox to the closeEvent (asking if I really want to leave) it seems to work. I'm going to keep it that way, later on I shall try to fix it. At the moment I have another problem that puzzles me and I like it to be fixed, it seems to be more urgent ... I wrote it down in a new thread.

Thanks for your reply.