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 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 );} );
138 QStringList keys = mUserSettings->childGroups();
139 if ( mGlobalSettings )
141 const QStringList constChildGroups = mGlobalSettings->childGroups();
142 std::copy_if( constChildGroups.constBegin(), constChildGroups.constEnd(), std::back_inserter( keys ), [&keys](
const QString & key ) {return !keys.contains( key );} );
149 if ( mGlobalSettings )
151 keys = mGlobalSettings->childGroups();
158 return *sGlobalSettingsPath();
164 if ( !mUserSettings->value( pKey ).isNull() )
166 return mUserSettings->value( pKey );
168 if ( mGlobalSettings )
170 return mGlobalSettings->value( pKey, defaultValue );
178 return mUserSettings->contains( pKey ) ||
179 ( mGlobalSettings && mGlobalSettings->contains( pKey ) );
184 return mUserSettings->fileName();
189 mUserSettings->sync();
195 mUserSettings->remove( pKey );
204 prefix = QStringLiteral(
"core" );
206 case Section::Server:
207 prefix = QStringLiteral(
"server" );
210 prefix = QStringLiteral(
"gui" );
212 case Section::Plugins:
213 prefix = QStringLiteral(
"plugins" );
216 prefix = QStringLiteral(
"misc" );
219 prefix = QStringLiteral(
"auth" );
222 prefix = QStringLiteral(
"app" );
224 case Section::Providers:
225 prefix = QStringLiteral(
"providers" );
227 case Section::Expressions:
228 prefix = QStringLiteral(
"expressions" );
231 prefix = QStringLiteral(
"gps" );
233 case Section::NoSection:
234 return sanitizeKey( key );
236 return prefix +
"/" + sanitizeKey( key );
242 int size = mUserSettings->beginReadArray( sanitizeKey( prefix ) );
243 if ( 0 == size && mGlobalSettings )
245 size = mGlobalSettings->beginReadArray( sanitizeKey( prefix ) );
246 mUsingGlobalArray = ( size > 0 );
253 mUsingGlobalArray =
false;
254 mUserSettings->beginWriteArray( prefix, size );
259 mUserSettings->endArray();
260 if ( mGlobalSettings )
262 mGlobalSettings->endArray();
264 mUsingGlobalArray =
false;
269 if ( mGlobalSettings && mUsingGlobalArray )
271 mGlobalSettings->setArrayIndex( i );
275 mUserSettings->setArrayIndex( i );
288 if ( ( currentValue.isValid() ||
value.isValid() ) && ( currentValue !=
value ) )
296 else if ( mGlobalSettings && mGlobalSettings->value(
prefixedKey( key, section ) ) == currentValue )
298 mUserSettings->remove(
prefixedKey( key, section ) );
303 QString QgsSettings::sanitizeKey(
const QString &key )
const
305 return QDir::cleanPath( key );
310 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)