Solved Configure QSettings ((QSettings::Format and QSettings::Scope )
-
I use QSettings in many places in my application so I want to use:
QCoreApplication::setOrganizationName("MySoft"); QCoreApplication::setOrganizationDomain("mysoft.com"); QCoreApplication::setApplicationName("Star Runner");
And then only use
QSettings settings
. But if I call the default constructor the default QSettings::Format and QSettings::Scope used is not what I want.I want to know if I can set QSettings::Format and QSettings::Scope just like I set the Organization Name beforehand?
-
@hbatalha QSettings has static setDefaultFormat that you can call along those other
set*
functions, so the only part missing is the default scope, but it's not so bad I think?QSettings settings(QSettings::SystemScope);
It does feel like it would be kinda completing the API nicely if there was also something like static
QSettings::setDefaultScope(QSettings::Scope)
. You could file a feature request on bugreports.qt.io if that's something you'd like to see in future versions. -
@hbatalha said in Configure QSettings ((QSettings::Format and QSettings::Scope ):
QCoreApplication::setOrganizationName("MySoft");
QCoreApplication::setOrganizationDomain("mysoft.com");
QCoreApplication::setApplicationName("Star Runner");Where exactly in your code do you execute this?
-
@jsulm said in Configure QSettings ((QSettings::Format and QSettings::Scope ):
Where exactly in your code do you execute this?
in the main.cpp
int main(int argc, char *argv[]) { QApplication a(argc, argv); QCoreApplication::setOrganizationName("MySoft"); QCoreApplication::setOrganizationDomain("mysoft.com"); QCoreApplication::setApplicationName("Star Runner"); ...
-
Just subclass
QSettings
class MySettings : public QSettings{ Q_OBJECT Q_DISABLE_COPY(MySettings) public: explicit MySettings(QObject *parent = nullptr) : QSettings(QSettings::IniFormat, QSettings::UserScope, #ifdef Q_OS_DARWIN QStringLiteral("mysoft.com") #else QStringLiteral("MySoft") #endif , QStringLiteral("Star Runner"), parent) {} };
now use
MySettings settings
wherever you'd have usedQSettings settings
-
@VRonin said in Configure QSettings ((QSettings::Format and QSettings::Scope ):
Just subclass QSettings
Ugh!
Can't we have something like this instead?*QSettings mySettings() { return QSettings(QSettings::IniFormat, QSettings::UserScope, QStringLiteral(...)); }
*c++17 and later
Use directly on initialization:
QSettings settings = mySettings();
-
@kshegunov said in Configure QSettings ((QSettings::Format and QSettings::Scope ):
Can't we have something like this instead?*
Potato/Potatoe
-
@VRonin said in Configure QSettings ((QSettings::Format and QSettings::Scope ):
Potato/Potatoe
The point is to avoid type proliferation, I know you know that's a thing. Why derive anything if you don't have to; you're not changing any behavior here!
-
@kshegunov don't you still need a class for this, or do you advocate a global free floating function here?
-
@J-Hilk said in Configure QSettings ((QSettings::Format and QSettings::Scope ):
@kshegunov don't you still need a class for this, or do you advocate a global free floating function here?
A normal C-like function,
inline
-d ideally. Why would you make a class for this? -
@kshegunov you can't as QSettings is a QObject.It does work with C++17, see @kshegunov answer below.
-
@hbatalha QSettings has static setDefaultFormat that you can call along those other
set*
functions, so the only part missing is the default scope, but it's not so bad I think?QSettings settings(QSettings::SystemScope);
It does feel like it would be kinda completing the API nicely if there was also something like static
QSettings::setDefaultScope(QSettings::Scope)
. You could file a feature request on bugreports.qt.io if that's something you'd like to see in future versions. -
@SGaist said in Configure QSettings ((QSettings::Format and QSettings::Scope ):
@kshegunov you can't as QSettings is a QObject.
Au contraire mon ami! Excuse the bad friench, but the point is C++17 mandates RVO, so this compiles and runs correctly with g++. You don't make no copies here.
-
The French part is completely correct :-)
I missed the C++17 bits. My bad ! -
@SGaist said in Configure QSettings ((QSettings::Format and QSettings::Scope ):
The French part is completely correct :-)
Good that my initial (and rather inexcusable) error was caught on time ;)
-
@Chris-Kawa said in Configure QSettings ((QSettings::Format and QSettings::Scope ):
QSettings has static setDefaultFormat that you can call along those other set* functions, so the only part missing is the default scope, but it's not so bad I think?
That's a good idea, I read it in the doc but misunderstood its role. I will try this approach and @kshegunov's answer and get back to you.