QML Object Attributes
QML 文件中的對象聲明定義一個新的類型。
An object declaration in a QML document defines a new type.
It also declares an object hierarchy that will be instantiated should an instance of that newly defined type be created.
The set of QML object-type attribute types is as follows:
QML對象類型的屬性類型集合如下:
- the id attribute id
- property attributes
- signal attributes
- signal handler attributes
- method attributes
- attached properties and attached signal handler attributes
- enumeration attributes
id attribute
每個QML對象類型都只有一個id屬性。
對象的 id 用于引用該對象的實例。
id 屬性必須以小寫字母或下劃線開頭,后面可以跟著任何字母、數字或下劃線。
An object can be referred to by its id from anywhere within the component scope in which it is declared.
對象實例的id屬性值創建后,不能再修改,且在其所在的組件范圍(component scope)內必須是唯一的。
property attributes
屬性是對象的狀態信息,可以在對象的聲明中定義。
對象的 property 可以為其分配靜態值,也可以綁定到一個 dynamic expression。
屬性的值可以被其他對象讀取或寫入。(除非 a particular QML type has explicitly disallowed this for a specific property. )
類型名必須以小寫字母開頭,后面可以跟著任何字母、數字或下劃線。
JavaScript 保留字不是有效的屬性名稱。
定義 property attributes
可以通過注冊類的 Q_PROPERTY 來為 C++ 中的類型定義屬性,然后向 QML 類型系統注冊該類。
或者在聲明對象時,使用以下語法在 QML 文檔中定義對象類型的屬性:
[default] [required] [readonly] property <propertyType> <propertyName>
Declaring a custom property implicitly creates a value-change signal for that property, as well as an associated signal handler called on<PropertyName>Changed, where
Rectangle {
property color previousColor
property color nextColor
onNextColorChanged: console.log("The next color will be: " + nextColor.toString())
}
Note the var value type is a generic placeholder type that can hold any type of value, including lists and objects:
property var someNumber: 1.5
property var someString: "abc"
property var someBool: true
property var someList: [1, 2, "three", "four"]
property var someObject: Rectangle { width: 100; height: 100; color: "red" }
任一 QML 類型都可以作為屬性類型。
定制的 QML 類型也可以作為屬性類型。
為 property attributes 分配值
初始化屬性的值,可以使用 : 運算符,也可以使用 = 運算符。
<propertyName> : <value>
在聲明對象時,定義新的屬性,并為其分配值:
[default] property <propertyType> <propertyName> : <value>
浙公網安備 33010602011771號