Results 1 to 5 of 5

Thread: Save application settings

  1. #1
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Save application settings

    Hello forum,

    I am developing a small app that has several modules that need to to be saved under different path as follows:

    /Settings/Debug/
    /Settings/Haptic/

    And i have separate functions to save to this different paths.

    Now will it suffice if i have a single QSettings object and call each time the static function to save these configurations into different paths or i have to create new QSettings object for each of the modules.


    Regards,
    Sajjad

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,233
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Save application settings

    Static methods don't have object instances, so calling them using an object instance makes no sense.

    However, the docs say about QSettings::setPath():

    Warning: This function doesn't affect existing QSettings objects.
    So, if you call setPath() before creating the QSettings object, the contents will be written to the new path. If call setpath() after creating the QSettings object, it will have no effect. This implies that if you want to save settings to two different places, then you need two QSettings objects, and you need to call setPath() before each one of them is created.

  3. #3
    Join Date
    Dec 2011
    Posts
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Save application settings

    There's no point in creating a singleton -- once you call setPath on QSettings it's retained for the life of the application.

    What you should do is call setPath with /Settings and then do something like this:
    settings.beginGroup("Debug");
    settings.setValue("size", win->size());
    settings.setValue("fullScreen", win->isFullScreen());
    settings.endGroup();

    This should get you exactly what you want. QSettings are also very cheap to be created on the fly.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,233
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Save application settings

    once you call setPath on QSettings it's retained for the life of the application.
    So you're saying that if you call setPath() twice for the same Format and Scope but a different path, the second call has no effect at all?

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Save application settings

    Quote Originally Posted by Agnostic Pope View Post
    This should get you exactly what you want. QSettings are also very cheap to be created on the fly.
    Groups do not get the OP two separate settings files, which is what was asked for. Groups with a single file, however, is usually a better option IMHO.

Similar Threads

  1. Replies: 2
    Last Post: 16th July 2011, 07:53
  2. Qt application settings in JSON
    By asvil in forum Qt-based Software
    Replies: 0
    Last Post: 5th March 2011, 21:35
  3. Replies: 0
    Last Post: 25th April 2010, 17:41
  4. Replies: 2
    Last Post: 2nd September 2009, 13:36
  5. Can we save & load the settings in GUI !
    By Krish in forum Newbie
    Replies: 6
    Last Post: 26th March 2008, 15:33

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.