PDA

View Full Version : QSettings::setPath()



hvengel
18th February 2008, 22:25
On Qt3 I have my QSettings object setup this way:


mySettings = new QSettings();
mySettings->setPath( "myOrg", "myApp", QSettings::User);
mySettings->insertSearchPath( QSettings::Unix, QDir::homeDirPath() + myBaseDir + "/config" );

Looking at the Qt4 docs it looks like:


mySettings = new QSettings("myOrg", "myApp");
mySettings->setPath(QSettings::NativeFormat, QSettings::UserScope, QDir::homeDirPath() + myBaseDir + "/config");

Should do exactly the same thing but it does not. For some reason the path parameter of the of the call to QSettings::setaPath(format, scope, path) call does not do anything. How do I get this to use the path I specify for the location of the configuration file? Seems kind of strange that the Trolls would have a parameter on a call that does not do anything. So either the documentation is wrong or there is something about this that I don't understand. Anyone know what is going on with this?

jacek
18th February 2008, 22:39
QSettings::setPath() is a static method and you have to invoke it before you create QSettings instance.

Qt docs say:
Warning: This function doesn't affect existing QSettings objects.

hvengel
18th February 2008, 22:59
Yes I saw that but I was not sure what that meant? Are you saying that when my code says:

mySettings = new QSettings("myOrg", "myApp");

that at that point it is an existing QSettings object and that I need to call QSettings::setPath() before the QSettings object is instantiated? On the surface this does not make sense. After all how can you call a method for an object that does not exist? But I just did a test and it worked although somewhat differently than it does in Qt3.

So it appears that it is expecting:


mySettings -> setPath(,,,); // mySettings is not a valid object
// QSettings::setPath(...) also works
mySettings = new QSettings(...);

jacek
18th February 2008, 23:09
After all how can you call a method for an object that does not exist?
setPath() is a static method, so it doesn't need any instances.


mySettings -> setPath(,,,); // mySettings is not a valid object
// QSettings::setPath(...) also works
The proper way to invoke a static method is "QSettings::setPath(...);".

"invalidPtr->setPath(...)" works, but that's a very bad programming style.

bender86
19th February 2008, 19:53
You should use QSettings::IniFormat instead of QSettings::NativeFormat, because on Windows native format is registry and also in Mac Os X it doesn't use files.

hvengel
21st February 2008, 00:37
I know that. I want to use the native format on each platform.