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
restricted-type.qdoc
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\ingroup qmllint-warnings-and-errors
6
7\title Restricted type
8\brief [restricted-type] A restricted type was accessed.
9
10\qmllintwarningcategory restricted-type
11
12\section1 You can't access an unscoped enum from here
13
14\section2 What happened?
15You accessed the value of an enum defined in C++ by its enum type name.
16
17\section2 Why is this bad?
18Unscoped enums defined in C++ can't be accessed by their enum type name.
19They will be undefined at runtime.
20
21\section2 Example
22
23\qml
24import QtQuick
25import SomeModule // contains MyClass
26
27Item {
28 property int i: MyClass.Hello.World
29}
30
31\endqml
32where MyClass is defined as
33\code
34class MyClass: public QObject
35{
36 Q_OBJECT
37 QML_ELEMENT
38
39public:
40 enum Hello { World };
41 Q_ENUM(Hello);
42 ...
43
44};
45\endcode
46To fix this warning, remove the unnecessary enum type name from its QML usage:
47\qml
48import QtQuick
49
50Item {
51 property int i: MyClass.World
52}
53
54\endqml
55
56If you are the author of the enum, you can also modify the enum definition to use an enum
57class instead of changing the QML code:
58
59\code
60class MyClass: public QObject
61{
62 Q_OBJECT
63 QML_ELEMENT
64
65public:
66 enum class Hello { World };
67 Q_ENUM(Hello);
68 ...
69};
70\endcode
71
72\note You can find more information about enum type registration
73\l{qtqml-cppintegration-data.html#enumeration-types}{here}.
74
75*/