Article directory
1. Introduction to QSettings
Often when you design an application, you want to remember its settings (window size, position, options, etc.) after the application is closed. This information is typically 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 KDE applications) use INI text files to save the application's parameter settings. Qt provides the QSettings class to support application parameter saving. Using QSetting, you can save and restore application settings in the portable manager. The QSettings API is based on QVariant and can save most value-based types, such as QString ORect and Qlmage, with minimal effort.
2. Instructions for using QSettings
-
Initialization of QSettings
When creating a QSettings object, you must pass the name of the company or organization and the name of the application. For example, if the product is named Star Runner and the company is named MySoft, you need to construct the QSettings object as follows:QSettings settings("MySoft", "Star Runner");
If you use Q settings in multiple places in your application, you may need to use
QCoreApplication:setOrganizationName()
andQCoreApplication:setApplicationNamel()
specify the organization name and application name, and then use the default QSettings hook function:QCoreApplication::setOrganizationName("MySoft"); QCoreApplication::setOrganizationDomain("mysoft.com"); QCoreApplication::setApplicationName("Star Runner"); ... QSettings settings;
Sometimes you want to access settings stored in a specific file or registry path. On all platforms, if you want to read the INI file directly, you can use
QSettings
the constructor, which takes the file name as the first parameter and will pass itQSettings::IniFormat
as the second parameter. For example:QSettings settings("/home/petra/misc/myapp.ini", QSettings::IniFormat);
-
Parameter settings for QSettings
Each setting consists of a specified setting name (composed of a section and key, separated by/
)QString
and a section that stores data related to the keyQVariant
. UsesetValue()
functions to set parameters. For example:settings.setValue("editor/wrapMargin", 68);
If a setting with the same key already exists, the new value will overwrite the existing value. For efficiency reasons, changes may not be saved immediately to permanent storage. (Can be called
sync()
to commit changes.) -
Parameter reading of QSettings Reading parameters
throughvalue()
functions:int margin = settings.value("editor/wrapMargin").toInt();
If no setting with the specified name is found, QSettings will return an empty
QVariant
(can be converted to the integer 0). You canvalue()
specify another default value by passing a second argument to (this default argument will be returned even if it does not exist):int margin = settings.value("editor/wrapMargin", 80).toInt();
-
Check and delete parameters in QSettings
Calledcontains()
to test whether the given key exists. Called toremove()
delete the settings associated with a key. CalledallKeys()
to get a list of all keys. Calledclear()
to delete all keys. -
Naming rules for parameter names (keys)
Setting keys can contain any Unicode characters. The Windows registry and INI files use case-insensitive keys, while the CFPreferences API on macOS and iOS uses case-sensitive keys. To avoid portability issues, follow these simple rules:- Always use the same case to reference the same key. For example, if you call a key somewhere in your code
text font
, don't call it somewhere elseText Fonts
. - Except for case, avoid using the same key name. For example, if you have a
MainWindow
key called , don't try to save another key asmainwindow
. /
Do not use slashes ( and ) in section or keyword names\
; the backslash character is used to separate subkeys (see below). On the window,\
converted by QSettings/
, this makes them exactly the same.
Hierarchical keys can be formed using the "/" character as a delimiter, 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
beginGroup()
the prefix and call it at the endendGroup()
. Here's the same example, 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();
- Always use the same case to reference the same key. For example, if you call a key somewhere in your code
-
QSettings' thread-safe
Q settings are reentrant. This means that different QSettings objects can be used simultaneously in different threads. Even though the QSettings object refers to the same file on disk (or the same entry in the system registry). If a setting is modified through one QSettings object, the changes will be immediately seen in any other QSettings objects operating in the same location and in the same process. As long as certain conditions are met, Q settings can be safely used to read and write to the same system location from different processes (which can be different instances of an application running simultaneously, or completely different applications). ForQSettings::IniFormat
, it uses advisory file locking and smart merge algorithms to ensure data integrity. The condition for this is that the writable configuration file must be a regular file and must be located in a directory where the current user can create new temporary files -
The storage location of the configuration file saved by QSettings in the system
-
On Unix systems,
NativeFormat
the following is used by default if the file format is (orXDG_CONFIG_DIRS
the/etc/XDG
default if not set):- $HOME/.config/MySoft/Star Runner.conf (Qt for Embedded Linux: $HOME/Settings/MySoft/Star Runner.conf)
- $HOME/.config/MySoft.conf (Qt for Embedded Linux: $HOME/Settings/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
-
On macOS versions 10.2 and 10.3, the following 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,
NativeFormat
settings are stored in the following registry path (On Windows, for 32-bit programs running in WOW64 mode, settings are stored in the following registry path: . in theHKEY_LOCAL_MACHINE\Software\WOW6432node
application's home directorySettings/MySoft/StarRunner.conf
.):- 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
-
If the file format is
IniFormat
, the following is used on Unix, macOS, and iOS (if not setXDG_CONFIG_DIRS
,/etc/XDG
the default is used.):- $HOME/.config/MySoft/Star Runner.ini (Qt for Embedded Linux: $HOME/Settings/MySoft/Star Runner.ini)
- $HOME/.config/MySoft.ini (Qt for Embedded Linux: $HOME/Settings/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
On Windows, use the following files (
FOLDRID_RoamingAppData
usually pointed toC:\Users\UserName\AppData\Roaming
, also%AppData%
shown by environment variables.FOLDERID_ProgramData
Usually pointed toC:\ProgramData
.):- FOLDERID_RoamingAppData\MySoft\Star Runner.ini
- FOLDERID_RoamingAppData\MySoft.ini
- FOLDERID_ProgramData\MySoft\Star Runner.ini
- FOLDERID_ProgramData\MySoft.ini
-