Settings QMl Type

Contents(内容)

  • Properties(属性)
  • Detailed Description(细节描述)
    • Application Identifiers(应用程序标识)
    • Categories(类别)
    • Notes(备注)

Settings QML Type
import Statement : import Qt.labs.settingss 1.0
Properties(属性)

  • category(类别) : string(字符串)

Detailed Description(细节描述)
The Settings type provides persistent platform-independent application settings.(设置类型提供永久性的独立平台的应用程序的设置。)
Note: This type is made available by importing the Qt.labs.settings module. Types in the Qt.labs module are not guaranteed to remain compatible in future versions.
备注: 这个类型通过插入Qt.labs.settings 模块激活。Qt.labs 的类型模块不能保证在未来的版本上仍然兼容。
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.
用户通常期望一个应用程序在跨回话中保留它的原本设置(比如窗口的大小和位置、选项等。)Settings type 可以使你用最小的开销来保存和恢复应用程序的设置。
Individual setting values are specified by declaring properties within a Settings element. All basic type 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.
个人设置的值通过定义Settings 元素的属性来指定。支持所有基本的属性类型。推荐使用属性别名的方法来使用两种方法来自动更新属性。以下例子展示如何去使用Setting 来储存和恢复窗口的几何属性。

  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.
在第一个应用程序启动时,该窗口获取默认尺寸指定为 800 x 600。注意这个没有默认的位置被指定–我们让窗口管理器处理这种情况。后续当窗口的几何大小更改后,新的数值会自动存储到永久设置。第二个应用程序运行会从永久的设置中获得初始化的值,使窗口回到原来的位置和大小
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 Identifiers(应用程序标识)
Application specific settings are identified by providing application name, organization and domain.
应用程序的特定设置通过提供应用程序的名称、组织和域名来标识。

  #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:
这些通常在C++的main() 开始中指定,但也可以通过以下属性在Qml中控制:

  • Qt.application.name,
  • Qt.application.organization and
  • Qt.application.domain.

Categories(类别)
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.
应用程序设置可以通过类别属性指定一个类别名称来分为逻辑类别。使用逻辑类别不仅提供一个清除的设置结构体,但也可以避免设置键可能带来的冲突。

  Item {
      id: panel

      visible: true

      Settings {
          category: "OutputPanel"
          property alias visible: panel.visible
          // ...
      }
  }

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.
而不是确保在应用程序中的所有设置都具有唯一的名称,设置可以分为独特的类别,然后可能包含使用相同的名称,用于在其他类别-不发生冲突的设置。

Notes备注
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.
当前的实现基于 QSettings。这强制了某些限制性,如丢失更改通知。写一个设置值,使用一个实例的设置不会更新另一个设置实例中的值,即使他们指在同一类别相同的设置。
Property Documentation(属性文档)

category : string

This property holds the name of the settings category.
这个属性保存设置类别的名称
Categories can be used to group related settings together.
类别可以被用于同时为相关的设置分组。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值