PDA

View Full Version : change path of settings ini file



tuli
20th October 2012, 11:33
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.



QSettings::setDefaultFormat(QSettings::IniFormat);
QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, a.applicationDirPath());

QSettings st;


st.setValue("test", 1234);
st.sync();

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..

mvuori
20th October 2012, 12:50
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);

no
21st October 2012, 10:32
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://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://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:


QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, a.applicationDirPath());
QSettings st(QSettings::IniFormat, QSettings::UserScope, "myapp");

...might work.

ChrisW67
22nd October 2012, 02:47
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.

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.

tuli
22nd October 2012, 20:35
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.

tuli
15th January 2013, 10:12
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?