The QSettings class provides persistent platform-independent application settings. 更多...
继承 QObject .
The QSettings class provides persistent platform-independent application settings.
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 XML preferences files on Mac OS X. 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 custom storage formats .
QSettings 的 API 基于 QVariant , allowing you to save most value-based types, such as QString , QRect ,和 QImage ,采用 the minimum of effort.
If all you need is a non-persistent memory-based structure, consider using QMap < QString , QVariant > 代替。
When creating a QSettings 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 QSettings object as follows:
QSettings settings("MySoft", "Star Runner");
QSettings objects can be created either on the stack or on the heap (i.e. using new ). Constructing and destroying a QSettings object is very fast.
If you use QSettings from many places in your application, you might want to specify the organization name and the application name using QCoreApplication.setOrganizationName () and QCoreApplication.setApplicationName (), and then use the default QSettings 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 Mac OS X instead of the organization name, since Mac OS X applications conventionally use Internet domains to identify themselves. If no domain is set, a fake domain is derived from the organization name. See the 特定平台注意事项 below for details.)
QSettings 存储设置。构成每设置由 QString 指定设置的名称 ( key ) 和 QVariant that stores the data associated with the key. To write a setting, use setValue ()。例如:
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 sync () to commit your changes.)
可以获取设置的值使用 value ():
int margin = settings.value("editor/wrapMargin").toInt();
If there is no setting with the specified name, QSettings returns a null QVariant (which can be converted to the integer 0). You can specify another default value by passing a second argument to value ():
int margin = settings.value("editor/wrapMargin", 80).toInt();
要测试给定键是否存在,调用 contains (). To remove the setting associated with a key, call remove (). To obtain the list of all keys, call allKeys (). To remove all keys, call clear ().
因为 QVariant 属于 QtCore library, it cannot provide conversion functions to data types such as QColor , QImage ,和 QPixmap , which are part of QtGui . In other words, there is no toColor() , toImage() ,或 toPixmap() 函数在 QVariant .
相反,可以使用 QVariant.value () or the qVariantValue () 模板函数。例如:
QSettings settings("MySoft", "Star Runner"); QColor color = settings.value("DataPump/bgcolor").value<QColor>();
反向转换 (如,从 QColor to QVariant ) is automatic for all data types supported by QVariant ,包括 GUI 相关类型:
QSettings settings("MySoft", "Star Runner"); QColor color = palette().background().color(); settings.setValue("DataPump/bgcolor", color);
自定义类型注册采用 qRegisterMetaType () 和 qRegisterMetaTypeStreamOperators () can be stored using QSettings.
Setting keys can contain any Unicode characters. The Windows registry and INI files use case-insensitive keys, whereas the Carbon Preferences API on Mac OS X uses case-sensitive keys. To avoid portability problems, follow these simple rules:
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 beginGroup () 和调用 endGroup () 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();
若设置组使用 beginGroup (), the behavior of most functions changes consequently. Groups can be set recursively.
In addition to groups, QSettings also supports an "array" concept. See beginReadArray () 和 beginWriteArray () for details.
Let's assume that you have created a QSettings 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:
(见 Platform-Specific 注意事项 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 setFallbacksEnabled(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 QSettings.SystemScope (as opposed to QSettings.UserScope , the default).
让我们看下范例:
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 QSettings objects access which location. " X " means that the location is the main location associated to the QSettings 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. 用户、应用程序 | X | |||
| 2. 用户、组织 | o | X | ||
| 3. 系统、应用程序 | o | X | ||
| 4. 系统、组织 | 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 QSettings.IniFormat as the first argument to the QSettings constructor, followed by the scope, the organization name, and the application name:
QSettings settings(QSettings.IniFormat, QSettings.UserScope,
"MySoft", "Star Runner");
设置编辑器 example lets you experiment with different settings location and with fallbacks turned on or off.
QSettings is often used to store the state of a GUI application. The following example illustrates how to use QSettings 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("size", size()); settings.setValue("pos", pos()); settings.endGroup(); } void MainWindow.readSettings() { QSettings settings("Moose Soft", "Clipper"); settings.beginGroup("MainWindow"); resize(settings.value("size", QSize(400, 400)).toSize()); move(settings.value("pos", QPoint(200, 200)).toPoint()); settings.endGroup(); }
见 Window Geometry 了解为什么更好的论述,调用 QWidget.resize () 和 QWidget.move () 而不是 QWidget.setGeometry () to restore a window's geometry.
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(); } }
见 应用程序 范例,了解使用 QSettings 的自包含范例。
QSettings 可重入 . This means that you can use distinct QSettings object in different threads simultaneously. This guarantee stands even when the QSettings objects refer to the same files on disk (or to the same entries in the system registry). If a setting is modified through one QSettings object, the change will immediately be visible in any other QSettings objects that operate on the same location and that live in the same process.
QSettings 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. It uses advisory file locking and a smart merging algorithm to ensure data integrity. Note that sync () imports changes made by other processes (in addition to writing the changes from this QSettings).
作为提及在 Fallback Mechanism section, QSettings 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.
在 Unix 系统,若文件格式为 NativeFormat , the following files are used by default:
On Mac OS X versions 10.2 and 10.3, these files are used by default:
在 Windows, NativeFormat settings are stored in the following registry paths:
注意: On Windows, for 32-bit programs running in WOW64 mode, settings are stored in the following registry path: HKEY_LOCAL_MACHINE\Software\WOW6432node .
On BlackBerry only a single file is used (see 平台的局限性 ). If the file format is NativeFormat , this is "Settings/MySoft/Star Runner.conf" in the application's home directory.
若文件格式为 IniFormat , the following files are used on Unix and Mac OS X:
在 Windows,使用下列文件:
%APPDATA% path is usually C:\Documents and Settings\ User Name \Application Data ; the %COMMON_APPDATA% path is usually C:\Documents and Settings\All Users\Application Data .
On BlackBerry only a single file is used (see 平台的局限性 ). If the file format is IniFormat , this is "Settings/MySoft/Star Runner.ini" in the application's home 目录。
On Symbian, the following files are used for both IniFormat and NativeFormat (in this example, we assume that the application is installed on the e-drive and its Secure ID is 0xECB00931 ):
SystemScope settings location is determined from the installation drive and Secure ID (UID3) of the application. If the application is built-in on the ROM, the drive used for SystemScope is c: .
注意: Symbian SystemScope settings are by default private to the application and not shared between applications, unlike other environments.
路径对于 .ini and .conf files can be changed using setPath (). On Unix and Mac OS X, the user can override them by setting the XDG_CONFIG_HOME 环境变量;见 setPath () 了解细节。
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 QSettings constructor that takes a file name as first argument and pass QSettings.IniFormat as second argument. For example:
QSettings settings("/home/petra/misc/myapp.ini", QSettings.IniFormat);
You can then use the QSettings object to read and write settings in the file.
On Mac OS X, you can access XML-based .plist files by passing QSettings.NativeFormat as second argument. For example:
QSettings settings("/Users/petra/misc/myapp.plist", QSettings.NativeFormat);
On Windows, QSettings lets you access settings that have been written with QSettings (or settings in a supported format, e.g., string data) in the system registry. This is done by constructing a QSettings object with a path in the registry and QSettings.NativeFormat .
例如:
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 QSettings 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 QSettings 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.
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.
UserScope settings in Symbian are writable by any application by default. To protect the application settings from access and tampering by other applications, the settings need to be placed in the private secure area of the application. This can be done by specifying the settings storage path directly to the private area. The following snippet changes the UserScope to c:/private/ecb00931/MySoft.conf (provided the application is installed on the c-drive and its Secure ID is 0xECB00931 :
QSettings settings(QApplication.applicationDirPath() + "/MySoft.conf");
Framework libraries (like Qt itself) may store configuration and cache settings using UserScope , which is accessible and writable by other applications. If the application is very security sensitive or uses high platform security capabilities, it may be prudent to also force framework settings to be stored in the private directory of the application. This can be done by changing the default path of UserScope before QApplication is created:
#include <QSettings> #include <QDesktopServices> int main(int argc, char *argv[]) { #ifdef Q_OS_SYMBIAN // Use QDesktopServices:storageLocation as QApplication is not yet created QSettings.setPath( QSettings.NativeFormat, QSettings.UserScope, QDesktopServices.storageLocation(QDesktopServices.DataLocation) + "/settings"); #endif QApplication app(argc, argv); ... }
Note that this may affect framework libraries' functionality if they expect the settings to be shared between applications.
On Mac OS X, the global Qt settings (stored in com.trolltech.plist ) are stored in the application settings file in two situations:
In these situations, the application settings file is named using the bundle identifier of the application, which must consequently be set in the application's Info.plist 文件。
This feature is provided to ease the acceptance of Qt applications into the Mac App Store, as the default behaviour of storing global Qt settings in the com.trolltech.plist file does not conform with Mac App Store file system usage requirements. For more information about submitting Qt applications to the Mac App Store, see Preparing a Qt application for Mac App Store submission .
While QSettings 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:
#ifdef Q_WS_MAC QSettings settings("grenoullelogique.fr", "Squash"); #else QSettings settings("Grenoulle Logique", "Squash"); #endif
此枚举类型指定存储格式,用于 QSettings .
| 常量 | 值 | 描述 |
|---|---|---|
| QSettings.NativeFormat | 0 | Store the settings using the most appropriate storage format for the platform. On Windows, this means the system registry; on Mac OS X, this means the CFPreferences API; on Unix, this means textual configuration files in INI format. |
| QSettings.IniFormat | 1 | Store the settings in INI files. |
| QSettings.InvalidFormat | 16 | 返回的特殊值由 registerFormat (). |
On Unix, NativeFormat and IniFormat mean the same thing, except that the file extension is different ( .conf for NativeFormat, .ini 为 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:
pos = @Point(100 100)
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.
windir = C:\Windows
QSettings always treats backslash as a special character and provides no API for reading or writing such entries.
另请参阅 registerFormat () 和 setPath ().
This enum specifies whether settings are user-specific or shared by all users of the same system.
| 常量 | 值 | 描述 |
|---|---|---|
| QSettings.UserScope | 0 | Store settings in a location specific to the current user (e.g., in the user's home directory). |
| QSettings.SystemScope | 1 | Store settings in a global location, so that all users on the same machine access the same set of settings. |
另请参阅 setPath ().
下列状态值是可能的:
| 常量 | 值 | 描述 |
|---|---|---|
| QSettings.NoError | 0 | 没有发生错误。 |
| QSettings.AccessError | 1 | An access error occurred (e.g. trying to write to a read-only file). |
| QSettings.FormatError | 2 | A format error occurred (e.g. loading a malformed INI file). |
另请参阅 status ().
parent argument, if not None, causes self to be owned by Qt instead of PyQt.
构造 QSettings 对象为 accessing settings of the application called application from the organization called organization , and with parent parent .
范例:
QSettings settings("Moose Tech", "Facturo-Pro");
作用域设置为 QSettings.UserScope ,和 format is set to QSettings.NativeFormat (i.e. calling setDefaultFormat () 之后 calling this constructor has no effect).
另请参阅 setDefaultFormat () 和 Fallback Mechanism .
parent argument, if not None, causes self to be owned by Qt instead of PyQt.
构造 QSettings 对象为 accessing settings of the application called application from the organization called organization , and with parent parent .
若 scope is QSettings.UserScope , QSettings object searches user-specific settings first, before it searches system-wide settings as a fallback. If scope is QSettings.SystemScope , QSettings object ignores user-specific settings and provides access to system-wide settings.
存储格式被设为 QSettings.NativeFormat (i.e. calling setDefaultFormat () 之后 calling this constructor has no effect).
If no application name is given, the QSettings object will only access the organization-wide locations .
另请参阅 setDefaultFormat ().
parent argument, if not None, causes self to be owned by Qt instead of PyQt.
构造 QSettings 对象为 accessing settings of the application called application from the organization called organization , and with parent parent .
若 scope is QSettings.UserScope , QSettings object searches user-specific settings first, before it searches system-wide settings as a fallback. If scope is QSettings.SystemScope , QSettings object ignores user-specific settings and provides access to system-wide settings.
若 format is QSettings.NativeFormat , native API is used for storing settings. If format is QSettings.IniFormat , INI format is used.
If no application name is given, the QSettings object will only access the organization-wide locations .
parent argument, if not None, causes self to be owned by Qt instead of PyQt.
构造 QSettings 对象为 accessing the settings stored in the file called fileName , 采用父级 parent . If the file doesn't already exist, it is created.
若 format is QSettings.NativeFormat , meaning of fileName depends on the platform. On Unix, fileName is the name of an INI file. On Mac OS X, fileName is the name of a .plist 文件。在 Windows, fileName 是系统注册表路径。
若 format is QSettings.IniFormat , fileName 是 INI 文件的名称。
警告: 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:
另请参阅 fileName ().
parent argument, if not None, causes self to be owned by Qt instead of PyQt.
构造 QSettings 对象为 accessing settings of the application and organization set previously with a call to QCoreApplication.setOrganizationName (), QCoreApplication.setOrganizationDomain (), and QCoreApplication.setApplicationName ().
作用域是 QSettings.UserScope and the format is defaultFormat () ( QSettings.NativeFormat 默认情况下)。使用 setDefaultFormat () 之后 calling this constructor to change the default format used by this 构造函数。
代码
QSettings settings("Moose Soft", "Facturo-Pro");
相当于
QCoreApplication.setOrganizationName("Moose Soft"); QCoreApplication.setApplicationName("Facturo-Pro"); QSettings settings;
若 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 () 会返回 AccessError .
On Mac OS X, if both a name and an Internet domain are specified for the organization, the domain is preferred over the name. On other platforms, the name is preferred over the domain.
另请参阅 QCoreApplication.setOrganizationName (), QCoreApplication.setOrganizationDomain (), QCoreApplication.setApplicationName (), and setDefaultFormat ().
Returns a list of all keys, including subkeys, that can be read 使用 QSettings 对象。
范例:
QSettings settings; settings.setValue("fridge/color", 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"]
若设置组使用 beginGroup (), only the keys in the group are returned, without the group prefix:
settings.beginGroup("fridge"); keys = settings.allKeys(); // keys: ["color", "size"]
另请参阅 childGroups () 和 childKeys ().
返回用于存储设置的应用程序名称。
该函数在 Qt 4.4 引入。
另请参阅 QCoreApplication.applicationName (), format (), scope (),和 organizationName ().
追加 prefix 到当前组。
The current group is automatically prepended to all keys specified to QSettings . In addition, query functions such as childGroups (), childKeys (),和 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("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:
调用 endGroup () to reset the current group to what it was before the corresponding beginGroup() call. Groups can be nested.
另请参阅 endGroup () and group ().
添加 prefix to the current group and starts reading from an array. Returns the size of the array.
范例:
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();
使用 beginWriteArray () to write the array in the first place.
另请参阅 beginWriteArray (), endArray (),和 setArrayIndex ().
添加 prefix to the current group and starts writing an array of size size 。若 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 (int i = 0; i < logins.size(); ++i) { settings.setArrayIndex(i); settings.setValue("userName", list.at(i).userName); settings.setValue("password", list.at(i).password); } settings.endArray();
The generated keys will have the form
To read back an array, use beginReadArray ().
另请参阅 beginReadArray (), endArray (),和 setArrayIndex ().
Returns a list of all key top-level groups that contain keys that can be read using the QSettings 对象。
范例:
QSettings settings; settings.setValue("fridge/color", Qt.white); settings.setValue("fridge/size", QSize(32, 96)); settings.setValue("sofa", true); settings.setValue("tv", false); QStringList groups = settings.childGroups(); // groups: ["fridge"]
若设置组使用 beginGroup (), 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 childKeys () 和 childGroups() recursively.
另请参阅 childKeys () 和 allKeys ().
返回所有顶层键的列表,可以被读取使用 QSettings 对象。
范例:
QSettings settings; settings.setValue("fridge/color", Qt.white); settings.setValue("fridge/size", QSize(32, 96)); settings.setValue("sofa", true); settings.setValue("tv", false); QStringList keys = settings.childKeys(); // keys: ["sofa", "tv"]
若设置组使用 beginGroup (), 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 childGroups () 递归。
另请参阅 childGroups () 和 allKeys ().
移除首要位置中的所有条目,关联此 QSettings 对象。
不移除回退位置条目。
若只希望移除的条目在当前 group (),使用 remove("") 代替。
另请参阅 remove () 和 setFallbacksEnabled ().
Returns true if there exists a setting called key ; returns false otherwise.
若设置组使用 beginGroup (), key is taken to be relative to that group.
Note that the Windows registry and INI files use case-insensitive keys, whereas the Carbon Preferences API on Mac OS X uses case-sensitive keys. To avoid portability problems, see the Section and Key 句法 规则。
返回用于存储设置的默认文件格式为 QSettings ( QObject *) constructor. If no default format is set, QSettings.NativeFormat is used.
该函数在 Qt 4.4 引入。
另请参阅 setDefaultFormat () 和 format ().
Closes the array that was started using beginReadArray () 或 beginWriteArray ().
另请参阅 beginReadArray () 和 beginWriteArray ().
Resets the group to what it was before the corresponding beginGroup () 调用。
范例:
settings.beginGroup("alpha"); // settings.group() == "alpha" settings.beginGroup("beta"); // settings.group() == "alpha/beta" settings.endGroup(); // settings.group() == "alpha" settings.endGroup(); // settings.group() == ""
另请参阅 beginGroup () 和 group ().
重实现自 QObject.event ().
Returns true if fallbacks are enabled; returns false 否则。
默认情况下,回退是启用的。
另请参阅 setFallbacksEnabled ().
Returns the path where settings written using this QSettings object are stored.
在 Windows,若格式为 QSettings.NativeFormat , return value is a system registry path, not a file path.
另请参阅 isWritable () 和 format ().
返回用于存储设置的格式。
该函数在 Qt 4.4 引入。
另请参阅 defaultFormat (), fileName (), scope (), organizationName (),和 applicationName ().
返回当前组。
另请参阅 beginGroup () 和 endGroup ().
Returns the codec that is used for accessing INI files. By default, no codec is used, so a null pointer is returned.
该函数在 Qt 4.5 引入。
另请参阅 setIniCodec ().
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.
警告: This function is not perfectly reliable, because the file permissions can change at any time.
另请参阅 fileName (), status (),和 sync ().
返回用于存储设置的组织名称。
该函数在 Qt 4.4 引入。
另请参阅 QCoreApplication.organizationName (), format (), scope (),和 applicationName ().
移除设置 key and any sub-settings of key .
范例:
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().
若 key 为空字符串,所有键在当前 group () 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 Carbon Preferences API on Mac OS X uses case-sensitive keys. To avoid portability problems, see the Section and Key 句法 规则。
另请参阅 setValue (), value (),和 contains ().
返回用于存储设置的作用域。
该函数在 Qt 4.4 引入。
另请参阅 format (), organizationName (), and applicationName ().
将当前数组索引设为 i . Calls to functions such as setValue (), value (), remove (),和 contains () will operate on the array entry at that index.
必须调用 beginReadArray () 或 beginWriteArray () before you can call this function.
将默认文件格式设为给定 format , which is used for storing settings for the QSettings ( QObject *) 构造函数。
如果未设置默认格式, QSettings.NativeFormat 被使用。 See the documentation for the QSettings constructor you are using to see if that constructor will ignore this function.
该函数在 Qt 4.4 引入。
另请参阅 defaultFormat () 和 format ().
把是否启用回退设为 b .
默认情况下,回退是启用的。
另请参阅 fallbacksEnabled ().
设置访问 INI 文件的编解码器 (包括 .conf 文件在 Unix) 到 codec . The codec is used for decoding any data that is read from the INI file, and for encoding any data that is written to the file. By default, no codec is used, and non-ASCII characters are encoded using standard INI escape sequences.
警告: The codec must be set immediately after creating the QSettings object, before accessing any data.
该函数在 Qt 4.5 引入。
另请参阅 iniCodec ().
这是重载函数。
设置访问 INI 文件的编解码器 (包括 .conf 文件在 Unix) 到 QTextCodec for the encoding specified by codecName 。常见值对于 codecName include "ISO 8859-1", "UTF-8", and "UTF-16". If the encoding isn't recognized, nothing happens.
该函数在 Qt 4.5 引入。
另请参阅 QTextCodec.codecForName ().
设置用于存储设置的路径为给定 format and scope , to path 。 format 可以是自定义格式。
下表汇总了默认值:
| 平台 | Format | 作用域 | Path |
|---|---|---|---|
| Windows | IniFormat | UserScope | %APPDATA% |
| SystemScope | %COMMON_APPDATA% | ||
| Unix | NativeFormat , IniFormat | UserScope | $HOME/.config |
| SystemScope | /etc/xdg | ||
| Qt for Embedded Linux | NativeFormat , IniFormat | UserScope | $HOME/Settings |
| SystemScope | /etc/xdg | ||
| Mac OS X | IniFormat | UserScope | $HOME/.config |
| SystemScope | /etc/xdg | ||
| Symbian | NativeFormat , IniFormat | UserScope | c:/data/.config |
| SystemScope | <drive>/private/<uid> |
默认 UserScope paths on Unix and Mac OS X ( $HOME/.config or $HOME/Settings) can be overridden by the user by setting the XDG_CONFIG_HOME 环境变量。默认 SystemScope paths on Unix and Mac OS X ( /etc/xdg ) can be overridden when building the Qt library using the configure 脚本的 --sysconfdir 标志 (见 QLibraryInfo for details).
设置 NativeFormat paths on Windows and Mac OS X has no effect.
警告: 此函数不影响现有 QSettings 对象。
该函数在 Qt 4.1 引入。
另请参阅 registerFormat ().
设置值为设置 key to value 。若 key 已存在,先前值被覆写。
Note that the Windows registry and INI files use case-insensitive keys, whereas the Carbon Preferences API on Mac OS X uses case-sensitive keys. To avoid portability problems, see the Section and Key 句法 规则。
范例:
QSettings settings; settings.setValue("interval", 30); settings.value("interval").toInt(); // returns 30 settings.setValue("interval", 6.55); settings.value("interval").toDouble(); // returns 6.55
另请参阅 value (), remove (),和 contains ().
返回状态代码指示遇到的首个错误通过 QSettings ,或 QSettings.NoError if no error occurred.
注意: 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().
另请参阅 sync ().
Writes any unsaved changes to permanent storage, and reloads any settings that have been changed in the meantime by another 应用程序。
此函数被自动调用从 QSettings 's destructor and by the event loop at regular intervals, so you normally don't need to call it yourself.
另请参阅 status ().
返回值为设置 key . If the setting doesn't exist, returns defaultValue .
若未指定默认值,默认 QVariant 被返回。
Note that the Windows registry and INI files use case-insensitive keys, whereas the Carbon Preferences API on Mac OS X uses case-sensitive keys. To avoid portability problems, see the Section and Key 句法 规则。
范例:
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 0