30 if ( QFileInfo::exists( path ) )
32 *sGlobalSettingsPath() = path;
38void QgsSettings::init()
40 if ( ! sGlobalSettingsPath()->isEmpty() )
42 mGlobalSettings =
new QSettings( *sGlobalSettingsPath(), QSettings::IniFormat );
43#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
44 mGlobalSettings->setIniCodec(
"UTF-8" );
52 mUserSettings =
new QSettings( organization, application, parent );
57 const QString &application, QObject *parent )
59 mUserSettings =
new QSettings( scope, organization, application, parent );
64 const QString &organization,
const QString &application, QObject *parent )
66 mUserSettings =
new QSettings( format, scope, organization, application, parent );
72 mUserSettings =
new QSettings(
fileName, format, parent );
78 mUserSettings =
new QSettings( parent );
85 delete mGlobalSettings;
91 const QString pKey =
prefixedKey( prefix, section );
92 mUserSettings->beginGroup( pKey );
93 if ( mGlobalSettings )
95 mGlobalSettings->beginGroup( pKey );
101 mUserSettings->endGroup();
102 if ( mGlobalSettings )
104 mGlobalSettings->endGroup();
110 return mUserSettings->group();
115 QStringList keys = mUserSettings->allKeys();
116 if ( mGlobalSettings )
118 const QStringList constAllKeys = mGlobalSettings->allKeys();
119 std::copy_if( constAllKeys.constBegin(), constAllKeys.constEnd(), std::back_inserter( keys ), [&keys](
const QString & key ) {return !keys.contains( key );} );
127 QStringList keys = mUserSettings->childKeys();
128 if ( mGlobalSettings )
130 const QStringList constChildKeys = mGlobalSettings->childKeys();
131 std::copy_if( constChildKeys.constBegin(), constChildKeys.constEnd(), std::back_inserter( keys ), [&keys](
const QString & key ) {return !keys.contains( key );} );
142 QStringList keys = mUserSettings->childGroups();
143 if ( mGlobalSettings )
145 const QStringList constChildGroups = mGlobalSettings->childGroups();
146 std::copy_if( constChildGroups.constBegin(), constChildGroups.constEnd(), std::back_inserter( keys ), [&keys](
const QString & key ) {return !keys.contains( key );} );
152 return mUserSettings->childGroups();
155 return mGlobalSettings ? mGlobalSettings->childGroups() : QStringList();
168 return *sGlobalSettingsPath();
176 return mUserSettings->value( pKey );
178 if ( mGlobalSettings )
180 return mGlobalSettings->value( pKey, defaultValue );
188 return mUserSettings->contains( pKey ) ||
189 ( mGlobalSettings && mGlobalSettings->contains( pKey ) );
194 return mUserSettings->fileName();
199 mUserSettings->sync();
205 mUserSettings->remove( pKey );
214 prefix = QStringLiteral(
"core" );
216 case Section::Server:
217 prefix = QStringLiteral(
"server" );
220 prefix = QStringLiteral(
"gui" );
222 case Section::Plugins:
223 prefix = QStringLiteral(
"plugins" );
226 prefix = QStringLiteral(
"misc" );
229 prefix = QStringLiteral(
"auth" );
232 prefix = QStringLiteral(
"app" );
234 case Section::Providers:
235 prefix = QStringLiteral(
"providers" );
237 case Section::Expressions:
238 prefix = QStringLiteral(
"expressions" );
241 prefix = QStringLiteral(
"gps" );
243 case Section::NoSection:
244 return sanitizeKey( key );
246 return prefix +
"/" + sanitizeKey( key );
252 int size = mUserSettings->beginReadArray( sanitizeKey( prefix ) );
253 if ( 0 == size && mGlobalSettings )
255 size = mGlobalSettings->beginReadArray( sanitizeKey( prefix ) );
256 mUsingGlobalArray = ( size > 0 );
263 mUsingGlobalArray =
false;
264 mUserSettings->beginWriteArray( prefix, size );
269 mUserSettings->endArray();
270 if ( mGlobalSettings )
272 mGlobalSettings->endArray();
274 mUsingGlobalArray =
false;
279 if ( mGlobalSettings && mUsingGlobalArray )
281 mGlobalSettings->setArrayIndex( i );
285 mUserSettings->setArrayIndex( i );
291 if ( mGlobalSettings && mGlobalSettings->contains( key ) )
294 if ( mUserSettings->contains( key ) )
309 if ( ( currentValue.isValid() ||
value.isValid() ) && ( currentValue !=
value ) )
317 else if ( mGlobalSettings && mGlobalSettings->value(
prefixedKey( key, section ) ) == currentValue )
319 mUserSettings->remove(
prefixedKey( key, section ) );
324QString QgsSettings::sanitizeKey(
const QString &key )
const
326 return QDir::cleanPath( key );
331 mUserSettings->clear();
SettingsOrigin
The setting origin describes where a setting is stored.
@ Global
Global settings are stored in global_settings.ini
@ Local
Local settings are stored in the user profile.
This class is a composition of two QSettings instances:
QStringList childGroups(Qgis::SettingsOrigin origin=Qgis::SettingsOrigin::Any) const
Returns a list of all key top-level groups that contain keys that can be read using the QSettings obj...
Qgis::SettingsOrigin origin(const QString &key) const
Returns the origin of the setting if it exists at the given key.
void endGroup()
Resets the group to what it was before the corresponding beginGroup() call.
void clear()
Removes all entries in the user settings.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
QString prefixedKey(const QString &key, QgsSettings::Section section) const
Returns the sanitized and prefixed key.
void endArray()
Closes the array that was started using beginReadArray() or beginWriteArray().
QString group() const
Returns the current group.
void beginGroup(const QString &prefix, QgsSettings::Section section=QgsSettings::NoSection)
Appends prefix to the current group.
QStringList childKeys() const
Returns a list of all top-level keys that can be read using the QSettings object.
static QString globalSettingsPath()
Returns the path to the Global Settings QSettings storage file.
QgsSettings(const QString &organization, const QString &application=QString(), QObject *parent=nullptr)
Constructs a QgsSettings object for accessing settings of the application called application from the...
void sync()
Writes any unsaved changes to permanent storage, and reloads any settings that have been changed in t...
QStringList globalChildGroups() const
Returns a list of all key top-level groups (same as childGroups) but only for groups defined in globa...
void beginWriteArray(const QString &prefix, int size=-1)
Adds prefix to the current group and starts writing an array of size size.
Section
Sections for namespaced settings.
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
QString fileName() const
Returns the path where settings written using this QSettings object are stored.
QStringList allKeys() const
Returns a list of all keys, including subkeys, that can be read using the QSettings object.
int beginReadArray(const QString &prefix)
Adds prefix to the current group and starts reading from an array. Returns the size of the array.
void setArrayIndex(int i)
Sets the current array index to i.
static bool isNull(const QVariant &variant)
Returns true if the specified variant should be considered a NULL value.
#define BUILTIN_UNREACHABLE
Q_GLOBAL_STATIC(QReadWriteLock, sDefinitionCacheLock)