本页内容

重命名的类型

qmllint 将此警告类别拼写为“[renamed-type] ”。

重命名的类型

发生了什么?

一个通过QT_QML_SOURCE_TYPENAME或手动编写的 qmldir 重命名的类型,被以其未导出的名称调用。

为什么这样不好?

这会导致代码混乱,因为一个类型现在拥有多个名称。重命名的单例在使用其未导出名称时将无法正常工作,而且当前 QML 模块之外的 QML 文件无法使用该未导出名称。

示例

# CMakeLists.txt
set_source_files_properties(Tree.qml PROPERTIES QT_QML_SOURCE_TYPENAME BinaryTree)
qt_add_qml_module(MyModule
    URI MyModule
    QML_FILES Tree.qml
)
// Tree.qml
import QtQml

Item {
    property Tree left // uses old name - not ok
    property Tree right // uses old name - not ok
}

要解决此警告,请将旧名称替换为新名称:

// Tree.qml
import QtQml

Item {
    property BinaryTree left // uses new name - ok
    property BinaryTree right // uses new name - ok
}

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