28 bool
QgsSettings::setGlobalSettingsPath( const QString &path )
30 if ( QFileInfo::exists( path ) )
32 *sGlobalSettingsPath() = path;
38 void 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 for (
const auto &s : mGlobalSettings->allKeys() )
120 if ( ! keys.contains( s ) )
132 QStringList keys = mUserSettings->childKeys();
133 if ( mGlobalSettings )
135 for (
const auto &s : mGlobalSettings->childKeys() )
137 if ( ! keys.contains( s ) )
148 QStringList keys = mUserSettings->childGroups();
149 if ( mGlobalSettings )
151 for (
const auto &s : mGlobalSettings->childGroups() )
153 if ( ! keys.contains( s ) )
164 if ( mGlobalSettings )
166 keys = mGlobalSettings->childGroups();
173 return *sGlobalSettingsPath();
179 if ( !mUserSettings->value( pKey ).isNull() )
181 return mUserSettings->value( pKey );
183 if ( mGlobalSettings )
185 return mGlobalSettings->value( pKey, defaultValue );
193 return mUserSettings->contains( pKey ) ||
194 ( mGlobalSettings && mGlobalSettings->contains( pKey ) );
199 return mUserSettings->fileName();
204 mUserSettings->sync();
210 mUserSettings->remove( pKey );
219 prefix = QStringLiteral(
"core" );
221 case Section::Server:
222 prefix = QStringLiteral(
"server" );
225 prefix = QStringLiteral(
"gui" );
227 case Section::Plugins:
228 prefix = QStringLiteral(
"plugins" );
231 prefix = QStringLiteral(
"misc" );
234 prefix = QStringLiteral(
"auth" );
237 prefix = QStringLiteral(
"app" );
239 case Section::Providers:
240 prefix = QStringLiteral(
"providers" );
242 case Section::Expressions:
243 prefix = QStringLiteral(
"expressions" );
246 prefix = QStringLiteral(
"gps" );
248 case Section::NoSection:
249 return sanitizeKey( key );
251 return prefix +
"/" + sanitizeKey( key );
257 int size = mUserSettings->beginReadArray( sanitizeKey( prefix ) );
258 if ( 0 == size && mGlobalSettings )
260 size = mGlobalSettings->beginReadArray( sanitizeKey( prefix ) );
261 mUsingGlobalArray = ( size > 0 );
268 mUsingGlobalArray =
false;
269 mUserSettings->beginWriteArray( prefix, size );
274 mUserSettings->endArray();
275 if ( mGlobalSettings )
277 mGlobalSettings->endArray();
279 mUsingGlobalArray =
false;
284 if ( mGlobalSettings && mUsingGlobalArray )
286 mGlobalSettings->setArrayIndex( i );
290 mUserSettings->setArrayIndex( i );
303 if ( ( currentValue.isValid() ||
value.isValid() ) && ( currentValue !=
value ) )
311 else if ( mGlobalSettings && mGlobalSettings->value(
prefixedKey( key, section ) ) == currentValue )
313 mUserSettings->remove(
prefixedKey( key, section ) );
318 QString QgsSettings::sanitizeKey(
const QString &key )
const
320 return QDir::cleanPath( key );
325 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.
Q_GLOBAL_STATIC(QReadWriteLock, sDefinitionCacheLock)