Results 1 to 3 of 3

Thread: QSettings and closeEvent()

  1. #1
    Join Date
    Sep 2008
    Location
    Flanders/Belgium
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QSettings and closeEvent()

    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:
    Qt Code:
    1. 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:
    Qt Code:
    1. sessionTime -=1;
    2. worElapsed -=1;
    3. statusFile->setValue("countdown_1/timeLeft", sessionTime);
    4. 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.
    Qt Code:
    1. sessionTime -=1;
    2. worElapsed -=1;
    3. ///// statusFile->setValue("countdown_1/timeLeft", sessionTime); -> no longer used
    4. /////statusFile->setValue("countdown_2/busyTime", worElapsed); -> no longer used
    5.  
    6. // but using the closeEvent this way:
    7. ...
    8. void MyWidget::closeEvent(QCloseEvent *event)
    9. {
    10. QMessageBox warning;
    11. warning.warning(this, tr("closeEvent"), tr("closeEvent works"), QMessageBox::Ok);
    12.  
    13. writeStatus();
    14.  
    15. event->accept();
    16.  
    17. }
    18. /////////////////////////////////////////////////////////////////////
    19. void MyWidget::writeStatus()
    20. {
    21. statusFile->setValue("countdown_1/timeLeft", sessionTime);
    22. statusFile->setValue("countdown_2/busyTime", worElapsed);
    23.  
    24. QMessageBox warning;
    25. warning.warning(this, tr("writeStatus"), tr("writeStatus should work"), QMessageBox::Ok);
    26.  
    27. }
    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.
    Vito
    from Flanders/Belgium

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSettings and closeEvent()

    The settings object is not properly destructed. Consider creating it on the stack when needed.
    Qt Code:
    1. {
    2. QSettings settings(...);
    3. ...
    4. } // settings goes out of scope and unsaved changes will eventually be written to permanent storage
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Sep 2008
    Location
    Flanders/Belgium
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSettings and closeEvent()

    Quote Originally Posted by jpn View Post
    The settings object is not properly destructed. Consider creating it on the stack when needed.
    Qt Code:
    1. {
    2. QSettings settings(...);
    3. ...
    4. } // settings goes out of scope and unsaved changes will eventually be written to permanent storage
    To copy to clipboard, switch view to plain text mode 
    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.
    Vito
    from Flanders/Belgium

Similar Threads

  1. QSettings and QGLWidgets
    By Rayven in forum Qt Programming
    Replies: 1
    Last Post: 31st May 2008, 18:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.