限制类型

此警告类别由 qmllint 拼写为[restricted-type]

您不能从此处访问非作用域枚举

发生了什么?

您通过枚举类型名称访问了 C++ 中定义的枚举值。

为什么会这样?

C++ 中定义的非作用域枚举不能通过其枚举类型名称访问。它们在运行时将是未定义的。

示例

import QtQuick
import SomeModule // contains MyClass

Item {
    property int i: MyClass.Hello.World
}

其中 MyClass 定义为

class MyClass: public QObject
{
    Q_OBJECT
    QML_ELEMENT

public:
    enum Hello { World };
    Q_ENUM(Hello);
    ...

};

要修复此警告,请从 QML 使用中删除不必要的枚举类型名:

import QtQuick

Item {
    property int i: MyClass.World
}

如果你是枚举的作者,你也可以修改枚举定义,使用枚举类,而不是修改 QML 代码:

class MyClass: public QObject
{
    Q_OBJECT
    QML_ELEMENT

public:
    enum class Hello { World };
    Q_ENUM(Hello);
    ...
};

注: 有关枚举类型注册的更多信息,请点击此处

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