PDA

View Full Version : Save application settings



sajis997
8th May 2012, 13:35
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

d_stranz
8th May 2012, 17:27
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.

Agnostic Pope
8th May 2012, 17:32
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.

d_stranz
8th May 2012, 20:54
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?

ChrisW67
9th May 2012, 04:54
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.