이 페이지에서

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 속성에 할당된 자바스크립트 객체의 일반 속성에서 변경 사항이 발생하더라도, 이를 참조하는 바인딩의 업데이트는 트리거되지 않는다는 점에 유의해야 합니다. 아래 예제에서는 wheels 속성의 변경이 "text" 속성에 할당된 바인딩의 재평가를 유발하지 않으므로 "The car has 4 wheels"가 표시됩니다:

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})" `가 지정되어 있었다면, `car` 속성 자체가 변경되어 변경 알림이 발생하므로 텍스트가 "The car has 6 wheels"로 업데이트될 것입니다.

속성 값 초기화 의미론

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 속성은 빈 객체 선언으로 평가되는 표현식에 바인딩되어 있으므로, 이 속성은 해당 빈 객체 값으로 초기화됩니다.

마찬가지로, 자바스크립트에서 콜론(:)은 객체 속성 값 할당일 수도 있고, 코드 레이블일 수도 있습니다. 따라서 객체 선언으로 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.