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);
To copy to clipboard, switch view to plain text mode
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);
sessionTime -=1;
worElapsed -=1;
statusFile->setValue("countdown_1/timeLeft", sessionTime);
statusFile->setValue("countdown_2/busyTime", worElapsed);
To copy to clipboard, switch view to plain text mode
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:
...
{
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);
warning.
warning(this, tr
("writeStatus"), tr
("writeStatus should work"),
QMessageBox::Ok);
}
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);
}
To copy to clipboard, switch view to plain text mode
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
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.
Bookmarks