Results 1 to 6 of 6

Thread: Where to save QSettings?

  1. #1
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Where to save QSettings?

    Hi all.

    I'm working on a little destkop gui app and use QSettings to save settings to ini file, so they can be loaded on next app run. I have some lineEdit components on form where user puts settings, so to load settings I call my method to load settings from the MainWindow constructor, but don't know where to call my save settings method. I know I could make a separate button ("Save settings") for this, but would rather have it triggered somewhere automatically. I can not save settings on MainWindow destructor, where would you put it, much thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Where to save QSettings?

    You could overwrite the main window's closeEvent().
    Or in main(), not return directly from app.exec() but keep its return value in a local variable, then save then return the variable.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Where to save QSettings?

    Quote Originally Posted by anda_skoa View Post
    You could overwrite the main window's closeEvent().
    Or in main(), not return directly from app.exec() but keep its return value in a local variable, then save then return the variable.

    Cheers,
    _
    Thanks, window's closeEvent() sounds more appropriate to me, but where do find it or how do I make it, thanks

  4. #4
    Join Date
    Jul 2015
    Location
    Sweden
    Posts
    12
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Where to save QSettings?

    Just override the closeEvent in your ManiWindow class

    mainwindow.h
    Qt Code:
    1. void closeEvent(QCloseEvent* event) override;
    To copy to clipboard, switch view to plain text mode 
    (btw. the override specifier requires a C++ 11 compatible compiler, should not be much of a problem any longer though)

    maniwindow.cpp
    Qt Code:
    1. void MainWindow::closeEvent(QCloseEvent* event)
    2. {
    3. ...
    4. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Where to save QSettings?

    mikag thanks, solved

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,117
    Thanks
    235
    Thanked 656 Times in 646 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Where to save QSettings?

    You can also write the QSettings entry immediately after the user finishes editing the QLineEdit. I do this when the settings I need are "local" to that piece of code, and I don't want to have to propagate that throughout the app. For example, I have a dialog that is used to edit properties for a plot; when the user closes the dialog after editing, I immediately write the changes out to QSettings. The rest of the app doesn't have to know.

    Some people may not like this style, but I find it is useful for creating reusable components that don't depend on other parts of the app in order to function correctly.

Similar Threads

  1. Save a QStringList to arrey in QSettings
    By halvors in forum Qt Programming
    Replies: 8
    Last Post: 7th October 2015, 21:52
  2. Save Font settings using QSettings
    By arunkumaraymuo1 in forum Qt Programming
    Replies: 2
    Last Post: 31st July 2012, 13:58
  3. Save enums in QSettings
    By redhat in forum Qt Programming
    Replies: 4
    Last Post: 22nd January 2009, 11:22
  4. QSettings does not save...
    By mtrpoland in forum Qt Programming
    Replies: 2
    Last Post: 28th August 2007, 12:15

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.