33 if ( QFileInfo::exists( path ) )
35 *sGlobalSettingsPath() = path;
41void QgsSettings::init()
43 if ( ! sGlobalSettingsPath()->isEmpty() )
45 mGlobalSettings =
new QSettings( *sGlobalSettingsPath(), QSettings::IniFormat );
46#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
47 mGlobalSettings->setIniCodec(
"UTF-8" );
55 mUserSettings =
new QSettings( organization, application, parent );
60 const QString &application, QObject *parent )
62 mUserSettings =
new QSettings( scope, organization, application, parent );
67 const QString &organization,
const QString &application, QObject *parent )
69 mUserSettings =
new QSettings( format, scope, organization, application, parent );
75 mUserSettings =
new QSettings(
fileName, format, parent );
81 mUserSettings =
new QSettings( parent );
88 delete mGlobalSettings;
94 const QString pKey =
prefixedKey( prefix, section );
95 mUserSettings->beginGroup( pKey );
96 if ( mGlobalSettings )
98 mGlobalSettings->beginGroup( pKey );
104 mUserSettings->endGroup();
105 if ( mGlobalSettings )
107 mGlobalSettings->endGroup();
113 return mUserSettings->group();
118 QStringList keys = mUserSettings->allKeys();
119 if ( mGlobalSettings )
121 const QStringList constAllKeys = mGlobalSettings->allKeys();
122 std::copy_if( constAllKeys.constBegin(), constAllKeys.constEnd(), std::back_inserter( keys ), [&keys](
const QString & key ) {return !keys.contains( key );} );
130 QStringList keys = mUserSettings->childKeys();
131 if ( mGlobalSettings )
133 const QStringList constChildKeys = mGlobalSettings->childKeys();
134 std::copy_if( constChildKeys.constBegin(), constChildKeys.constEnd(), std::back_inserter( keys ), [&keys](
const QString & key ) {return !keys.contains( key );} );
145 QStringList keys = mUserSettings->childGroups();
146 if ( mGlobalSettings )
148 const QStringList constChildGroups = mGlobalSettings->childGroups();
149 std::copy_if( constChildGroups.constBegin(), constChildGroups.constEnd(), std::back_inserter( keys ), [&keys](
const QString & key ) {return !keys.contains( key );} );
155 return mUserSettings->childGroups();
158 return mGlobalSettings ? mGlobalSettings->childGroups() : QStringList();
171 return *sGlobalSettingsPath();
179 return mUserSettings->value( pKey );
181 if ( mGlobalSettings )
183 return mGlobalSettings->value( pKey, defaultValue );
191 return mUserSettings->contains( pKey ) ||
192 ( mGlobalSettings && mGlobalSettings->contains( pKey ) );
197 return mUserSettings->fileName();
202 mUserSettings->sync();
208 mUserSettings->remove( pKey );
217 prefix = QStringLiteral(
"core" );
220 prefix = QStringLiteral(
"server" );
223 prefix = QStringLiteral(
"gui" );
226 prefix = QStringLiteral(
"plugins" );
229 prefix = QStringLiteral(
"misc" );
232 prefix = QStringLiteral(
"auth" );
235 prefix = QStringLiteral(
"app" );
238 prefix = QStringLiteral(
"providers" );
241 prefix = QStringLiteral(
"expressions" );
244 prefix = QStringLiteral(
"gps" );
247 return sanitizeKey( key );
249 return prefix +
"/" + sanitizeKey( key );
255 int size = mUserSettings->beginReadArray( sanitizeKey( prefix ) );
256 if ( 0 == size && mGlobalSettings )
258 size = mGlobalSettings->beginReadArray( sanitizeKey( prefix ) );
259 mUsingGlobalArray = ( size > 0 );
266 mUsingGlobalArray =
false;
267 mUserSettings->beginWriteArray( prefix, size );
272 mUserSettings->endArray();
273 if ( mGlobalSettings )
275 mGlobalSettings->endArray();
277 mUsingGlobalArray =
false;
282 if ( mGlobalSettings && mUsingGlobalArray )
284 mGlobalSettings->setArrayIndex( i );
288 mUserSettings->setArrayIndex( i );
294 if ( mGlobalSettings && mGlobalSettings->contains( key ) )
297 if ( mUserSettings->contains( key ) )
312 if ( ( currentValue.isValid() ||
value.isValid() ) && ( currentValue !=
value ) )
320 else if ( mGlobalSettings && mGlobalSettings->value(
prefixedKey( key, section ) ) == currentValue )
322 mUserSettings->remove(
prefixedKey( key, section ) );
327QString QgsSettings::sanitizeKey(
const QString &key )
const
329 return QDir::cleanPath( key );
334 mUserSettings->clear();
SettingsOrigin
The setting origin describes where a setting is stored.
@ Global
Global settings are stored in qgis_global_settings.ini
@ Local
Local settings are stored in the user profile.
A helper class for access to either a temporary QgsSettings object or the thread local object.
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...
static void releaseFlush()
Releases a previously made hold on flushing QgsSettings objects and writing new values to the underly...
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.
@ Gps
GPS section, since QGIS 3.22.
static void holdFlush()
Temporarily places a hold on flushing QgsSettings objects and writing new values to the underlying in...
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.
static QgsSettingsProxy get()
Returns a proxy for a QgsSettings object.
void setArrayIndex(int i)
Sets the current array index to i.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
#define BUILTIN_UNREACHABLE
Q_GLOBAL_STATIC(QReadWriteLock, sDefinitionCacheLock)
thread_local QgsSettings * sQgsSettingsThreadSettings
thread_local QgsSettings * sQgsSettingsThreadSettings