本页内容

在根元素外部声明的枚举

qmllint 将此警告类别拼写为[non-root-enum] 。

在根元素外部声明枚举

发生了什么?

在组件的根元素之外声明了一个枚举。

为什么这样不好?

将无法访问该枚举。枚举的访问方式为 <组件名称>.<可选枚举名称>.<枚举条目>。如果枚举不在组件的根元素内,则此查找将无法正常工作。

示例

// Main.qml
import QtQuick

Item {
    Item {
        id: item
        enum Color { Red, Green, Blue }
    }
}

要解决此警告,请将枚举移至组件的根节点:

// Main.qml
import QtQuick

Item {
    enum Color { Red, Green, Blue } // Accessible in Main.qml but also from other files
    Item {
        id: item
    }
}

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