QtBase
v6.3.1
|
The QSettings class provides persistent platform-independent application settings. More...
#include <qsettings.h>
Public Types | |
enum | Status { NoError = 0 , AccessError , FormatError } |
enum | Format { NativeFormat , IniFormat , InvalidFormat = 16 , CustomFormat1 , CustomFormat2 , CustomFormat3 , CustomFormat4 , CustomFormat5 , CustomFormat6 , CustomFormat7 , CustomFormat8 , CustomFormat9 , CustomFormat10 , CustomFormat11 , CustomFormat12 , CustomFormat13 , CustomFormat14 , CustomFormat15 , CustomFormat16 } |
enum | Scope { UserScope , SystemScope } |
typedef QMap< QString, QVariant > | SettingsMap |
typedef bool(* | ReadFunc) (QIODevice &device, SettingsMap &map) |
typedef bool(* | WriteFunc) (QIODevice &device, const SettingsMap &map) |
Protected Member Functions | |
bool | event (QEvent *event) override |
Protected Member Functions inherited from QObject | |
QObject * | sender () const |
int | senderSignalIndex () const |
int | receivers (const char *signal) const |
bool | isSignalConnected (const QMetaMethod &signal) const |
virtual void | timerEvent (QTimerEvent *event) |
virtual void | childEvent (QChildEvent *event) |
virtual void | customEvent (QEvent *event) |
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 () |
Signals inherited from QObject | |
void | destroyed (QObject *=nullptr) |
void | objectNameChanged (const QString &objectName, QPrivateSignal) |
Protected Attributes inherited from QObject | |
QScopedPointer< QObjectData > | d_ptr |
Properties inherited from QObject | |
QString | objectName |
the name of this object More... | |
Related Functions inherited from QObject | |
template< class T > T | qobject_cast (const QObject *object) |
template< typename T > T | qFindChildqFindChildren (const QObject *obj, const QString &name)() |
template< typename T > QList< T > | qFindChildrenqFindChildren (const QObject *obj, const QString &name)() |
QObjectList | |
The QSettings class provides persistent platform-independent application settings.
\inmodule QtCore
\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 62 of file qsettings.h.
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.
Definition at line 186 of file qsettings.h.
Typedef for QMap<QString, QVariant>.
Definition at line 185 of file qsettings.h.
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.
Definition at line 187 of file qsettings.h.
enum QSettings::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 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
{ Definition at line 84 of file qsettings.h.
enum QSettings::Scope |
) in file paths:
QSettings always treats backslash as a special character and provides no API for reading or writing such entries.
%
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.\endlist
Enumerator | |
---|---|
UserScope | |
SystemScope |
Definition at line 115 of file qsettings.h.
enum QSettings::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).
Enumerator | |
---|---|
NoError | |
AccessError | |
FormatError |
Definition at line 75 of file qsettings.h.
|
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).
Definition at line 2482 of file qsettings.cpp.
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}.
Definition at line 2507 of file qsettings.cpp.
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 2531 of file qsettings.cpp.
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.
.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
@
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. \endlistDefinition at line 2567 of file qsettings.cpp.
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
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.
Definition at line 2607 of file qsettings.cpp.
Constructs a QSettings object in the same way as QSettings(QObject *parent) but with the given scope.
Definition at line 2620 of file qsettings.cpp.
QSettings::~QSettings | ( | ) |
Destroys the QSettings object.
Any unsaved changes will eventually be written to permanent storage.
Definition at line 2688 of file qsettings.cpp.
QStringList QSettings::allKeys | ( | ) | const |
Returns a list of all keys, including subkeys, that can be read using the QSettings object.
Example:
If a group is set using beginGroup(), only the keys in the group are returned, without the group prefix:
Definition at line 3057 of file qsettings.cpp.
QString QSettings::applicationName | ( | ) | const |
Returns the application name used for storing the settings.
Definition at line 2798 of file qsettings.cpp.
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:
This will set the value of three settings:
\list
mainwindow/size
mainwindow/fullScreen
outputpanel/visible
\endlistCall endGroup() to reset the current group to what it was before the corresponding beginGroup() call. Groups can be nested.
Definition at line 2890 of file qsettings.cpp.
Adds prefix to the current group and starts reading from an array. Returns the size of the array.
Example:
Use beginWriteArray() to write the array in the first place.
Definition at line 2946 of file qsettings.cpp.
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:
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
To read back an array, use beginReadArray().
Definition at line 2982 of file qsettings.cpp.
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:
If a group is set using beginGroup(), the first-level keys in that group are returned, without the group prefix.
You can navigate through the entire setting hierarchy using childKeys() and childGroups() recursively.
Definition at line 3105 of file qsettings.cpp.
QStringList QSettings::childKeys | ( | ) | const |
Returns a list of all top-level keys that can be read using the QSettings object.
Example:
If a group is set using beginGroup(), the top-level keys in that group are returned, without the group prefix:
You can navigate through the entire setting hierarchy using childKeys() and childGroups() recursively.
Definition at line 3081 of file qsettings.cpp.
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.
Definition at line 2712 of file qsettings.cpp.
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.
Definition at line 3214 of file qsettings.cpp.
|
static |
Returns default file format used for storing settings for the QSettings(QObject *) constructor. If no default format is set, QSettings::NativeFormat is used.
Definition at line 3332 of file qsettings.cpp.
void QSettings::endArray | ( | ) |
Closes the array that was started using beginReadArray() or beginWriteArray().
Definition at line 2999 of file qsettings.cpp.
void QSettings::endGroup | ( | ) |
Resets the group to what it was before the corresponding beginGroup() call.
Example:
Definition at line 2906 of file qsettings.cpp.
|
overrideprotectedvirtual |
\reimp
Reimplemented from QObject.
Definition at line 3250 of file qsettings.cpp.
bool QSettings::fallbacksEnabled | ( | ) | const |
Returns true
if fallbacks are enabled; returns false
otherwise.
By default, fallbacks are enabled.
Definition at line 3240 of file qsettings.cpp.
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.
Definition at line 2746 of file qsettings.cpp.
QSettings::Format QSettings::format | ( | ) | const |
Returns the format used for storing the settings.
Definition at line 2759 of file qsettings.cpp.
QString QSettings::group | ( | ) | const |
Returns the current group.
Definition at line 2928 of file qsettings.cpp.
bool QSettings::isAtomicSyncRequired | ( | ) | const |
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
.
Definition at line 2831 of file qsettings.cpp.
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.
Definition at line 3123 of file qsettings.cpp.
QString QSettings::organizationName | ( | ) | const |
Returns the organization name used for storing the settings.
Definition at line 2785 of file qsettings.cpp.
|
static |
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:
Definition at line 3448 of file qsettings.cpp.
Removes the setting key and any sub-settings of key.
Example:
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:
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.
Definition at line 3179 of file qsettings.cpp.
QSettings::Scope QSettings::scope | ( | ) | const |
Returns the scope used for storing the settings.
Definition at line 2772 of file qsettings.cpp.
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 3028 of file qsettings.cpp.
void QSettings::setAtomicSyncRequired | ( | bool | enable | ) |
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.
Definition at line 2858 of file qsettings.cpp.
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.
Definition at line 3319 of file qsettings.cpp.
void QSettings::setFallbacksEnabled | ( | bool | b | ) |
Sets whether fallbacks are enabled to b.
By default, fallbacks are enabled.
Definition at line 3227 of file qsettings.cpp.
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
FOLDERID_RoamingAppData
\row FOLDERID_ProgramData
\row $HOME/
.config \row /etc/xdg
\row $HOME/Settings
\row /etc/xdg
\row $HOME/
.config \row /etc/xdg
\endtableThe 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.
Definition at line 3371 of file qsettings.cpp.
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:
Definition at line 3145 of file qsettings.cpp.
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().
Definition at line 2814 of file qsettings.cpp.
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.
Definition at line 2730 of file qsettings.cpp.
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:
Definition at line 3288 of file qsettings.cpp.