在本页

枚举键的大小写

qmllint 将此警告类别拼写为[enum-key-case] 。

枚举键以小写字母开头

发生了什么?

某个枚举中包含一个以小写字母开头的键。

为什么这样不好?

这会在执行查找时造成歧义。枚举键应以大写字母开头,以便将其与单例和关联对象的属性区分开来。不遵循此约定可能会导致意外结果。

示例

// Properties.qml
import QtQuick

Item {
    enum Color { red, green, blue }
}
// Main.qml
import QtQuick

Item {
    // This lookup could be interpreted as a property lookup on a Singleton or attached object.
    property int i: Properties.green
}

要修复此警告,请重命名该键,使其以大写字母开头。

// Properties.qml
import QtQuick

Item {
    enum Color { Red, Green, Blue }
}
// Main.qml
import QtQuick

Item {
    // No ambiguity for the user or the QML engine, this is an enum lookup.
    property int i: Properties.Green
}

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