var QML Value Type
汎用プロパティ型。詳細...
詳細な説明
var 型は、任意のデータ型を参照できるジェネリックなプロパティ型です。
これは、通常の JavaScript 変数と同等です。たとえば、`var properties` には、数値、文字列、オブジェクト、配列、関数を格納できます。
Item {
property var aNumber: 100
property var aBool: false
property var aString: "Hello world!"
property var anotherString: String("#FF008800")
property var aColor: Qt.rgba(0.2, 0.3, 0.4, 0.5)
property var aRect: Qt.rect(10, 10, 10, 10)
property var aPoint: Qt.point(10, 10)
property var aSize: Qt.size(10, 10)
property var aVector3d: Qt.vector3d(100, 100, 100)
property var anArray: [1, 2, 3, "four", "five", (function() { return "six"; })]
property var anObject: { "foo": 10, "bar": 20 }
property var aFunction: (function() { return "one"; })
}変更通知の動作
var プロパティに割り当てられた JavaScript オブジェクトの通常のプロパティに変更があっても、それらにアクセスするバインディングの更新はトリガーされない点に注意することが重要です。以下の例では、「The car has 4 wheels」と表示されます。これは、wheels プロパティの変更が、「text」プロパティに割り当てられたバインディングの再評価を引き起こさないためです:
Item {
property var car: new Object({wheels: 4})
Text {
text: "The car has " + car.wheels + " wheels";
}
Component.onCompleted: {
car.wheels = 6;
}
}もし onCompleted ハンドラが代わりに `"car = new Object({wheels: 6})" ` となっていた場合、text は「The car has 6 wheels」に更新されます。これは、car プロパティ自体が変更され、変更通知が発行されるためです。
プロパティ値の初期化のセマンティクス
QMLの構文では、プロパティ値の初期化代入の右側に置かれる中括弧が、バインディングの代入を表すと定義されています。var プロパティを初期化する際、これは混乱を招く可能性があります。JavaScriptにおいて、空の中括弧は式ブロックまたは空のオブジェクト宣言のいずれかを表すためです。var 型のプロパティを空のオブジェクト値で初期化したい場合は、中括弧を丸括弧で囲む必要があります。
var 型のプロパティは、デフォルトでundefined 型です。
例:
Item {
property var first: {} // nothing = undefined
property var second: {{}} // empty expression block = undefined
property var third: ({}) // empty object
}前の例では、first プロパティは空の式にバインドされており、その結果はundefinedとなります。second プロパティは、単一の空の式ブロック(「{}」)を含む式にバインドされており、同様に結果はundefinedとなります。third プロパティは、空のオブジェクト宣言として評価される式にバインドされているため、このプロパティはその空のオブジェクト値で初期化されます。
同様に、JavaScript におけるコロンは、オブジェクトのプロパティ値への代入、あるいはコードラベルのいずれかとなり得ます。したがって、var プロパティをオブジェクト宣言で初期化する際も、括弧が必要になる場合があります:
Item {
property var first: { example: 'true' } // example is interpreted as a label
property var second: ({ example: 'true' }) // example is interpreted as a property
property var third: { 'example': 'true' } // example is interpreted as a property
Component.onCompleted: {
console.log(first.example) // prints 'undefined', as "first" was assigned a string
console.log(second.example) // prints 'true'
console.log(third.example) // prints 'true'
}
}「QMLの値型」も参照してください 。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.