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.
類別可以被用於同時爲相關的設置分組。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章