Provides persistent platform-independent application settings. 更多...
import 语句: | import Qt.labs.settings 1.0 |
The Settings type provides persistent platform-independent application settings.
注意: This type is made available by importing the Qt.labs.settings 模块。 Types in the Qt.labs module are not guaranteed to remain compatible in future versions.
Users normally expect an application to remember its settings (window sizes and positions, options, etc.) across sessions. The Settings type enables you to save and restore such application settings with the minimum of effort.
Individual setting values are specified by declaring properties within a Settings element. All 基本类型 properties are supported. The recommended approach is to use property aliases in order to get automatic property updates both ways. The following example shows how to use Settings to store and restore the geometry of a window.
import QtQuick.Window 2.1 import Qt.labs.settings 1.0 Window { id: window width: 800 height: 600 Settings { property alias x: window.x property alias y: window.y property alias width: window.width property alias height: window.height } }
At first application startup, the window gets default dimensions specified as 800x600. Notice that no default position is specified - we let the window manager handle that. Later when the window geometry changes, new values will be automatically stored to the persistent settings. The second application run will get initial values from the persistent settings, bringing the window back to the previous position and size.
A fully declarative syntax, achieved by using property aliases, comes at the cost of storing persistent settings whenever the values of aliased properties change. Normal properties can be used to gain more fine-grained control over storing the persistent settings. The following example illustrates how to save a setting on component destruction.
import QtQuick 2.1 import Qt.labs.settings 1.0 Item { id: page state: settings.state states: [ State { name: "active" // ... }, State { name: "inactive" // ... } ] Settings { id: settings property string state: "active" } Component.onDestruction: { settings.state = page.state } }
Notice how the default value is now specified in the persistent setting property, and the actual property is bound to the setting in order to get the initial value from the persistent settings.
Application specific settings are identified by providing application name , organization and domain , or by specifying fileName .
#include <QGuiApplication> #include <QQmlApplicationEngine> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); app.setOrganizationName("Some Company"); app.setOrganizationDomain("somecompany.com"); app.setApplicationName("Amazing Application"); QQmlApplicationEngine engine("main.qml"); return app.exec(); }
These are typically specified in C++ in the beginning of
main()
, but can also be controlled in QML via the following properties:
Application settings may be divided into logical categories by specifying a category name via the category property. Using logical categories not only provides a cleaner settings structure, but also prevents possible conflicts between setting keys.
If several categories are required, use several Settings objects, each with their own category:
Item { id: panel visible: true Settings { category: "OutputPanel" property alias visible: panel.visible // ... } Settings { category: "General" property alias fontSize: fontSizeSpinBox.value // ... } }
Instead of ensuring that all settings in the application have unique names, the settings can be divided into unique categories that may then contain settings using the same names that are used in other categories - without a conflict.
The current implementation is based on QSettings . This imposes certain limitations, such as missing change notifications. Writing a setting value using one instance of Settings does not update the value in another Settings instance, even if they are referring to the same setting in the same category.
The information is stored in the system registry on Windows, and in XML preferences files on macOS. On other Unix systems, in the absence of a standard, INI text files are used. See QSettings 文档编制了解更多细节。
另请参阅 QSettings .
category : string |
This property holds the name of the settings category.
Categories can be used to group related settings together.
fileName : string |
This property holds the path to the settings file. If the file doesn't already exist, it is created.
该特性在 Qt 5.12 引入。
另请参阅 QSettings::fileName and QSettings::IniFormat .
设置值为设置 key to value . If the key already exists, the previous value is overwritten.
该方法在 Qt 5.12 引入。
另请参阅 value() and QSettings::setValue .
把任何未保存改变写入永久存储,并重新加载同时已被其它应用程序改变的任何设置。
此函数被自动调用从 QSettings 的析构函数和通过定期间隔的事件循环,因此,通常不需要自己调用它。
另请参阅 QSettings::sync .
返回值为设置 key 。若设置不存在,返回 defaultValue .
该方法在 Qt 5.12 引入。
另请参阅 setValue() and QSettings::value .