var QML Value Type

일반 속성 유형입니다. 더 보기...

상세 설명

var 유형은 모든 데이터 유형을 참조할 수 있는 일반 속성 유형입니다.

일반 자바스크립트 변수와 동일합니다. 예를 들어 var 속성은 숫자, 문자열, 객체, 배열 및 함수를 저장할 수 있습니다:

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 객체의 일반 프로퍼티를 변경해도 해당 객체에 액세스하는 바인딩의 업데이트가 트리거되지 않는다는 점에 유의해야 합니다. 아래 예시에서는 "자동차에는 4개의 바퀴가 있습니다."가 표시되는데, 이는 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})" 가 있다면, 자동차 속성 자체가 변경되어 변경 알림이 발생하므로 텍스트는 "자동차에는 6개의 바퀴가 있습니다"로 업데이트됩니다.

프로퍼티 값 초기화 의미

QML 구문은 속성 값 초기화 할당의 오른쪽에 있는 중괄호가 바인딩 할당을 나타내는 것으로 정의합니다. JavaScript의 빈 중괄호는 표현식 블록 또는 빈 객체 선언을 나타낼 수 있으므로 var 속성을 초기화할 때 혼동될 수 있습니다. var 속성을 빈 객체 값으로 초기화하려면 중괄호를 괄호로 묶어야 합니다.

var 유형의 프로퍼티는 기본적으로 undefined 입니다.

예를 들어

Item {
    property var first:  {}   // nothing = undefined
    property var second: {{}} // empty expression block = undefined
    property var third:  ({}) // empty object
}

앞의 예에서 first 속성은 결과가 정의되지 않은 빈 표현식에 바인딩되어 있습니다. second 속성은 하나의 빈 표현식 블록("{}")을 포함하는 표현식에 바인딩되며, 마찬가지로 정의되지 않은 결과를 갖습니다. third 속성은 빈 개체 선언으로 평가되는 표현식에 바인딩되어 있으므로 해당 빈 개체 값으로 속성이 초기화됩니다.

마찬가지로 자바스크립트에서 콜론은 객체 속성 값 할당 또는 코드 레이블이 될 수 있습니다. 따라서 객체 선언으로 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 값 유형도참조하세요 .

© 2025 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.