Hey there
I’m trying to set up a function which saves some settings with help of the QSettings class. It should have the possiblity to save these settings either into the windows user registry or into an INI-File which should be saved in the directory defined by the user.
So what I know by now is that if I define the QSettings differently, it differs where theses settings are going to be saved.
QSettings s;
Like this they’re saved in the regstry.
QSettings s(directory, QSettings::IniFormat);
In this way, they’re saved into an INI-File at “directory”.
Now, to avoid to write the exact same code twice there should be a short differentiation at the beginning of the function.
First I thought of something like this:
writeSettings(QString dir)
{
QSettings s;
if(!dir.isEmpty) s.setPath(QSettings::IniFormat, QSettings::UserScope, dir);
s.setValue(...);
...
}
But this doesn’t work. Then I though, this could solve the problem:
writeSettings(QString dir)
{
if(dir.isEmpty) QSettings s;
else QSettings s(dir, QSettings::IniFormat);
s.setValue(...);
...
}
Then it won’t determine “s” properly that at the first “s.setValue(…)” an “s is undefined” error occurs.
Is there another way to solve this? At the moment I double up the whole function with two different parameter types to distinguish between both possibilities. And this isn’t acceptable in my eyes.
Thx for any help!
↧