PDA

View Full Version : How to set the scope of QSettings to be application based.



wladek
15th July 2010, 14:00
Hi all,

I am using QSettings and have seen that one could set the applicationName and the organizationName for a QApplication, so that QSettings can be used as:
QSettings settings; and the setting will reside into the already set applicationName and organizationName.

My question here: is there a possibility to achieve something similar also with the Scope of the QSettings.
If i would just do QSettings settings; the Scope would be QSettings:UserScope and I need QSettings:SystemScope.

One solution would be to use this constructor:
QSettings settings(QSettings::SystemScope, QApplication::organizationName());, but this involves more coding.

Is there any other better way to achieve this?

Thanks in advance,
Wladek

Lykurg
15th July 2010, 14:31
See the static function QSettings::setPath().

Lykurg
15th July 2010, 14:38
See the static function QSettings::setPath().
Ok, after a quick control look inside the documentation: Forget about that solution, I thought you can just swap the path, but it's not that easy.

So for avoiding typing all that parameters, you can define a global function returning a proper settings object. (Or define a macro.)

wladek
15th July 2010, 15:16
Hey Lykurg,

Thanks for your reply. I did try to define a macro like:
#define Settings QSettings(QSettings::SystemScope, QApplication::organizationName(), QApplication::applicationName());But then when I would want to use it like:
QSettings settings = Settings; I would get the error:
QSettings::QSettings(const QSettings&)' is private After checking with the QSettings source code i've seen that the copy constructor was disabled
Q_DISABLE_COPY(QSettings)

So, as a solution I defined my own custom class, SystemSettings which inherits QSettings, and in it's constructor just calls the proper QSettings's constructor.
class SystemSettings : public QSettings
{
public:
SystemSettings()
:QSettings(QSettings::SystemScope, QApplication::organizationName(), QApplication::applicationName()){};
};

And, to be in the same side Qt is, i am also disabling my copy constructor: :)
private:
Q_DISABLE_COPY(SystemSettings);

Would you suggest different?

Thanks,
Wladek

Lykurg
15th July 2010, 15:26
Yeah, great, I'm a fan of subclassing :D Haven't thought of that before, and that's the most elegant solution!