From 229b363c857f6ba8565096260367c454dc2c1a95 Mon Sep 17 00:00:00 2001 From: Lars Schmertmann Date: Fri, 6 Jul 2018 08:42:36 +0200 Subject: [PATCH] Add QSettings(Scope...) constructor to QSettings Because of the system specific folder/filenames (organization name vs domain) it was not possible to explicitly access the settings with SystemScope without using #ifdef, as it is done by Qt internally. The new constructor uses the default name while creating an instance with a scope. [ChangeLog][QtCore][QSettings] Added QSettings(Scope...) constructor to avoid using #ifdef in Qt applications. Change-Id: I81016430a1d18a382bfdd1e1cf32de367f98d7aa Reviewed-by: Oswald Buddenhagen Reviewed-by: Edward Welbourne --- src/corelib/io/qsettings.cpp | 45 +++++++++++++++++++++++++++++++----- src/corelib/io/qsettings.h | 4 ++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/corelib/io/qsettings.cpp b/src/corelib/io/qsettings.cpp index 8b9f7bc9e5..4a9b2a7261 100644 --- a/src/corelib/io/qsettings.cpp +++ b/src/corelib/io/qsettings.cpp @@ -64,8 +64,8 @@ #include "qrect.h" #endif // !QT_NO_GEOM_VARIANT -#ifndef QT_NO_QOBJECT -#include "qcoreapplication.h" +#ifndef QT_BUILD_QMAKE +# include "qcoreapplication.h" #endif #ifndef QT_BOOTSTRAPPED @@ -2660,9 +2660,10 @@ QSettings::QSettings(const QString &fileName, Format format, QObject *parent) called, the QSettings object will not be able to read or write any settings, and status() will return AccessError. - On \macos and iOS, if both a name and an Internet domain are specified - for the organization, the domain is preferred over the name. On - other platforms, the name is preferred over the domain. + You should supply both the domain (used by default on \macos and iOS) and + the name (used by default elsewhere), although the code will cope if you + supply only one, which will then be used (on all platforms), at odds with + the usual naming of the file on platforms for which it isn't the default. \sa QCoreApplication::setOrganizationName(), QCoreApplication::setOrganizationDomain(), @@ -2670,7 +2671,20 @@ QSettings::QSettings(const QString &fileName, Format format, QObject *parent) setDefaultFormat() */ QSettings::QSettings(QObject *parent) - : QObject(*QSettingsPrivate::create(globalDefaultFormat, UserScope, + : QSettings(UserScope, parent) +{ +} + +/*! + \since 5.13 + + Constructs a QSettings object in the same way as + QSettings(QObject *parent) but with the given \a scope. + + \sa QSettings(QObject *parent) +*/ +QSettings::QSettings(Scope scope, QObject *parent) + : QObject(*QSettingsPrivate::create(globalDefaultFormat, scope, #ifdef Q_OS_MAC QCoreApplication::organizationDomain().isEmpty() ? QCoreApplication::organizationName() @@ -2710,6 +2724,25 @@ QSettings::QSettings(const QString &fileName, Format format) { d_ptr->q_ptr = this; } + +# ifndef QT_BUILD_QMAKE +QSettings::QSettings(Scope scope) + : d_ptr(QSettingsPrivate::create(globalDefaultFormat, scope, +# ifdef Q_OS_DARWIN + QCoreApplication::organizationDomain().isEmpty() + ? QCoreApplication::organizationName() + : QCoreApplication::organizationDomain() +# else + QCoreApplication::organizationName().isEmpty() + ? QCoreApplication::organizationDomain() + : QCoreApplication::organizationName() +# endif + , QCoreApplication::applicationName()) + ) +{ + d_ptr->q_ptr = this; +} +# endif #endif /*! diff --git a/src/corelib/io/qsettings.h b/src/corelib/io/qsettings.h index ccfec787a6..947507b642 100644 --- a/src/corelib/io/qsettings.h +++ b/src/corelib/io/qsettings.h @@ -132,6 +132,7 @@ public: const QString &application = QString(), QObject *parent = nullptr); QSettings(const QString &fileName, Format format, QObject *parent = nullptr); explicit QSettings(QObject *parent = nullptr); + explicit QSettings(Scope scope, QObject *parent = nullptr); #else explicit QSettings(const QString &organization, const QString &application = QString()); @@ -140,6 +141,9 @@ public: QSettings(Format format, Scope scope, const QString &organization, const QString &application = QString()); QSettings(const QString &fileName, Format format); +# ifndef QT_BUILD_QMAKE + explicit QSettings(Scope scope = UserScope); +# endif #endif ~QSettings();