QML 열거형
QML에서 사용되는 열거형은 QML 또는 C++에서 정의할 수 있습니다.
QML에서 열거형을 정의하는 방법에 대한 자세한 내용은 열거형 속성을 참조하십시오.
C++에서 정의할 경우, QML에 노출되는 열거형은 Q_ENUM 또는 Q_ENUM_NS 매크로로 표시되어야 하며 QML_NAMED_ELEMENT 또는 QML_ELEMENT 매크로를 통해 QML에 노출되는 유형의 일부여야 합니다. 자세한 내용은 열거형 유형을 참조하십시오.
명명된 QML 유형만 QML에서 사용할 수 있는 열거형을 보유할 수 있습니다. 각 열거형에는 주변 명명된 유형이 있어야 합니다. QML 네임스페이스도 QML 유형이며 열거형을 포함할 수 있습니다.
따라서 열거형 값은 <Type>.<Value>
로 참조할 수 있습니다. 예를 들어 Text 유형에는 AlignRight
열거형 값이 있습니다:
Text { horizontalAlignment: Text.AlignRight }
열거형 값은 유형 이름에 의해 생성된 유형 참조의 속성입니다. JavaScript의 대괄호 구문을 사용하여 검색할 수도 있지만 오류가 발생하기 쉬우므로 권장하지 않습니다:
// Avoid this if possible Text { horizontalAlignment: Text["AlignRight"] }
QML에서 열거형 사용
열거형은 QML에서 별도의 유형이 아니라 주변 유형의 속성입니다. 열거형 값은 열거형의 기본 유형으로 표시됩니다. 대부분의 열거형의 경우 JavaScript의 숫자 타입이나 QML의 이중 타입을 사용하여 저장하는 것이 안전합니다. 그러나 64비트 정수의 경우 그 범위가 안전한 정수 범위인 64비트 배수를 초과하기 때문에 이 방법이 항상 작동하는 것은 아닙니다. 따라서 안전 정수 범위를 벗어나는 값(-(2^53 - 1) ~ 2^53 - 1, 포함)을 가진 열거형은 QML에서 안전하게 사용할 수 없습니다. 32비트 부호 있는 정수의 숫자 범위에 맞는 값을 가진 열거형의 경우 QML int 형을 저장소로 안전하게 사용할 수 있습니다.
예를 들어
import QtQuick Item { // refer to Text.AlignRight using an int type property int enumValue: textItem.horizontalAlignment signal valueEmitted(int someValue) Text { id: textItem horizontalAlignment: Text.AlignRight } // emit valueEmitted() signal, which expects an int, with Text.AlignRight Component.onCompleted: valueEmitted(Text.AlignRight) }
© 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.