Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
enumerations.qdoc
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3/*!
4\page qtqml-typesystem-enumerations.html
5\title QML Enumerations
6\brief Description of QML Enumerations
7
8Enumerations used in QML can be defined either in QML or in C++.
9
10For information on defining enumerations in QML, see
11\l{QML Object Attributes#enumeration-attributes}{Enumeration Attributes}.
12
13When defined in C++, enumerations exposed to QML must be marked with the
14\l{Q_ENUM} or \l{Q_ENUM_NS} macros and be part of a type exposed to QML via the
15\l{QML_NAMED_ELEMENT} or \l{QML_ELEMENT} macros. For more details, see
16\l{Data Type Conversion Between QML and C++#enumeration-types}{Enumeration Types}.
17
18Only named QML types can hold enumerations usable in QML. Each enumeration must
19have a surrounding named type. \l{QML Namespaces} are also QML types and can hold
20enums.
21
22As a result, an enumeration value can be referred to as \c {<Type>.<Value>}. For
23example, the \l Text type has an \c AlignRight enumeration value:
24
25\qml
26Text { horizontalAlignment: Text.AlignRight }
27\endqml
28
29Enumeration values are properties of the type reference produced by the type
30name. You can also retrieve them using JavaScript’s square bracket syntax,
31though this is error-prone and not recommended:
32
33\qml
34// Avoid this if possible
35Text { horizontalAlignment: Text["AlignRight"] }
36\endqml
37
38\section1 Using Enumerations in QML
39
40Enumerations are not separate types in QML but are properties of their
41surrounding types. An enumeration value is represented as the underlying type of
42the enumeration. For most enumerations, it is safe to use JavaScript's
43\e Number type or QML's \e double type to store them. However, this does not
44always work for 64-bit integers, as their range exceeds the safe integer range
45of 64-bit doubles. Therefore, enumerations with values outside the safe integer
46range (-(2^53 - 1) to 2^53 - 1, inclusive) cannot be safely used in QML. For
47enumerations with values that fit into the numeric range of a 32-bit signed
48integer, you can safely use the QML \e int type as storage.
49
50For example:
51
52\qml
53import QtQuick
54
55Item {
56 // refer to Text.AlignRight using an int type
57 property int enumValue: textItem.horizontalAlignment
58
59 signal valueEmitted(int someValue)
60
61 Text {
62 id: textItem
63 horizontalAlignment: Text.AlignRight
64 }
65
66 // emit valueEmitted() signal, which expects an int, with Text.AlignRight
67 Component.onCompleted: valueEmitted(Text.AlignRight)
68}
69\endqml
70
71\sa {QML Value Types}
72\sa {QML Object Attributes#enumeration-attributes}{Enumeration Attributes}
73\sa {Data Type Conversion Between QML and C++#enumeration-types}{Enumeration Types}
74
75\section2 Enumeration key string <-> key value helpers
76
77The Qt global object provides helper methods that can map the name of a key in
78the enumeration to its numeric value or vice versa.
79
80\list
81 \li Qt.enumStringToValue: maps a string containing the name of a key to its numerical value
82 \li Qt.enumValueToString: maps a value to the name of a key with that value
83 \li Qt.enumValueToStrings: maps a value to a list of names of the keys with that value
84\endlist
85
86Example:
87\qml
88// Main.qml
89import QtQml
90
91Item {
92 enum E { Black = 0, White = 1, Grey = 2, Gray = 2 }
93
94 // 1 (White)
95 property int i: Qt.enumStringToValue(Main.E, "White")
96
97 // "Black"
98 property string s: Qt.enumValueToString(Main.E, Main.E.Black)
99
100 // ["Grey", "Gray"]
101 property list<string> ss: Qt.enumValueToStrings(Main.E, Main.E.Grey)
102}
103\endqml
104
105\sa QtQml::Qt::enumStringToValue
106\sa QtQml::Qt::enumValueToString
107\sa QtQml::Qt::enumValueToStrings
108*/