Results 1 to 6 of 6

Thread: change path of settings ini file

  1. #1
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default change path of settings ini file

    hi,

    I want to portably (both as in inter-os as well as in portable software) store the settings for my application.
    To do that, i decided to save them in the INIformat to the same directory as the executable.


    Qt Code:
    1. QSettings::setDefaultFormat(QSettings::IniFormat);
    2. QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, a.applicationDirPath());
    3.  
    4.  
    5.  
    6. st.setValue("test", 1234);
    7. st.sync();
    To copy to clipboard, switch view to plain text mode 

    This works well, but creates an unnecessary folder:

    ./mycompany/myapp.ini

    I`d like it to be

    ./myapp.ini

    is taht possible?


    edit:
    removed original post. Turns out setPath() is a static method, even though my IDE lead me to believe differently..
    Last edited by tuli; 20th October 2012 at 11:44.

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: change path of settings ini file

    Of course you can have your settings file anywhere you want by just telling that in the constructor:

    myQSettings = new QSettings(anydirYouLike + "thisapp.ini", QSettings::IniFormat);

  3. #3
    Join Date
    Jan 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: change path of settings ini file

    I imagine because you're using the default QSettings constructor (by not supplying any arguments in QSettings st;), Qt creates the file in the application scope ([path]/organization/application.ini). See http://doc.qt.io/qt-4.8/http://qt-project.org/doc/qt-4.8/qsettings.html#QSettings-5. It apparently accepts your defaults (format and path - this constructor defaults to UserScope as well), but still applies the organization/application stuff to your path.

    If you use another constructor, e.g., http://doc.qt.io/qt-4.8/http://qt-project.org/doc/qt-4.8/qsettings.html#QSettings-3, and leave out the application argument, you'll probably (not sure it'll take the path you've set) get ./<company>.ini, where <company> is the organization argument you supply:

    Qt Code:
    1. QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, a.applicationDirPath());
    2. QSettings st(QSettings::IniFormat, QSettings::UserScope, "myapp");
    To copy to clipboard, switch view to plain text mode 

    ...might work.

  4. #4
    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: change path of settings ini file

    Qt Code:
    1. I want to portably (both as in inter-os as well as in portable software) store the settings for my application.
    2. To do that, i decided to save them in the INIformat to the same directory as the executable.
    To copy to clipboard, switch view to plain text mode 
    Bad choice if you expect it to work on Windows Vista or 7 which prohibit unprivileged writing in the Program Files directories. If someone installs the software there it will likely fail.

  5. #5
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: change path of settings ini file

    Bad choice if you expect it to work on Windows Vista or 7 which prohibit unprivileged writing in the Program Files directories. If someone installs the software there it will likely fail.
    true, but as i said it`s a portable program, ie you are not meant to install it.
    Thanks guys, that did the trick.

  6. #6
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: change path of settings ini file

    alright, here i am again.

    I am tempted to save projekt files (basically a bunch of numbers) on a per-projekt basis to .ini files. This is a popular request, everyone seems to love .ini file and hate my .xml s. :/


    QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, "c:\\");
    QSettings settings(QSettings::IniFormat, QSettings::UserScope, "projekt123", "inifile");

    settings.setValue("projx/asdf", QVariant((quint32)1234589));
    settings.sync();
    THis works fine.
    However:

    - i have to cheat by passing ( "projekt123", "inifile") to the constructor to get a wanted filename.
    - i change the path to "c:\". How can i get the previously set path as to restore it once i am done saving the projekt?


    I feel like i am abusing the QSettigns class a little for this. Does Qt offer any specialzed way for storing data in .ini files?

Similar Threads

  1. Replies: 1
    Last Post: 17th February 2012, 02:28
  2. Replies: 2
    Last Post: 16th November 2011, 11:18
  3. Qt Creator 1.0.0 - LIB & PATH settings in Ubuntu
    By qoo in forum Installation and Deployment
    Replies: 1
    Last Post: 20th April 2009, 08:35
  4. How to change Path
    By shyam prasad in forum Qt Programming
    Replies: 1
    Last Post: 10th January 2007, 08:29
  5. Replies: 3
    Last Post: 31st March 2006, 18:38

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.