QSettings Class▲
-
Header: QSettings
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
-
qmake: QT += core
-
Inherits: https://
-
Group: QSettings is part of https://
Detailed Description▲
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.
https:// is an abstraction around these technologies, enabling you to save and restore application settings in a portable manner. It also supports https://.
https://'s API is based on https://, allowing you to save most value-based types, such as https://, https://, and https://, with the minimum of effort.
If all you need is a non-persistent memory-based structure, consider using https://<https://, https://> instead.
Basic Usage▲
When creating a https:// object, you must pass the name of your company or organization as well as the name of your application. For example, if your product is called Star Runner and your company is called MySoft, you would construct the https:// object as follows:
QSettings settings("MySoft", "Star Runner");https:// objects can be created either on the stack or on the heap (i.e. using new). Constructing and destroying a https:// object is very fast.
If you use https:// from many places in your application, you might want to specify the organization name and the application name using https://() and https://(), and then use the default https:// constructor:
QCoreApplication::setOrganizationName("MySoft");
QCoreApplication::setOrganizationDomain("mysoft.com");
QCoreApplication::setApplicationName("Star Runner");
...
QSettings settings;(Here, we also specify the organization's Internet domain. When the Internet domain is set, it is used on macOS and iOS instead of the organization name, since macOS and iOS applications conventionally use Internet domains to identify themselves. If no domain is set, a fake domain is derived from the organization name. See the https:// below for details.)
https:// stores settings. Each setting consists of a https:// that specifies the setting's name (the key) and a https:// that stores the data associated with the key. To write a setting, use https://(). For example:
settings.setValue("editor/wrapMargin", 68);If there already exists a setting with the same key, the existing value is overwritten by the new value. For efficiency, the changes may not be saved to permanent storage immediately. (You can always call https://() to commit your changes.)
You can get a setting's value back using https://():
int margin = settings.value("editor/wrapMargin").toInt();If there is no setting with the specified name, https:// returns a null https:// (which can be converted to the integer 0). You can specify another default value by passing a second argument to https://():
int margin = settings.value("editor/wrapMargin", 80).toInt();To test whether a given key exists, call https://(). To remove the setting associated with a key, call https://(). To obtain the list of all keys, call https://(). To remove all keys, call https://().
QVariant and GUI Types▲
Because https:// is part of the Qt Core module, it cannot provide conversion functions to data types such as https://, https://, and https://, which are part of Qt GUI. In other words, there is no toColor(), toImage(), or toPixmap() functions in https://.
Instead, you can use the https://() template function. For example:
QSettings settings("MySoft", "Star Runner");
QColor color = settings.value("DataPump/bgcolor").value<QColor>();The inverse conversion (e.g., from https:// to https://) is automatic for all data types supported by https://, including GUI-related types:
QSettings settings("MySoft", "Star Runner");
QColor color = palette().background().color();
settings.setValue("DataPump/bgcolor", color);Custom types registered using https://() that have operators for streaming to and from a https:// can be stored using https://.
Section and Key Syntax▲
Setting keys can contain any Unicode characters. 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, follow these simple rules:
-
Always refer to the same key using the same case. For example, if you refer to a key as "text fonts" in one place in your code, don't refer to it as "Text Fonts" somewhere else.
-
Avoid key names that are identical except for the case. For example, if you have a key called "MainWindow", don't try to save another key as "mainwindow".
-
Do not use slashes ('/' and '\') in section or key names; the backslash character is used to separate sub keys (see below). On windows '\' are converted by https:// to '/', which makes them identical.
You can form hierarchical keys using the '/' character as a separator, similar to Unix file paths. For example:
settings.setValue("mainwindow/size", win->size());
settings.setValue("mainwindow/fullScreen", win->isFullScreen());
settings.setValue("outputpanel/visible", panel->isVisible());If you want to save or restore many settings with the same prefix, you can specify the prefix using https://() and call https://() at the end. Here's the same example again, but this time using the group mechanism:
settings.beginGroup("mainwindow");
settings.setValue("size", win->size());
settings.setValue("fullScreen", win->isFullScreen());
settings.endGroup();
settings.beginGroup("outputpanel");
settings.setValue("visible", panel->isVisible());
settings.endGroup();If a group is set using https://(), the behavior of most functions changes consequently. Groups can be set recursively.
In addition to groups, https:// also supports an "array" concept. See https://() and https://() for details.
Fallback Mechanism▲
Let's assume that you have created a https:// object with the organization name MySoft and the application name Star Runner. When you look up a value, up to four locations are searched in that order:
-
a user-specific location for the Star Runner application
-
a user-specific location for all applications by MySoft
-
a system-wide location for the Star Runner application
-
a system-wide location for all applications by MySoft
(See https:// below for information on what these locations are on the different platforms supported by Qt.)
If a key cannot be found in the first location, the search goes on in the second location, and so on. This enables you to store system-wide or organization-wide settings and to override them on a per-user or per-application basis. To turn off this mechanism, call https://(false).
Although keys from all four locations are available for reading, only the first file (the user-specific location for the application at hand) is accessible for writing. To write to any of the other files, omit the application name and/or specify https:// (as opposed to https://, the default).
Let's see with an example:
QSettings obj1("MySoft", "Star Runner");
QSettings obj2("MySoft");
QSettings obj3(QSettings::SystemScope, "MySoft", "Star Runner");
QSettings obj4(QSettings::SystemScope, "MySoft");The table below summarizes which https:// objects access which location. "X" means that the location is the main location associated to the https:// object and is used both for reading and for writing; "o" means that the location is used as a fallback when reading.
|
Locations |
obj1 |
obj2 |
obj3 |
obj4 |
|---|---|---|---|---|
|
1. User, Application |
X |
|||
|
2. User, Organization |
o |
X |
||
|
3. System, Application |
o |
X |
||
|
4. System, Organization |
o |
o |
o |
X |
The beauty of this mechanism is that it works on all platforms supported by Qt and that it still gives you a lot of flexibility, without requiring you to specify any file names or registry paths.
If you want to use INI files on all platforms instead of the native API, you can pass https:// as the first argument to the https:// constructor, followed by the scope, the organization name, and the application name:
QSettings settings(QSettings::IniFormat, QSettings::UserScope,
"MySoft", "Star Runner");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 https://. The numeric value can be recovered using https://(), https://() and related functions.
The https:// example lets you experiment with different settings location and with fallbacks turned on or off.
Restoring the State of a GUI Application▲
https:// is often used to store the state of a GUI application. The following example illustrates how to use https:// to save and restore the geometry of an application's main window.
void MainWindow::writeSettings()
{
QSettings settings("Moose Soft", "Clipper");
settings.beginGroup("MainWindow");
settings.setValue("geometry", saveGeometry());
settings.endGroup();
}
void MainWindow::readSettings()
{
QSettings settings("Moose Soft", "Clipper");
settings.beginGroup("MainWindow");
const auto geometry = settings.value("geometry", QByteArray()).toByteArray();
if (geometry.isEmpty())
setGeometry(200, 200, 400, 400);
else
restoreGeometry(geometry)
settings.endGroup();
}See https:// for a discussion on why it is better to call https://() and https://() rather than https://() to restore a window's geometry.
The readSettings() and writeSettings() functions must be called from the main window's constructor and close event handler as follows:
MainWindow::MainWindow()
{
...
readSettings();
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if (userReallyWantsToQuit()) {
writeSettings();
event->accept();
} else {
event->ignore();
}
}See the Application example for a self-contained example that uses https://.
Accessing Settings from Multiple Threads or Processes Simultaneously▲
https:// is https://. This means that you can use distinct https:// object in different threads simultaneously. This guarantee stands even when the https:// objects refer to the same files on disk (or to the same entries in the system registry). If a setting is modified through one https:// object, the change will immediately be visible in any other https:// objects that operate on the same location and that live in the same process.
https:// can safely be used from different processes (which can be different instances of your application running at the same time or different applications altogether) to read and write to the same system locations, provided certain conditions are met. For https://, it uses advisory file locking and a smart merging algorithm to ensure data integrity. The condition for that to work is that the writeable configuration file must be a regular file and must reside in a directory that the current user can create new, temporary files in. If that is not the case, then one must use https://() to turn the safety off.
Note that https://() imports changes made by other processes (in addition to writing the changes from this https://).
Platform-Specific Notes▲
Locations Where Application Settings Are Stored▲
As mentioned in the https:// section, https:// stores settings for an application in up to four locations, depending on whether the settings are user-specific or system-wide and whether the settings are application-specific or organization-wide. For simplicity, we're assuming the organization is called MySoft and the application is called Star Runner.
On Unix systems, if the file format is https://, the following files are used by default:
-
$HOME/.config/MySoft/Star Runner.conf
-
$HOME/.config/MySoft.conf
-
for each directory <dir> in $XDG_CONFIG_DIRS: <dir>/MySoft/Star Runner.conf
-
for each directory <dir> in $XDG_CONFIG_DIRS: <dir>/MySoft.conf
If XDG_CONFIG_DIRS is unset, the default value of /etc/xdg is used.
On macOS and iOS, if the file format is https://, these files are used by default:
-
$HOME/Library/Preferences/com.MySoft.Star Runner.plist
-
$HOME/Library/Preferences/com.MySoft.plist
-
/Library/Preferences/com.MySoft.Star Runner.plist
-
/Library/Preferences/com.MySoft.plist
On Windows, https:// settings are stored in the following registry paths:
-
HKEY_CURRENT_USER\Software\MySoft\Star Runner
-
HKEY_CURRENT_USER\Software\MySoft\OrganizationDefaults
-
HKEY_LOCAL_MACHINE\Software\MySoft\Star Runner
-
HKEY_LOCAL_MACHINE\Software\MySoft\OrganizationDefaults
On Windows, for 32-bit programs running in WOW64 mode, settings are stored in the following registry path: HKEY_LOCAL_MACHINE\Software\WOW6432node.
If the file format is https://, this is "Settings/MySoft/Star Runner.conf" in the application's home directory.
If the file format is https://, the following files are used on Unix, macOS, and iOS:
-
$HOME/.config/MySoft/Star Runner.ini
-
$HOME/.config/MySoft.ini
-
for each directory <dir> in $XDG_CONFIG_DIRS: <dir>/MySoft/Star Runner.ini
-
for each directory <dir> in $XDG_CONFIG_DIRS: <dir>/MySoft.ini
If XDG_CONFIG_DIRS is unset, the default value of /etc/xdg is used.
On Windows, the following files are used:
-
FOLDERID_RoamingAppData\MySoft\Star Runner.ini
-
FOLDERID_RoamingAppData\MySoft.ini
-
FOLDERID_ProgramData\MySoft\Star Runner.ini
-
FOLDERID_ProgramData\MySoft.ini
The identifiers prefixed by FOLDERID_ are special item ID lists to be passed to the Win32 API function SHGetKnownFolderPath() to obtain the corresponding path.
FOLDERID_RoamingAppData usually points to C:\Users\User Name\AppData\Roaming, also shown by the environment variable %APPDATA%.
FOLDERID_ProgramData usually points to C:\ProgramData.
If the file format is https://, this is "Settings/MySoft/Star Runner.ini" in the application's home directory.
The paths for the .ini and .conf files can be changed using https://(). On Unix, macOS, and iOS the user can override them by setting the XDG_CONFIG_HOME environment variable; see https://() for details.
Accessing INI and .plist Files Directly▲
Sometimes you do want to access settings stored in a specific file or registry path. On all platforms, if you want to read an INI file directly, you can use the https:// constructor that takes a file name as first argument and pass https:// as second argument. For example:
QSettings settings("/home/petra/misc/myapp.ini",
QSettings::IniFormat);You can then use the https:// object to read and write settings in the file.
On macOS and iOS, you can access property list .plist files by passing https:// as second argument. For example:
QSettings settings("/Users/petra/misc/myapp.plist",
QSettings::NativeFormat);Accessing the Windows Registry Directly▲
On Windows, https:// lets you access settings that have been written with https:// (or settings in a supported format, e.g., string data) in the system registry. This is done by constructing a https:// object with a path in the registry and https://.
For example:
QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Office",
QSettings::NativeFormat);All the registry entries that appear under the specified path can be read or written through the https:// object as usual (using forward slashes instead of backslashes). For example:
settings.setValue("11.0/Outlook/Security/DontTrustInstalledFiles", 0);Note that the backslash character is, as mentioned, used by https:// to separate subkeys. As a result, you cannot read or write windows registry entries that contain slashes or backslashes; you should use a native windows API if you need to do so.
Accessing Common Registry Settings on Windows▲
On Windows, it is possible for a key to have both a value and subkeys. Its default value is accessed by using "Default" or "." in place of a subkey:
settings.setValue("HKEY_CURRENT_USER\\MySoft\\Star Runner\\Galaxy", "Milkyway");
settings.setValue("HKEY_CURRENT_USER\\MySoft\\Star Runner\\Galaxy\\Sun", "OurStar");
settings.value("HKEY_CURRENT_USER\\MySoft\\Star Runner\\Galaxy\\Default"); // returns "Milkyway"On other platforms than Windows, "Default" and "." would be treated as regular subkeys.
Platform Limitations▲
While https:// attempts to smooth over the differences between the different supported platforms, there are still a few differences that you should be aware of when porting your application:
-
The Windows system registry has the following limitations: A subkey may not exceed 255 characters, an entry's value may not exceed 16,383 characters, and all the values of a key may not exceed 65,535 characters. One way to work around these limitations is to store the settings using the https:// instead of the https://.
-
On Windows, when the Windows system registry is used, https:// does not preserve the original type of the value. Therefore, the type of the value might change when a new value is set. For example, a value with type REG_EXPAND_SZ will change to REG_SZ.
-
On macOS and iOS, https://() will return some extra keys for global settings that apply to all applications. These keys can be read using https://() but cannot be changed, only shadowed. Calling https://(false) will hide these global settings.
-
On macOS and iOS, the CFPreferences API used by https:// expects Internet domain names rather than organization names. To provide a uniform API, https:// derives a fake domain name from the organization name (unless the organization name already is a domain name, e.g. OpenOffice.org). The algorithm appends ".com" to the company name and replaces spaces and other illegal characters with hyphens. If you want to specify a different domain name, call https://(), https://(), and https://() in your main() function and then use the default https:// constructor. Another solution is to use preprocessor directives, for example:
Sélectionnez#ifdef Q_OS_MACQSettings settings("grenoullelogique.fr","Squash");#elseQSettings settings("Grenoulle Logique","Squash");#endif -
On macOS, permissions to access settings not belonging to the current user (i.e. https://) have changed with 10.7 (Lion). Prior to that version, users having admin rights could access these. For 10.7 and 10.8 (Mountain Lion), only root can. However, 10.9 (Mavericks) changes that rule again but only for the native format (plist files).
See Also▲
See also https://, https://, https://, Qt Widgets - Application Example
Member Type Documentation▲
enum QSettings::Format▲
This enum type specifies the storage format used by https://.
|
Constant |
Value |
Description |
|---|---|---|
|
https://::NativeFormat |
0 |
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. |
|
https://::Registry32Format |
2 |
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. |
|
https://::Registry64Format |
3 |
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. |
|
https://::IniFormat |
1 |
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 https://. |
|
https://::InvalidFormat |
16 |
Special value returned by https://(). |
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:
-
If you store types that https:// can't convert to https:// (e.g., https://, https://, and https://), Qt uses an @-based syntax to encode the type. For example:
Sélectionnezpos=@Point(100100)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:
Sélectionnezwindir=C:\Windowshttps:// 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, https:// 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.
Compatibility with older Qt versions▲
Please note that this behavior is different to how https:// behaved in versions of Qt prior to Qt 6. INI files written with Qt 5 or earlier are however fully readable by a Qt 6 based application (unless a ini codec different from utf8 had been set). But INI files written with Qt 6 will only be readable by older Qt versions if you set the "iniCodec" to a utf-8 textcodec.
Notes▲
See Also▲
See also https://(), https://()
QSettings::ReadFunc▲
Typedef for a pointer to a function with the following signature:
bool myReadFunc(QIODevice &device, QSettings::SettingsMap &map);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▲
See also https://, https://()
enum QSettings::Scope▲
This enum specifies whether settings are user-specific or shared by all users of the same system.
|
Constant |
Value |
Description |
|---|---|---|
|
https://::UserScope |
0 |
Store settings in a location specific to the current user (e.g., in the user's home directory). |
|
https://::SystemScope |
1 |
Store settings in a global location, so that all users on the same machine access the same set of settings. |
See Also▲
See also https://()
QSettings::SettingsMap▲
enum QSettings::Status▲
The following status values are possible:
|
Constant |
Value |
Description |
|---|---|---|
|
https://::NoError |
0 |
No error occurred. |
|
https://::AccessError |
1 |
An access error occurred (e.g. trying to write to a read-only file). |
|
https://::FormatError |
2 |
A format error occurred (e.g. loading a malformed INI file). |
See Also▲
See also https://()
QSettings::WriteFunc▲
Typedef for a pointer to a function with the following signature:
bool myWriteFunc(QIODevice &device, const QSettings::SettingsMap &map);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▲
See also https://, https://()
Member Function Documentation▲
QVariant QSettings::value(QAnyStringView key) const▲
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 https:// 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 https:// rules.
Example:
QSettings settings;
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 0In Qt versions prior to 6.4, this function took https://, not https://.
See Also▲
See also https://(), https://(), https://()
[explicit] QSettings::QSettings(const QString &organization, const QString &application = QString(), QObject *parent = nullptr)▲
Constructs a https:// object for accessing settings of the application called application from the organization called organization, and with parent parent.
Example:
QSettings settings("Moose Tech", "Facturo-Pro");The scope is set to https://, and the format is set to https:// (i.e. calling https://() before calling this constructor has no effect).
See Also▲
See also https://(), https://
QSettings::QSettings(QSettings::Scope scope, const QString &organization, const QString &application = QString(), QObject *parent = nullptr)▲
Constructs a https:// object for accessing settings of the application called application from the organization called organization, and with parent parent.
If scope is https://, the https:// object searches user-specific settings first, before it searches system-wide settings as a fallback. If scope is https://, the https:// object ignores user-specific settings and provides access to system-wide settings.
The storage format is set to https:// (i.e. calling https://() before calling this constructor has no effect).
If no application name is given, the https:// object will only access the organization-wide https://.
See Also▲
See also https://()
QSettings::QSettings(QSettings::Format format, QSettings::Scope scope, const QString &organization, const QString &application = QString(), QObject *parent = nullptr)▲
Constructs a https:// object for accessing settings of the application called application from the organization called organization, and with parent parent.
If scope is https://, the https:// object searches user-specific settings first, before it searches system-wide settings as a fallback. If scope is https://, the https:// object ignores user-specific settings and provides access to system-wide settings.
If format is https://, the native API is used for storing settings. If format is https://, the INI format is used.
If no application name is given, the https:// object will only access the organization-wide https://.
QSettings::QSettings(const QString &fileName, QSettings::Format format, QObject *parent = nullptr)▲
Constructs a https:// 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 https://, 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 https://, fileName is the name of an INI file.
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:
-
https:// 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, https:// 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.
See Also▲
See also https://()
[explicit] QSettings::QSettings(QObject *parent = nullptr)▲
Constructs a https:// object for accessing settings of the application and organization set previously with a call to https://(), https://(), and https://().
The scope is https:// and the format is https://() (https:// by default). Use https://() before calling this constructor to change the default format used by this constructor.
The code
QSettings settings("Moose Soft", "Facturo-Pro");is equivalent to
QCoreApplication::setOrganizationName("Moose Soft");
QCoreApplication::setApplicationName("Facturo-Pro");
QSettings settings;If https://() and https://() has not been previously called, the https:// object will not be able to read or write any settings, and https://() will return https://.
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▲
See also https://(), https://(), https://(), https://()
[explicit] QSettings::QSettings(QSettings::Scope scope, QObject *parent = nullptr)▲
Constructs a https:// object in the same way as https://(https:// *parent) but with the given scope.
See Also▲
See also https://(QObject *parent)
[virtual] QSettings::~QSettings()▲
Destroys the https:// object.
Any unsaved changes will eventually be written to permanent storage.
See Also▲
See also https://()
QStringList QSettings::allKeys() const▲
Returns a list of all keys, including subkeys, that can be read using the https:// object.
Example:
QSettings settings;
settings.setValue("fridge/color", QColor(Qt::white));
settings.setValue("fridge/size", QSize(32, 96));
settings.setValue("sofa", true);
settings.setValue("tv", false);
QStringList keys = settings.allKeys();
// keys: ["fridge/color", "fridge/size", "sofa", "tv"]If a group is set using https://(), only the keys in the group are returned, without the group prefix:
settings.beginGroup("fridge");
keys = settings.allKeys();
// keys: ["color", "size"]See Also▲
See also https://(), https://()
QString QSettings::applicationName() const▲
Returns the application name used for storing the settings.
See Also▲
See also https://(), https://(), https://(), https://()
void QSettings::beginGroup(QAnyStringView prefix)▲
Appends prefix to the current group.
The current group is automatically prepended to all keys specified to https://. In addition, query functions such as https://(), https://(), and https://() 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("size", win->size());
settings.setValue("fullScreen", win->isFullScreen());
settings.endGroup();
settings.beginGroup("outputpanel");
settings.setValue("visible", panel->isVisible());
settings.endGroup();This will set the value of three settings:
-
mainwindow/size
-
mainwindow/fullScreen
-
outputpanel/visible
Call https://() to reset the current group to what it was before the corresponding beginGroup() call. Groups can be nested.
In Qt versions prior to 6.4, this function took https://, not https://.
See Also▲
See also https://(), https://()
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) {
settings.setArrayIndex(i);
Login login;
login.userName = settings.value("userName").toString();
login.password = settings.value("password").toString();
logins.append(login);
}
settings.endArray();Use https://() to write the array in the first place.
In Qt versions prior to 6.4, this function took https://, not https://.
See Also▲
See also https://(), https://(), https://()
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 {
QString userName;
QString password;
};
QList<Login> logins;
...
QSettings settings;
settings.beginWriteArray("logins");
for (qsizetype i = 0; i < logins.size(); ++i) {
settings.setArrayIndex(i);
settings.setValue("userName", logins.at(i).userName);
settings.setValue("password", logins.at(i).password);
}
settings.endArray();The generated keys will have the form
-
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 https://().
In Qt versions prior to 6.4, this function took https://, not https://.
See Also▲
See also https://(), https://(), https://()
QStringList QSettings::childGroups() const▲
Returns a list of all key top-level groups that contain keys that can be read using the https:// object.
Example:
QSettings settings;
settings.setValue("fridge/color", QColor(Qt::white));
settings.setValue("fridge/size", QSize(32, 96));
settings.setValue("sofa", true);
settings.setValue("tv", false);
QStringList groups = settings.childGroups();
// groups: ["fridge"]If a group is set using https://(), the first-level keys in that group are returned, without the group prefix.
settings.beginGroup("fridge");
groups = settings.childGroups();
// groups: []You can navigate through the entire setting hierarchy using https://() and childGroups() recursively.
See Also▲
See also https://(), https://()
QStringList QSettings::childKeys() const▲
Returns a list of all top-level keys that can be read using the https:// object.
Example:
QSettings settings;
settings.setValue("fridge/color", QColor(Qt::white));
settings.setValue("fridge/size", QSize(32, 96));
settings.setValue("sofa", true);
settings.setValue("tv", false);
QStringList keys = settings.childKeys();
// keys: ["sofa", "tv"]If a group is set using https://(), the top-level keys in that group are returned, without the group prefix:
settings.beginGroup("fridge");
keys = settings.childKeys();
// keys: ["color", "size"]You can navigate through the entire setting hierarchy using childKeys() and https://() recursively.
See Also▲
See also https://(), https://()
void QSettings::clear()▲
Removes all entries in the primary location associated to this https:// object.
Entries in fallback locations are not removed.
If you only want to remove the entries in the current https://(), use remove("") instead.
See Also▲
See also https://(), https://()
bool QSettings::contains(QAnyStringView key) const▲
Returns true if there exists a setting called key; returns false otherwise.
If a group is set using https://(), 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 https:// rules.
In Qt versions prior to 6.4, this function took https://, not https://.
See Also▲
See also https://(), https://()
[static] QSettings::Format QSettings::defaultFormat()▲
Returns default file format used for storing settings for the https://(https:// *) constructor. If no default format is set, https:// is used.
See Also▲
See also https://(), https://()
void QSettings::endArray()▲
Closes the array that was started using https://() or https://().
See Also▲
See also https://(), https://()
void QSettings::endGroup()▲
Resets the group to what it was before the corresponding https://() call.
Example:
settings.beginGroup("alpha");
// settings.group() == "alpha"
settings.beginGroup("beta");
// settings.group() == "alpha/beta"
settings.endGroup();
// settings.group() == "alpha"
settings.endGroup();
// settings.group() == ""See Also▲
See also https://(), https://()
[override virtual protected] bool QSettings::event(QEvent *event)▲
Reimplements: https://.
bool QSettings::fallbacksEnabled() const▲
Returns true if fallbacks are enabled; returns false otherwise.
By default, fallbacks are enabled.
See Also▲
See also https://()
QString QSettings::fileName() const▲
Returns the path where settings written using this https:// object are stored.
On Windows, if the format is https://, the return value is a system registry path, not a file path.
See Also▲
See also https://(), https://()
QSettings::Format QSettings::format() const▲
Returns the format used for storing the settings.
See Also▲
See also https://(), https://(), https://(), https://(), https://()
QString QSettings::group() const▲
bool QSettings::isAtomicSyncRequired() const▲
Returns true if https:// 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▲
See also https://(), https://
bool QSettings::isWritable() const▲
Returns true if settings can be written using this https:// object; returns false otherwise.
One reason why isWritable() might return false is if https:// operates on a read-only file.
This function is not perfectly reliable, because the file permissions can change at any time.
See Also▲
See also https://(), https://(), https://()
QString QSettings::organizationName() const▲
Returns the organization name used for storing the settings.
See Also▲
See also https://(), https://(), https://(), https://()
[static] QSettings::Format QSettings::registerFormat(const QString &extension, QSettings::ReadFunc readFunc, QSettings::WriteFunc writeFunc, Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive)▲
Registers a custom storage format. On success, returns a special Format value that can then be passed to the https:// constructor. On failure, returns https://.
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 https:// 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 https://. 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 https://. Use https://() to specify other locations.
Example:
bool readXmlFile(QIODevice &device, QSettings::SettingsMap &map);
bool writeXmlFile(QIODevice &device, const QSettings::SettingsMap &map);
int main(int argc, char *argv[])
{
const QSettings::Format XmlFormat =
QSettings::registerFormat("xml", readXmlFile, writeXmlFile);
QSettings settings(XmlFormat, QSettings::UserScope, "MySoft",
"Star Runner");
...
}This function is https://.
See Also▲
See also https://()
void QSettings::remove(QAnyStringView key)▲
Removes the setting key and any sub-settings of key.
Example:
QSettings settings;
settings.setValue("ape");
settings.setValue("monkey", 1);
settings.setValue("monkey/sea", 2);
settings.setValue("monkey/doe", 4);
settings.remove("monkey");
QStringList keys = settings.allKeys();
// 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 https://() are removed. For example:
QSettings settings;
settings.setValue("ape");
settings.setValue("monkey", 1);
settings.setValue("monkey/sea", 2);
settings.setValue("monkey/doe", 4);
settings.beginGroup("monkey");
settings.remove("");
settings.endGroup();
QStringList keys = settings.allKeys();
// 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 https:// rules.
In Qt versions prior to 6.4, this function took https://, not https://.
See Also▲
See also https://(), https://(), https://()
QSettings::Scope QSettings::scope() const▲
Returns the scope used for storing the settings.
See Also▲
See also https://(), https://(), https://()
void QSettings::setArrayIndex(int i)▲
Sets the current array index to i. Calls to functions such as https://(), https://(), https://(), and https://() will operate on the array entry at that index.
You must call https://() or https://() before you can call this function.
void QSettings::setAtomicSyncRequired(bool enable)▲
Configures whether https:// is required to perform atomic saving and reloading (synchronization) of the settings. If the enable argument is true (the default), https://() will only perform synchronization operations that are atomic. If this is not possible, https://() will fail and https://() will be an error condition.
Setting this property to false will allow https:// 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 https:// configuration file that exists in an otherwise non-writeable directory or NTFS Alternate Data Streams.
See https:// for more information on the feature.
See Also▲
See also https://(), https://
[static] void QSettings::setDefaultFormat(QSettings::Format format)▲
Sets the default file format to the given format, which is used for storing settings for the https://(https:// *) constructor.
If no default format is set, https:// is used. See the documentation for the https:// constructor you are using to see if that constructor will ignore this function.
See Also▲
See also https://(), https://()
void QSettings::setFallbacksEnabled(bool b)▲
Sets whether fallbacks are enabled to b.
By default, fallbacks are enabled.
See Also▲
See also https://()
[static] void QSettings::setPath(QSettings::Format format, QSettings::Scope scope, const QString &path)▲
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:
|
Platform |
Format |
Scope |
Path |
|---|---|---|---|
|
Windows |
https:// |
https:// |
FOLDERID_RoamingAppData |
|
https:// |
FOLDERID_ProgramData |
||
|
Unix |
https://, https:// |
https:// |
$HOME/.config |
|
https:// |
/etc/xdg |
||
|
macOS and iOS |
https:// |
https:// |
$HOME/.config |
|
https:// |
/etc/xdg |
The default https:// 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 https:// paths on Unix, macOS, and iOS (/etc/xdg) can be overridden when building the Qt library using the configure script's -sysconfdir flag (see https:// for details).
Setting the https:// paths on Windows, macOS, and iOS has no effect.
This function doesn't affect existing https:// objects.
See Also▲
See also https://()
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 https:// rules.
Example:
QSettings settings;
settings.setValue("interval", 30);
settings.value("interval").toInt(); // returns 30
settings.setValue("interval", 6.55);
settings.value("interval").toDouble(); // returns 6.55In Qt versions prior to 6.4, this function took https://, not https://.
See Also▲
See also https://(), https://(), https://()
QSettings::Status QSettings::status() const▲
Returns a status code indicating the first error that was met by https://, or https:// if no error occurred.
Be aware that https:// delays performing some operations. For this reason, you might want to call https://() to ensure that the data stored in https:// is written to disk before calling status().
See Also▲
See also https://()
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 https://'s destructor and by the event loop at regular intervals, so you normally don't need to call it yourself.
See Also▲
See also https://()


