Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QSettings Class Reference

\inmodule QtCore More...

#include <qsettings.h>

+ Inheritance diagram for QSettings:
+ Collaboration diagram for QSettings:

Public Types

enum  Status { NoError = 0 , AccessError , FormatError }
 The following status values are possible: More...
 
enum  Format {
  NativeFormat = 0 , IniFormat = 1 , InvalidFormat = 16 , CustomFormat1 ,
  CustomFormat2 , CustomFormat3 , CustomFormat4 , CustomFormat5 ,
  CustomFormat6 , CustomFormat7 , CustomFormat8 , CustomFormat9 ,
  CustomFormat10 , CustomFormat11 , CustomFormat12 , CustomFormat13 ,
  CustomFormat14 , CustomFormat15 , CustomFormat16
}
 This enum type specifies the storage format used by QSettings. More...
 
enum  Scope { UserScope , SystemScope }
 This enum specifies whether settings are user-specific or shared by all users of the same system. More...
 
typedef QMap< QString, QVariantSettingsMap
 Typedef for QMap<QString, QVariant>.
 
typedef bool(* ReadFunc) (QIODevice &device, SettingsMap &map)
 Typedef for a pointer to a function with the following signature:
 
typedef bool(* WriteFunc) (QIODevice &device, const SettingsMap &map)
 Typedef for a pointer to a function with the following signature:
 

Public Member Functions

 QSettings (const QString &organization, const QString &application=QString(), QObject *parent=nullptr)
 Constructs a QSettings object for accessing settings of the application called application from the organization called organization, and with parent parent.
 
 QSettings (Scope scope, const QString &organization, const QString &application=QString(), QObject *parent=nullptr)
 Constructs a QSettings object for accessing settings of the application called application from the organization called organization, and with parent parent.
 
 QSettings (Format format, Scope scope, const QString &organization, const QString &application=QString(), QObject *parent=nullptr)
 Constructs a QSettings object for accessing settings of the application called application from the organization called organization, and with parent parent.
 
 QSettings (const QString &fileName, Format format, QObject *parent=nullptr)
 Constructs a QSettings object for accessing the settings stored in the file called fileName, with parent parent.
 
 QSettings (QObject *parent=nullptr)
 Constructs a QSettings object for accessing settings of the application and organization set previously with a call to QCoreApplication::setOrganizationName(), QCoreApplication::setOrganizationDomain(), and QCoreApplication::setApplicationName().
 
 QSettings (Scope scope, QObject *parent=nullptr)
 
 ~QSettings ()
 Destroys the QSettings object.
 
void clear ()
 Removes all entries in the primary location associated to this QSettings object.
 
void sync ()
 Writes any unsaved changes to permanent storage, and reloads any settings that have been changed in the meantime by another application.
 
Status status () const
 Returns a status code indicating the first error that was met by QSettings, or QSettings::NoError if no error occurred.
 
bool isAtomicSyncRequired () const
 
void setAtomicSyncRequired (bool enable)
 
void beginGroup (QAnyStringView prefix)
 Appends prefix to the current group.
 
void endGroup ()
 Resets the group to what it was before the corresponding beginGroup() call.
 
QString group () const
 Returns the current group.
 
int beginReadArray (QAnyStringView prefix)
 Adds prefix to the current group and starts reading from an array.
 
void beginWriteArray (QAnyStringView prefix, int size=-1)
 Adds prefix to the current group and starts writing an array of size size.
 
void endArray ()
 Closes the array that was started using beginReadArray() or beginWriteArray().
 
void setArrayIndex (int i)
 Sets the current array index to i.
 
QStringList allKeys () const
 Returns a list of all keys, including subkeys, that can be read using the QSettings object.
 
QStringList childKeys () const
 Returns a list of all top-level keys that can be read using the QSettings object.
 
QStringList childGroups () const
 Returns a list of all key top-level groups that contain keys that can be read using the QSettings object.
 
bool isWritable () const
 Returns true if settings can be written using this QSettings object; returns false otherwise.
 
void setValue (QAnyStringView key, const QVariant &value)
 Sets the value of setting key to value.
 
QVariant value (QAnyStringView key, const QVariant &defaultValue) const
 Returns the value for setting key.
 
QVariant value (QAnyStringView key) const
 
void remove (QAnyStringView key)
 Removes the setting key and any sub-settings of key.
 
bool contains (QAnyStringView key) const
 Returns true if there exists a setting called key; returns false otherwise.
 
void setFallbacksEnabled (bool b)
 Sets whether fallbacks are enabled to b.
 
bool fallbacksEnabled () const
 Returns true if fallbacks are enabled; returns false otherwise.
 
QString fileName () const
 Returns the path where settings written using this QSettings object are stored.
 
Format format () const
 
Scope scope () const
 
QString organizationName () const
 
QString applicationName () const
 
- Public Member Functions inherited from QObject
Q_INVOKABLE QObject (QObject *parent=nullptr)
 Constructs an object with parent object parent.
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects.
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 Filters events if this object has been installed as an event filter for the watched object.
 
QString objectName () const
 
Q_WEAK_OVERLOAD void setObjectName (const QString &name)
 Sets the object's name to name.
 
void setObjectName (QAnyStringView name)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QBindable< QStringbindableObjectName ()
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false.
 
bool isWindowType () const
 Returns true if the object is a window; otherwise returns false.
 
bool isQuickItemType () const
 Returns true if the object is a QQuickItem; otherwise returns false.
 
bool signalsBlocked () const noexcept
 Returns true if signals are blocked; otherwise returns false.
 
bool blockSignals (bool b) noexcept
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it).
 
QThreadthread () const
 Returns the thread in which the object lives.
 
bool moveToThread (QThread *thread QT6_DECL_NEW_OVERLOAD_TAIL)
 Changes the thread affinity for this object and its children and returns true on success.
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 This is an overloaded function that will start a timer of type timerType and a timeout of interval milliseconds.
 
int startTimer (std::chrono::nanoseconds time, Qt::TimerType timerType=Qt::CoarseTimer)
 
void killTimer (int id)
 Kills the timer with timer identifier, id.
 
void killTimer (Qt::TimerId id)
 
template<typename T >
findChild (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 Returns the child of this object that can be cast into type T and that is called name, or \nullptr if there is no such object.
 
template<typename T >
QList< T > findChildren (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects.
 
template<typename T >
findChild (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<typename T >
QList< T > findChildren (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
const QObjectListchildren () const
 Returns a list of child objects.
 
void setParent (QObject *parent)
 Makes the object a child of parent.
 
void installEventFilter (QObject *filterObj)
 Installs an event filter filterObj on this object.
 
void removeEventFilter (QObject *obj)
 Removes an event filter object obj from this object.
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=nullptr, const QObject *receiver=nullptr, const char *member=nullptr) const
 
bool disconnect (const QObject *receiver, const char *member=nullptr) const
 
void dumpObjectTree () const
 Dumps a tree of children to the debug output.
 
void dumpObjectInfo () const
 Dumps information about signal connections, etc.
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value.
 
bool setProperty (const char *name, QVariant &&value)
 
QVariant property (const char *name) const
 Returns the value of the object's name property.
 
QList< QByteArraydynamicPropertyNames () const
 
QBindingStoragebindingStorage ()
 
const QBindingStoragebindingStorage () const
 
QObjectparent () const
 Returns a pointer to the parent object.
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false.
 

Static Public Member Functions

static void setDefaultFormat (Format format)
 
static Format defaultFormat ()
 
static void setPath (Format format, Scope scope, const QString &path)
 
static Format registerFormat (const QString &extension, ReadFunc readFunc, WriteFunc writeFunc, Qt::CaseSensitivity caseSensitivity=Qt::CaseSensitive)
 
- Static Public Member Functions inherited from QObject
static QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 \threadsafe
 
static QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::ContextTypeForFunctor< Func2 >::ContextType *context, Func2 &&slot, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 &&slot)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 \threadsafe
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static bool disconnect (const QMetaObject::Connection &)
 Disconnect a connection.
 
template<typename Func1 , typename Func2 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot)
 
template<typename Func1 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *receiver, void **zero)
 

Protected Member Functions

bool event (QEvent *event) override
 \reimp
 
- Protected Member Functions inherited from QObject
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns \nullptr.
 
int senderSignalIndex () const
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal.
 
bool isSignalConnected (const QMetaMethod &signal) const
 
virtual void timerEvent (QTimerEvent *event)
 This event handler can be reimplemented in a subclass to receive timer events for the object.
 
virtual void childEvent (QChildEvent *event)
 This event handler can be reimplemented in a subclass to receive child events.
 
virtual void customEvent (QEvent *event)
 This event handler can be reimplemented in a subclass to receive custom events.
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 
 QObject (QObjectPrivate &dd, QObject *parent=nullptr)
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 \threadsafe
 
- Signals inherited from QObject
void destroyed (QObject *=nullptr)
 This signal is emitted immediately before the object obj is destroyed, after any instances of QPointer have been notified, and cannot be blocked.
 
void objectNameChanged (const QString &objectName, QPrivateSignal)
 This signal is emitted after the object's name has been changed.
 
- Protected Attributes inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Properties inherited from QObject
QString objectName
 the name of this object
 

Detailed Description

\inmodule QtCore

The QSettings class provides persistent platform-independent application settings.

\reentrant

Users normally expect an application to remember its settings (window sizes and positions, options, etc.) across sessions. This information is often stored in the system registry on Windows, and in property list files on \macos and iOS. On Unix systems, in the absence of a standard, many applications (including the KDE applications) use INI text files.

QSettings is an abstraction around these technologies, enabling you to save and restore application settings in a portable manner. It also supports \l{registerFormat()}{custom storage formats}.

QSettings's API is based on QVariant, allowing you to save most value-based types, such as QString, QRect, and QImage, with the minimum of effort.

If all you need is a non-persistent memory-based structure, consider using QMap<QString, QVariant> instead.

section1

Definition at line 26 of file qsettings.h.

Member Typedef Documentation

◆ ReadFunc

QSettings::ReadFunc

Typedef for a pointer to a function with the following signature:

ReadFunc is used in registerFormat() as a pointer to a function that reads a set of key/value pairs. ReadFunc should read all the options in one pass, and return all the settings in the SettingsMap container, which is initially empty.

See also
WriteFunc, registerFormat()

Definition at line 171 of file qsettings.h.

◆ SettingsMap

Typedef for QMap<QString, QVariant>.

See also
registerFormat()

Definition at line 170 of file qsettings.h.

◆ WriteFunc

QSettings::WriteFunc

Typedef for a pointer to a function with the following signature:

WriteFunc is used in registerFormat() as a pointer to a function that writes a set of key/value pairs. WriteFunc is only called once, so you need to output the settings in one go.

See also
ReadFunc, registerFormat()

Definition at line 172 of file qsettings.h.

Member Enumeration Documentation

◆ Format

This enum type specifies the storage format used by QSettings.

\value NativeFormat Store the settings using the most appropriate storage format for the platform. On Windows, this means the system registry; on \macos and iOS, this means the CFPreferences API; on Unix, this means textual configuration files in INI format. \value Registry32Format Windows only: Explicitly access the 32-bit system registry from a 64-bit application running on 64-bit Windows. On 32-bit Windows or from a 32-bit application on 64-bit Windows, this works the same as specifying NativeFormat. This enum value was added in Qt 5.7. \value Registry64Format Windows only: Explicitly access the 64-bit system registry from a 32-bit application running on 64-bit Windows. On 32-bit Windows or from a 64-bit application on 64-bit Windows, this works the same as specifying NativeFormat. This enum value was added in Qt 5.7. \value IniFormat Store the settings in INI files. Note that INI files lose the distinction between numeric data and the strings used to encode them, so values written as numbers shall be read back as QString. \value WebLocalStorageFormat WASM only: Store the settings in window.localStorage for the current origin. If cookies are not allowed, this falls back to the INI format. This provides up to 5MiB storage per origin, but access to it is synchronous and JSPI is not required. \value WebIndexedDBFormat WASM only: Store the settings in an Indexed DB for the current origin. If cookies are not allowed, this falls back to the INI format. This requires JSPI, but provides more storage than WebLocalStorageFormat.

\value InvalidFormat Special value returned by registerFormat(). \omitvalue CustomFormat1 \omitvalue CustomFormat2 \omitvalue CustomFormat3 \omitvalue CustomFormat4 \omitvalue CustomFormat5 \omitvalue CustomFormat6 \omitvalue CustomFormat7 \omitvalue CustomFormat8 \omitvalue CustomFormat9 \omitvalue CustomFormat10 \omitvalue CustomFormat11 \omitvalue CustomFormat12 \omitvalue CustomFormat13 \omitvalue CustomFormat14 \omitvalue CustomFormat15 \omitvalue CustomFormat16

On Unix, NativeFormat and IniFormat mean the same thing, except that the file extension is different (.conf for NativeFormat, .ini for IniFormat).

The INI file format is a Windows file format that Qt supports on all platforms. In the absence of an INI standard, we try to follow what Microsoft does, with the following exceptions:

\list

To minimize compatibility issues, any @ that doesn't appear at the first position in the value or that isn't followed by a Qt type (Point, Rect, Size, etc.) is treated as a normal character.

  • Although backslash is a special character in INI files, most Windows applications don't escape backslashes ({\}) in file paths:

QSettings always treats backslash as a special character and provides no API for reading or writing such entries.

  • The INI file format has severe restrictions on the syntax of a key. Qt works around this by using % as an escape character in keys. In addition, if you save a top-level setting (a key with no slashes in it, e.g., "someKey"), it will appear in the INI file's "General" section. To avoid overwriting other keys, if you save something using a key such as "General/someKey", the key will be located in the "%General" section, not in the "General" section.
  • In line with most implementations today, QSettings will assume that values in the INI file are utf-8 encoded. This means that values will be decoded as utf-8 encoded entries and written back as utf-8. To retain backward compatibility with older Qt versions, keys in the INI file are written in %-encoded format, but can be read in both %-encoded and utf-8 formats.

\endlist

Enumerator
NativeFormat 
IniFormat 
InvalidFormat 
CustomFormat1 
CustomFormat2 
CustomFormat3 
CustomFormat4 
CustomFormat5 
CustomFormat6 
CustomFormat7 
CustomFormat8 
CustomFormat9 
CustomFormat10 
CustomFormat11 
CustomFormat12 
CustomFormat13 
CustomFormat14 
CustomFormat15 
CustomFormat16 

Definition at line 48 of file qsettings.h.

◆ Scope

This enum specifies whether settings are user-specific or shared by all users of the same system.

\value UserScope Store settings in a location specific to the current user (e.g., in the user's home directory). \value SystemScope Store settings in a global location, so that all users on the same machine access the same set of settings.

See also
setPath()
Enumerator
UserScope 
SystemScope 

Definition at line 84 of file qsettings.h.

◆ Status

The following status values are possible:

\value NoError No error occurred. \value AccessError An access error occurred (e.g. trying to write to a read-only file). \value FormatError A format error occurred (e.g. loading a malformed INI file).

See also
status()
Enumerator
NoError 
AccessError 
FormatError 

Definition at line 39 of file qsettings.h.

Constructor & Destructor Documentation

◆ QSettings() [1/6]

QSettings::QSettings ( const QString & organization,
const QString & application = QString(),
QObject * parent = nullptr )
explicit

Constructs a QSettings object for accessing settings of the application called application from the organization called organization, and with parent parent.

Example:

The scope is set to QSettings::UserScope, and the format is set to QSettings::NativeFormat (i.e. calling setDefaultFormat() before calling this constructor has no effect).

See also
setDefaultFormat(), {Fallback Mechanism}

Definition at line 2527 of file qsettings.cpp.

◆ QSettings() [2/6]

QSettings::QSettings ( Scope scope,
const QString & organization,
const QString & application = QString(),
QObject * parent = nullptr )

Constructs a QSettings object for accessing settings of the application called application from the organization called organization, and with parent parent.

If scope is QSettings::UserScope, the QSettings object searches user-specific settings first, before it searches system-wide settings as a fallback. If scope is QSettings::SystemScope, the QSettings object ignores user-specific settings and provides access to system-wide settings.

The storage format is set to QSettings::NativeFormat (i.e. calling setDefaultFormat() before calling this constructor has no effect).

If no application name is given, the QSettings object will only access the organization-wide \l{Fallback Mechanism}{locations}.

See also
setDefaultFormat()

Definition at line 2552 of file qsettings.cpp.

◆ QSettings() [3/6]

QSettings::QSettings ( Format format,
Scope scope,
const QString & organization,
const QString & application = QString(),
QObject * parent = nullptr )

Constructs a QSettings object for accessing settings of the application called application from the organization called organization, and with parent parent.

If scope is QSettings::UserScope, the QSettings object searches user-specific settings first, before it searches system-wide settings as a fallback. If scope is QSettings::SystemScope, the QSettings object ignores user-specific settings and provides access to system-wide settings.

If format is QSettings::NativeFormat, the native API is used for storing settings. If format is QSettings::IniFormat, the INI format is used.

If no application name is given, the QSettings object will only access the organization-wide \l{Fallback Mechanism}{locations}.

Definition at line 2576 of file qsettings.cpp.

◆ QSettings() [4/6]

QSettings::QSettings ( const QString & fileName,
Format format,
QObject * parent = nullptr )

Constructs a QSettings object for accessing the settings stored in the file called fileName, with parent parent.

If the file doesn't already exist, it is created.

If format is QSettings::NativeFormat, the meaning of fileName depends on the platform. On Unix, fileName is the name of an INI file. On \macos and iOS, fileName is the name of a .plist file. On Windows, fileName is a path in the system registry.

If format is QSettings::IniFormat, fileName is the name of an INI file.

Warning
This function is provided for convenience. It works well for accessing INI or .plist files generated by Qt, but might fail on some syntaxes found in such files originated by other programs. In particular, be aware of the following limitations:

\list

  • QSettings provides no way of reading INI "path" entries, i.e., entries with unescaped slash characters. (This is because these entries are ambiguous and cannot be resolved automatically.)
  • In INI files, QSettings uses the @ character as a metacharacter in some contexts, to encode Qt-specific data types (e.g., @Rect ), and might therefore misinterpret it when it occurs in pure INI files. \endlist
See also
fileName()

Definition at line 2612 of file qsettings.cpp.

◆ QSettings() [5/6]

QSettings::QSettings ( QObject * parent = nullptr)
explicit

Constructs a QSettings object for accessing settings of the application and organization set previously with a call to QCoreApplication::setOrganizationName(), QCoreApplication::setOrganizationDomain(), and QCoreApplication::setApplicationName().

The scope is QSettings::UserScope and the format is defaultFormat() (QSettings::NativeFormat by default). Use setDefaultFormat() before calling this constructor to change the default format used by this constructor.

The code

is equivalent to

QCoreApplication::setOrganizationName("Moose Soft");
QCoreApplication::setApplicationName("Facturo-Pro");

If QCoreApplication::setOrganizationName() and QCoreApplication::setApplicationName() has not been previously called, the QSettings object will not be able to read or write any settings, and status() will return AccessError.

You should supply both the domain (used by default on \macos and iOS) and the name (used by default elsewhere), although the code will cope if you supply only one, which will then be used (on all platforms), at odds with the usual naming of the file on platforms for which it isn't the default.

See also
QCoreApplication::setOrganizationName(), QCoreApplication::setOrganizationDomain(), QCoreApplication::setApplicationName(), setDefaultFormat()

Definition at line 2652 of file qsettings.cpp.

◆ QSettings() [6/6]

QSettings::QSettings ( Scope scope,
QObject * parent = nullptr )
explicit
Since
5.13

Constructs a QSettings object in the same way as QSettings(QObject *parent) but with the given scope.

See also
QSettings(QObject *parent)

Definition at line 2665 of file qsettings.cpp.

◆ ~QSettings()

QSettings::~QSettings ( )

Destroys the QSettings object.

Any unsaved changes will eventually be written to permanent storage.

See also
sync()

Definition at line 2733 of file qsettings.cpp.

References d, QT_CATCH, and QT_TRY.

Member Function Documentation

◆ allKeys()

QStringList QSettings::allKeys ( ) const

Returns a list of all keys, including subkeys, that can be read using the QSettings object.

Example:

settings.setValue("fridge/color", QColor(Qt::white));
settings.setValue("fridge/size", QSize(32, 96));
settings.setValue("sofa", true);
settings.setValue("tv", false);
// keys: ["fridge/color", "fridge/size", "sofa", "tv"]

If a group is set using beginGroup(), only the keys in the group are returned, without the group prefix:

settings.beginGroup("fridge");
// keys: ["color", "size"]
See also
childGroups(), childKeys()

Definition at line 3111 of file qsettings.cpp.

References QSettingsPrivate::AllKeys, and d.

◆ applicationName()

QString QSettings::applicationName ( ) const
Since
4.4

Returns the application name used for storing the settings.

See also
QCoreApplication::applicationName(), format(), scope(), organizationName()

Definition at line 2843 of file qsettings.cpp.

References d.

◆ beginGroup()

void QSettings::beginGroup ( QAnyStringView prefix)

Appends prefix to the current group.

The current group is automatically prepended to all keys specified to QSettings. In addition, query functions such as childGroups(), childKeys(), and allKeys() are based on the group. By default, no group is set.

Groups are useful to avoid typing in the same setting paths over and over. For example:

settings.beginGroup("mainwindow");
settings.setValue("fullScreen", win->isFullScreen());
settings.beginGroup("outputpanel");

This will set the value of three settings:

\list

  • mainwindow/size
  • mainwindow/fullScreen
  • outputpanel/visible \endlist

Call endGroup() to reset the current group to what it was before the corresponding beginGroup() call. Groups can be nested.

Note
In Qt versions prior to 6.4, this function took QString, not QAnyStringView.
See also
endGroup(), group()

Definition at line 2938 of file qsettings.cpp.

References d.

Referenced by QQmlSettingsPrivate::instance(), QQuickStylePrivate::settings(), and snippet_ctor2().

+ Here is the caller graph for this function:

◆ beginReadArray()

int QSettings::beginReadArray ( QAnyStringView prefix)

Adds prefix to the current group and starts reading from an array.

Returns the size of the array.

Example:

struct Login {
QString userName;
QString password;
};
QList<Login> logins;
...
QSettings settings;
int size = settings.beginReadArray("logins");
for (int i = 0; i < size; ++i) {
Login login;
login.userName = settings.value("userName").toString();
login.password = settings.value("password").toString();
logins.append(login);
}

Use beginWriteArray() to write the array in the first place.

Note
In Qt versions prior to 6.4, this function took QString, not QAnyStringView.
See also
beginWriteArray(), endArray(), setArrayIndex()

Definition at line 2997 of file qsettings.cpp.

References d.

◆ beginWriteArray()

void QSettings::beginWriteArray ( QAnyStringView prefix,
int size = -1 )

Adds prefix to the current group and starts writing an array of size size.

If size is -1 (the default), it is automatically determined based on the indexes of the entries written.

If you have many occurrences of a certain set of keys, you can use arrays to make your life easier. For example, let's suppose that you want to save a variable-length list of user names and passwords. You could then write:

struct Login {
};
QList<Login> logins;
...
QSettings settings;
for (qsizetype i = 0; i < logins.size(); ++i) {
settings.setValue("userName", logins.at(i).userName);
settings.setValue("password", logins.at(i).password);
}

The generated keys will have the form

\list

  • logins/size
  • logins/1/userName
  • logins/1/password
  • logins/2/userName
  • logins/2/password
  • logins/3/userName
  • logins/3/password
  • ... \endlist

To read back an array, use beginReadArray().

Note
In Qt versions prior to 6.4, this function took QString, not QAnyStringView.
See also
beginReadArray(), endArray(), setArrayIndex()

Definition at line 3036 of file qsettings.cpp.

References d, remove(), and setValue().

+ Here is the call graph for this function:

◆ childGroups()

QStringList QSettings::childGroups ( ) const

Returns a list of all key top-level groups that contain keys that can be read using the QSettings object.

Example:

settings.setValue("fridge/color", QColor(Qt::white));
settings.setValue("fridge/size", QSize(32, 96));
settings.setValue("sofa", true);
settings.setValue("tv", false);
// groups: ["fridge"]

If a group is set using beginGroup(), the first-level keys in that group are returned, without the group prefix.

settings.beginGroup("fridge");
// groups: []

You can navigate through the entire setting hierarchy using childKeys() and childGroups() recursively.

See also
childKeys(), allKeys()

Definition at line 3159 of file qsettings.cpp.

References QSettingsPrivate::ChildGroups, and d.

◆ childKeys()

QStringList QSettings::childKeys ( ) const

Returns a list of all top-level keys that can be read using the QSettings object.

Example:

settings.setValue("fridge/color", QColor(Qt::white));
settings.setValue("fridge/size", QSize(32, 96));
settings.setValue("sofa", true);
settings.setValue("tv", false);
// keys: ["sofa", "tv"]

If a group is set using beginGroup(), the top-level keys in that group are returned, without the group prefix:

settings.beginGroup("fridge");
// keys: ["color", "size"]

You can navigate through the entire setting hierarchy using childKeys() and childGroups() recursively.

See also
childGroups(), allKeys()

Definition at line 3135 of file qsettings.cpp.

References QSettingsPrivate::ChildKeys, and d.

Referenced by QHstsStore::readPolicies().

+ Here is the caller graph for this function:

◆ clear()

void QSettings::clear ( )

Removes all entries in the primary location associated to this QSettings object.

Entries in fallback locations are not removed.

If you only want to remove the entries in the current group(), use remove("") instead.

See also
remove(), setFallbacksEnabled()

Definition at line 2757 of file qsettings.cpp.

References d.

◆ contains()

bool QSettings::contains ( QAnyStringView key) const

Returns true if there exists a setting called key; returns false otherwise.

If a group is set using beginGroup(), key is taken to be relative to that group.

Note that the Windows registry and INI files use case-insensitive keys, whereas the CFPreferences API on \macos and iOS uses case-sensitive keys. To avoid portability problems, see the \l{Section and Key Syntax} rules.

Note
In Qt versions prior to 6.4, this function took QString, not QAnyStringView.
See also
value(), setValue()

Definition at line 3277 of file qsettings.cpp.

References d.

Referenced by QQmlPreviewPosition::initLastSavedWindowPosition(), QLocalServerPrivate::listen(), QQmlPreviewPosition::loadWindowPositionSettings(), and QLocalServerPrivate::removeServer().

+ Here is the caller graph for this function:

◆ defaultFormat()

QSettings::Format QSettings::defaultFormat ( )
static
Since
4.4

Returns default file format used for storing settings for the QSettings(QObject *) constructor. If no default format is set, QSettings::NativeFormat is used.

See also
format()

Definition at line 3398 of file qsettings.cpp.

References globalDefaultFormat.

◆ endArray()

void QSettings::endArray ( )

Closes the array that was started using beginReadArray() or beginWriteArray().

See also
beginReadArray(), beginWriteArray()

Definition at line 3053 of file qsettings.cpp.

References d, qWarning, setValue(), QString::size(), and QSettingsGroup::toString().

+ Here is the call graph for this function:

◆ endGroup()

void QSettings::endGroup ( )

Resets the group to what it was before the corresponding beginGroup() call.

Example:

// settings.group() == "alpha"
// settings.group() == "alpha/beta"
// settings.group() == "alpha"
// settings.group() == ""
See also
beginGroup(), group()

Definition at line 2954 of file qsettings.cpp.

References d, qWarning, QString::size(), and QSettingsGroup::toString().

Referenced by snippet_ctor2().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ event()

bool QSettings::event ( QEvent * event)
overrideprotectedvirtual

\reimp

Reimplemented from QObject.

Definition at line 3313 of file qsettings.cpp.

References d, QObject::event(), and QEvent::UpdateRequest.

+ Here is the call graph for this function:

◆ fallbacksEnabled()

bool QSettings::fallbacksEnabled ( ) const

Returns true if fallbacks are enabled; returns false otherwise.

By default, fallbacks are enabled.

See also
setFallbacksEnabled()

Definition at line 3303 of file qsettings.cpp.

References d.

◆ fileName()

QString QSettings::fileName ( ) const

Returns the path where settings written using this QSettings object are stored.

On Windows, if the format is QSettings::NativeFormat, the return value is a system registry path, not a file path.

See also
isWritable(), format()

Definition at line 2791 of file qsettings.cpp.

References d.

Referenced by QQmlSettingsPrivate::init().

+ Here is the caller graph for this function:

◆ format()

QSettings::Format QSettings::format ( ) const
Since
4.4

Returns the format used for storing the settings.

See also
defaultFormat(), fileName(), scope(), organizationName(), applicationName()

Definition at line 2804 of file qsettings.cpp.

References d.

Referenced by setDefaultFormat(), and QFFmpegImageCapture::setImageSettings().

+ Here is the caller graph for this function:

◆ group()

QString QSettings::group ( ) const

Returns the current group.

See also
beginGroup(), endGroup()

Definition at line 2976 of file qsettings.cpp.

References d.

Referenced by remove().

+ Here is the caller graph for this function:

◆ isAtomicSyncRequired()

bool QSettings::isAtomicSyncRequired ( ) const
Since
5.10

Returns true if QSettings is only allowed to perform atomic saving and reloading (synchronization) of the settings. Returns false if it is allowed to save the settings contents directly to the configuration file.

The default is true.

See also
setAtomicSyncRequired(), QSaveFile

Definition at line 2876 of file qsettings.cpp.

References d.

◆ isWritable()

bool QSettings::isWritable ( ) const

Returns true if settings can be written using this QSettings object; returns false otherwise.

One reason why isWritable() might return false is if QSettings operates on a read-only file.

Warning
This function is not perfectly reliable, because the file permissions can change at any time.
See also
fileName(), status(), sync()

Definition at line 3177 of file qsettings.cpp.

References d.

Referenced by QHstsStore::isWritable().

+ Here is the caller graph for this function:

◆ organizationName()

QString QSettings::organizationName ( ) const
Since
4.4

Returns the organization name used for storing the settings.

See also
QCoreApplication::organizationName(), format(), scope(), applicationName()

Definition at line 2830 of file qsettings.cpp.

References d.

◆ registerFormat()

QSettings::Format QSettings::registerFormat ( const QString & extension,
ReadFunc readFunc,
WriteFunc writeFunc,
Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive )
static
Since
4.1 \threadsafe

Registers a custom storage format. On success, returns a special Format value that can then be passed to the QSettings constructor. On failure, returns InvalidFormat.

The extension is the file extension associated to the format (without the '.').

The readFunc and writeFunc parameters are pointers to functions that read and write a set of key/value pairs. The QIODevice parameter to the read and write functions is always opened in binary mode (i.e., without the QIODevice::Text flag).

The caseSensitivity parameter specifies whether keys are case sensitive or not. This makes a difference when looking up values using QSettings. The default is case sensitive.

By default, if you use one of the constructors that work in terms of an organization name and an application name, the file system locations used are the same as for IniFormat. Use setPath() to specify other locations.

Example:

int main(int argc, char *argv[])
{
const QSettings::Format XmlFormat =
QSettings settings(XmlFormat, QSettings::UserScope, "MySoft",
"Star Runner");
...
}
void endGroup()
Resets the group to what it was before the corresponding beginGroup() call.
void beginWriteArray(QAnyStringView prefix, int size=-1)
Adds prefix to the current group and starts writing an array of size size.
void endArray()
Closes the array that was started using beginReadArray() or beginWriteArray().
Format
This enum type specifies the storage format used by QSettings.
Definition qsettings.h:48
@ NativeFormat
Definition qsettings.h:49
void remove(QAnyStringView key)
Removes the setting key and any sub-settings of key.
int beginReadArray(QAnyStringView prefix)
Adds prefix to the current group and starts reading from an array.
QStringList childKeys() const
Returns a list of all top-level keys that can be read using the QSettings object.
static Format registerFormat(const QString &extension, ReadFunc readFunc, WriteFunc writeFunc, Qt::CaseSensitivity caseSensitivity=Qt::CaseSensitive)
QStringList allKeys() const
Returns a list of all keys, including subkeys, that can be read using the QSettings object.
QStringList childGroups() const
Returns a list of all key top-level groups that contain keys that can be read using the QSettings obj...
void beginGroup(QAnyStringView prefix)
Appends prefix to the current group.
void setArrayIndex(int i)
Sets the current array index to i.
double toDouble(bool *ok=nullptr) const
Returns the variant as a double if the variant has userType() \l QMetaType::Double,...
GLsizei GLuint * groups
GLfloat GLfloat GLfloat alpha
Definition qopenglext.h:418
QWidget * panel
Definition settings.cpp:7
bool myReadFunc(QIODevice &device, QSettings::SettingsMap &map)
[26]
bool readXmlFile(QIODevice &device, QSettings::SettingsMap &map)
[28]
settings beginGroup("mainwindow")
[12]
QList< Login > logins
settings endGroup()
bool myWriteFunc(QIODevice &device, const QSettings::SettingsMap &map)
[27]
settings setValue("DataPump/bgcolor", color)
bool writeXmlFile(QIODevice &device, const QSettings::SettingsMap &map)
edit isVisible()
Definition parser.h:19
See also
setPath()

Definition at line 3512 of file qsettings.cpp.

References Qt::CaseSensitive, CustomFormat1, extension(), info, InvalidFormat, Q_ASSERT, and settingsGlobalMutex.

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ remove()

void QSettings::remove ( QAnyStringView key)

Removes the setting key and any sub-settings of key.

Example:

settings.setValue("monkey", 1);
settings.setValue("monkey/sea", 2);
settings.setValue("monkey/doe", 4);
settings.remove("monkey");
// keys: ["ape"]

Be aware that if one of the fallback locations contains a setting with the same key, that setting will be visible after calling remove().

If key is an empty string, all keys in the current group() are removed. For example:

settings.setValue("monkey", 1);
settings.setValue("monkey/sea", 2);
settings.setValue("monkey/doe", 4);
settings.beginGroup("monkey");
// keys: ["ape"]

Note that the Windows registry and INI files use case-insensitive keys, whereas the CFPreferences API on \macos and iOS uses case-sensitive keys. To avoid portability problems, see the \l{Section and Key Syntax} rules.

Note
In Qt versions prior to 6.4, this function took QString, not QAnyStringView.
See also
setValue(), value(), contains()

Definition at line 3239 of file qsettings.cpp.

References d, and group().

Referenced by beginWriteArray(), QLocalServerPrivate::closeServer(), and QLocalServerPrivate::removeServer().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ scope()

QSettings::Scope QSettings::scope ( ) const
Since
4.4

Returns the scope used for storing the settings.

See also
format(), organizationName(), applicationName()

Definition at line 2817 of file qsettings.cpp.

References d.

Referenced by setPath().

+ Here is the caller graph for this function:

◆ setArrayIndex()

void QSettings::setArrayIndex ( int i)

Sets the current array index to i.

Calls to functions such as setValue(), value(), remove(), and contains() will operate on the array entry at that index.

You must call beginReadArray() or beginWriteArray() before you can call this function.

Definition at line 3082 of file qsettings.cpp.

References d, i, qMax(), qWarning, QString::size(), and QSettingsGroup::toString().

Referenced by beginWriteArray(), and for().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setAtomicSyncRequired()

void QSettings::setAtomicSyncRequired ( bool enable)
Since
5.10

Configures whether QSettings is required to perform atomic saving and reloading (synchronization) of the settings. If the enable argument is true (the default), sync() will only perform synchronization operations that are atomic. If this is not possible, sync() will fail and status() will be an error condition.

Setting this property to false will allow QSettings to write directly to the configuration file and ignore any errors trying to lock it against other processes trying to write at the same time. Because of the potential for corruption, this option should be used with care, but is required in certain conditions, like a QSettings::IniFormat configuration file that exists in an otherwise non-writeable directory or NTFS Alternate Data Streams.

See \l QSaveFile for more information on the feature.

See also
isAtomicSyncRequired(), QSaveFile

Definition at line 2903 of file qsettings.cpp.

References d.

◆ setDefaultFormat()

void QSettings::setDefaultFormat ( Format format)
static
Since
4.4

Sets the default file format to the given format, which is used for storing settings for the QSettings(QObject *) constructor.

If no default format is set, QSettings::NativeFormat is used. See the documentation for the QSettings constructor you are using to see if that constructor will ignore this function.

See also
format()

Definition at line 3385 of file qsettings.cpp.

References format(), and globalDefaultFormat.

+ Here is the call graph for this function:

◆ setFallbacksEnabled()

void QSettings::setFallbacksEnabled ( bool b)

Sets whether fallbacks are enabled to b.

By default, fallbacks are enabled.

See also
fallbacksEnabled()

Definition at line 3290 of file qsettings.cpp.

References d.

Referenced by QHstsStore::QHstsStore().

+ Here is the caller graph for this function:

◆ setPath()

void QSettings::setPath ( Format format,
Scope scope,
const QString & path )
static
Since
4.1

Sets the path used for storing settings for the given format and scope, to path. The format can be a custom format.

The table below summarizes the default values:

\table \header

  • Platform
  • Format
  • Scope
  • Path \row
  • {1,2} Windows
  • {1,2} IniFormat
  • UserScope
  • FOLDERID_RoamingAppData \row
  • SystemScope
  • FOLDERID_ProgramData \row
  • {1,2} Unix
  • {1,2} NativeFormat, IniFormat
  • UserScope
  • $HOME/.config \row
  • SystemScope
  • /etc/xdg \row
  • {1,2} \macos and iOS
  • {1,2} IniFormat
  • UserScope
  • $HOME/.config \row
  • SystemScope
  • /etc/xdg \endtable

The default UserScope paths on Unix, \macos, and iOS ($HOME/.config or $HOME/Settings) can be overridden by the user by setting the XDG_CONFIG_HOME environment variable. The default SystemScope paths on Unix, \macos, and iOS (/etc/xdg) can be overridden when building the Qt library using the configure script's -sysconfdir flag (see QLibraryInfo for details).

Setting the NativeFormat paths on Windows, \macos, and iOS has no effect.

Warning
This function doesn't affect existing QSettings objects.
See also
registerFormat()

Definition at line 3435 of file qsettings.cpp.

References initDefaultPaths(), pathHashKey(), scope(), QDir::separator(), and settingsGlobalMutex.

+ Here is the call graph for this function:

◆ setValue()

void QSettings::setValue ( QAnyStringView key,
const QVariant & value )

Sets the value of setting key to value.

If the key already exists, the previous value is overwritten.

Note that the Windows registry and INI files use case-insensitive keys, whereas the CFPreferences API on \macos and iOS uses case-sensitive keys. To avoid portability problems, see the \l{Section and Key Syntax} rules.

Example:

settings.setValue("interval", 30);
settings.value("interval").toInt(); // returns 30
settings.setValue("interval", 6.55);
settings.value("interval").toDouble(); // returns 6.55
Note
In Qt versions prior to 6.4, this function took QString, not QAnyStringView.
See also
value(), remove(), contains()

Definition at line 3202 of file qsettings.cpp.

References d, and qWarning.

Referenced by beginWriteArray(), beginWriteArray(), QLocalServerPrivate::closeServer(), endArray(), QLocalServerPrivate::listen(), main(), snippet_ctor2(), QQmlSettingsPrivate::store(), QQmlToolingSettings::writeDefaults(), MainWindow::writeSettings(), and QColorDialogStaticData::writeSettings().

+ Here is the caller graph for this function:

◆ status()

QSettings::Status QSettings::status ( ) const

Returns a status code indicating the first error that was met by QSettings, or QSettings::NoError if no error occurred.

Be aware that QSettings delays performing some operations. For this reason, you might want to call sync() to ensure that the data stored in QSettings is written to disk before calling status().

See also
sync()

Definition at line 2859 of file qsettings.cpp.

References d.

Referenced by QQmlSettingsPrivate::instance(), and QQmlToolingSettings::writeDefaults().

+ Here is the caller graph for this function:

◆ sync()

void QSettings::sync ( )

Writes any unsaved changes to permanent storage, and reloads any settings that have been changed in the meantime by another application.

This function is called automatically from QSettings's destructor and by the event loop at regular intervals, so you normally don't need to call it yourself.

See also
status()

Definition at line 2775 of file qsettings.cpp.

References d.

Referenced by QHstsStore::synchronize(), and QQmlToolingSettings::writeDefaults().

+ Here is the caller graph for this function:

◆ value() [1/2]

QVariant QSettings::value ( QAnyStringView key) const

Definition at line 3348 of file qsettings.cpp.

References d.

◆ value() [2/2]

QVariant QSettings::value ( QAnyStringView key,
const QVariant & defaultValue ) const

Returns the value for setting key.

If the setting doesn't exist, returns defaultValue.

If no default value is specified, a default QVariant is returned.

Note that the Windows registry and INI files use case-insensitive keys, whereas the CFPreferences API on \macos and iOS uses case-sensitive keys. To avoid portability problems, see the \l{Section and Key Syntax} rules.

Example:

settings.setValue("animal/snake", 58);
settings.value("animal/snake", 1024).toInt(); // returns 58
settings.value("animal/zebra", 1024).toInt(); // returns 1024
settings.value("animal/zebra").toInt(); // returns 0
Note
In Qt versions prior to 6.4, this function took QString, not QAnyStringView.
See also
setValue(), contains(), remove()

Definition at line 3354 of file qsettings.cpp.

References d.

Referenced by QLocalSocket::connectToServer(), QFileDialogPrivate::createWidgets(), QWindowsDirect2DPaintEnginePrivate::fontFaceFromFontEngine(), for(), QFileDialogPrivate::init(), QQmlPreviewPosition::initLastSavedWindowPosition(), QQmlSettingsPrivate::load(), QQmlPreviewPosition::loadWindowPositionSettings(), main(), QLibraryInfo::platformPluginArguments(), readInputFile(), MainWindow::readSettings(), QColorDialogStaticData::readSettings(), QQuickStyleSpec::resolve(), resolveSetting(), resolveSetting(), resolveSetting(), snippet_ctor2(), and QWindowsScreen::subpixelAntialiasingTypeHint().

+ Here is the caller graph for this function:

The documentation for this class was generated from the following files: