缺少类型
该警告类别由 qmllint 拼写为[missing-type]
。
无法推断别名类型
发生了什么?
别名属性指向一个 C++ 类型的属性,但找不到其 QML 对应类型。这可能是由于导入的 QML 模块没有声明它与其他模块的 QML 依赖关系。
注意: 如果你导入的 QML 模块有外部依赖关系,请确认它们是否已安装,它们的模块是否在导入路径中。
警告还可能表明,别名引用的属性类型没有对应的 QML 类型。例如,引用的属性类型可能缺少QML_ELEMENT 宏。在这种情况下,请参阅Defining QML Types from C++或Overview - QML and C++ Integration。
为什么会这样?
QML 工具无法找到 C++ 类型的 QML 对应:编译器无法将此属性别名编译为 C++ 和qmllint,以及 QML Language Server不能分析这个属性别名。
示例
让我们的 QML 模块有一个 C++ 类,其属性myProperty
:
#include <QQuickItem> #include <QtQml/qqmlregistration.h> #include <QObject> class MyCppObject : public QObject { Q_OBJECT QML_ELEMENT public: MyCppObject(QObject *parent = nullptr) : QObject(parent) {} Q_PROPERTY(QQuickItem *myProperty READ myProperty WRITE setMyProperty NOTIFY notifyMyProperty) QQuickItem *myProperty() { return m_myProperty; } void setMyProperty(QQuickItem *item) { emit notifyMyProperty(); m_myProperty = item; } private: QQuickItem *m_myProperty; signals: void notifyMyProperty(); };
与以下CMakeLists.txt
:
project(mymodule VERSION 0.1 LANGUAGES CXX) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Qt6 6.5 REQUIRED COMPONENTS Quick) qt_standard_project_setup(REQUIRES 6.5) qt_add_executable(appmymodule main.cpp ) qt_add_qml_module(appmymodule URI mymodule VERSION 1.0 QML_FILES Main.qml HelloWorld.qml SOURCES mycppobject.cpp mycppobject.h ) target_link_libraries(appmymodule PRIVATE Qt6::Quick )
已声明 C++ 依赖关系Quick
,因此该类可以编译,并且可以找到QQuickItem include。此外,mymodule
与QtQuick 没有任何依赖关系。
现在,让我们尝试在 QML 的别名中使用myProperty
。程序可以运行,但 QML 工具(如编译器)会抱怨使用了myProperty
:
import mymodule MyCppObject { id: root property alias myAlias: root.myProperty // not ok: Cannot deduce type of alias [missing-type] }
出现警告信息的原因是,在 QML 代码中,myProperty
和它的 QML 对应文件Item
的类型QQuickItem
是未知的,即使你的 QML 文件中有import QtQuick
。这是因为同一类型可以在不同模块中以不同属性多次公开:mymodule
实际上必须精确了解myProperty
的 QML 类型。
要修复此警告,请在CMakeLists.txt
中添加依赖关系:
qt_add_qml_module(mymodule URI mymodule ... # declarare QML dependency to QtQuick module DEPENDENCIES QtQuick ... )
现在,警告应该消失了!
另请参阅"声明模块依赖关系"。
未找到属性类型
发生了什么事?
一个绑定被设置在一个找不到 QML 类型的属性上。这可能是由于 QML 模块没有声明它对其他模块的 QML 依赖性。
注意: 如果你导入的 QML 模块有外部依赖关系,请确认它们是否已安装,以及它们的模块是否在导入路径中。
警告还可能表明,属性类型没有对应的 QML 类型。例如,属性类型可能缺少QML_ELEMENT 宏。在这种情况下,请参阅Defining QML Types from C++或Overview - QML and C++ Integration。
为什么会这样?
QML 工具无法找到 C++ 类型的 QML 对应:编译器无法将此属性绑定编译到 C++ 和qmllint,也无法分析此属性绑定。 QML Language Server不能分析这个属性绑定。
示例
让我们的 QML 模块有一个 C++ 类,它有两个属性:myProperty
和myProperty2
:
#include <QQuickItem> #include <QtQml/qqmlregistration.h> #include <QObject> class MyCppObject : public QObject { Q_OBJECT QML_ELEMENT public: MyCppObject(QObject *parent = nullptr) : QObject(parent) {} Q_PROPERTY(QQuickItem *myProperty READ myProperty WRITE setMyProperty NOTIFY notifyMyProperty) QQuickItem *myProperty() { return m_myProperty; } void setMyProperty(QQuickItem *item) { emit notifyMyProperty(); m_myProperty = item; } Q_PROPERTY(QQuickItem *myProperty2 READ myProperty2 WRITE setMyProperty2 NOTIFY notifyMyProperty2) QQuickItem *myProperty2() { return m_myProperty2; } void setMyProperty2(QQuickItem *item) { emit notifyMyProperty2(); m_myProperty2 = item; } private: QQuickItem *m_myProperty; QQuickItem *m_myProperty2; signals: void notifyMyProperty(); void notifyMyProperty2(); };
下面是CMakeLists.txt
:
project(mymodule VERSION 0.1 LANGUAGES CXX) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Qt6 6.5 REQUIRED COMPONENTS Quick) qt_standard_project_setup(REQUIRES 6.5) qt_add_executable(appmymodule main.cpp ) qt_add_qml_module(appmymodule URI mymodule VERSION 1.0 QML_FILES Main.qml HelloWorld.qml SOURCES mycppobject.cpp mycppobject.h ) target_link_libraries(appmymodule PRIVATE Qt6::Quick )
已声明 C++ 依赖关系Quick
,因此该类可编译,并可找到QQuickItem include。此外,mymodule
与QtQuick 没有任何依赖关系。
现在,让我们尝试在 QML 的别名中将myProperty2
与myProperty
绑定。程序会运行,但 QML 工具(如编译器)会抱怨使用了myProperty
:
import mymodule MyCppObject { id: root myProperty: myProperty2 // not ok: No type found for property "myProperty". [missing-type] }
出现警告信息的原因是,在 QML 代码中,myProperty
的QQuickItem
类型及其 QML 对应的Item
类型不详:CMakeLists.txt
中未声明 mymodule 的依赖关系 "QtQuick"。
要修复这个警告,请在CMakeLists.txt
中添加依赖关系:
qt_add_qml_module(mymodule URI mymodule ... # declarare QML dependency to QtQuick module DEPENDENCIES QtQuick ... )
现在,警告应该消失了!
另请参阅 " 声明模块依赖关系"。
© 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.