31 if ( QFileInfo::exists( path ) )
33 *sGlobalSettingsPath() = path;
39void QgsSettings::init()
41 if ( ! sGlobalSettingsPath()->isEmpty() )
43 mGlobalSettings =
new QSettings( *sGlobalSettingsPath(), QSettings::IniFormat );
44#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
45 mGlobalSettings->setIniCodec(
"UTF-8" );
53 mUserSettings =
new QSettings( organization, application, parent );
58 const QString &application, QObject *parent )
60 mUserSettings =
new QSettings( scope, organization, application, parent );
65 const QString &organization,
const QString &application, QObject *parent )
67 mUserSettings =
new QSettings( format, scope, organization, application, parent );
73 mUserSettings =
new QSettings(
fileName, format, parent );
79 mUserSettings =
new QSettings( parent );
86 delete mGlobalSettings;
92 const QString pKey =
prefixedKey( prefix, section );
93 mUserSettings->beginGroup( pKey );
94 if ( mGlobalSettings )
96 mGlobalSettings->beginGroup( pKey );
102 mUserSettings->endGroup();
103 if ( mGlobalSettings )
105 mGlobalSettings->endGroup();
111 return mUserSettings->group();
116 QStringList keys = mUserSettings->allKeys();
117 if ( mGlobalSettings )
119 const QStringList constAllKeys = mGlobalSettings->allKeys();
120 std::copy_if( constAllKeys.constBegin(), constAllKeys.constEnd(), std::back_inserter( keys ), [&keys](
const QString & key ) {return !keys.contains( key );} );
128 QStringList keys = mUserSettings->childKeys();
129 if ( mGlobalSettings )
131 const QStringList constChildKeys = mGlobalSettings->childKeys();
132 std::copy_if( constChildKeys.constBegin(), constChildKeys.constEnd(), std::back_inserter( keys ), [&keys](
const QString & key ) {return !keys.contains( key );} );
139 QStringList keys = mUserSettings->childGroups();
140 if ( mGlobalSettings )
142 const QStringList constChildGroups = mGlobalSettings->childGroups();
143 std::copy_if( constChildGroups.constBegin(), constChildGroups.constEnd(), std::back_inserter( keys ), [&keys](
const QString & key ) {return !keys.contains( key );} );
150 if ( mGlobalSettings )
152 keys = mGlobalSettings->childGroups();
159 return *sGlobalSettingsPath();
167 return mUserSettings->value( pKey );
169 if ( mGlobalSettings )
171 return mGlobalSettings->value( pKey, defaultValue );
179 return mUserSettings->contains( pKey ) ||
180 ( mGlobalSettings && mGlobalSettings->contains( pKey ) );
185 return mUserSettings->fileName();
190 mUserSettings->sync();
196 mUserSettings->remove( pKey );
205 prefix = QStringLiteral(
"core" );
207 case Section::Server:
208 prefix = QStringLiteral(
"server" );
211 prefix = QStringLiteral(
"gui" );
213 case Section::Plugins:
214 prefix = QStringLiteral(
"plugins" );
217 prefix = QStringLiteral(
"misc" );
220 prefix = QStringLiteral(
"auth" );
223 prefix = QStringLiteral(
"app" );
225 case Section::Providers:
226 prefix = QStringLiteral(
"providers" );
228 case Section::Expressions:
229 prefix = QStringLiteral(
"expressions" );
232 prefix = QStringLiteral(
"gps" );
234 case Section::NoSection:
235 return sanitizeKey( key );
237 return prefix +
"/" + sanitizeKey( key );
243 int size = mUserSettings->beginReadArray( sanitizeKey( prefix ) );
244 if ( 0 == size && mGlobalSettings )
246 size = mGlobalSettings->beginReadArray( sanitizeKey( prefix ) );
247 mUsingGlobalArray = ( size > 0 );
254 mUsingGlobalArray =
false;
255 mUserSettings->beginWriteArray( prefix, size );
260 mUserSettings->endArray();
261 if ( mGlobalSettings )
263 mGlobalSettings->endArray();
265 mUsingGlobalArray =
false;
270 if ( mGlobalSettings && mUsingGlobalArray )
272 mGlobalSettings->setArrayIndex( i );
276 mUserSettings->setArrayIndex( i );
289 if ( ( currentValue.isValid() ||
value.isValid() ) && ( currentValue !=
value ) )
297 else if ( mGlobalSettings && mGlobalSettings->value(
prefixedKey( key, section ) ) == currentValue )
299 mUserSettings->remove(
prefixedKey( key, section ) );
304QString QgsSettings::sanitizeKey(
const QString &key )
const
306 return QDir::cleanPath( key );
311 mUserSettings->clear();
This class is a composition of two QSettings instances:
QStringList childGroups() const
Returns a list of all key top-level groups that contain keys that can be read using the QSettings obj...
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.
Q_GLOBAL_STATIC(QReadWriteLock, sDefinitionCacheLock)