导入时出现警告
qmllint 将此警告类别拼写为[import] 。
导入模块失败
发生了什么?
无法找到通过import 语句导入的模块。
这可能是由于以下原因造成的,例如
- import 语句中的拼写错误,或者
- 用户定义的模块未被构建,或者
- 导入路径错误,或者
- 模块缺失
这为什么不好?
应用程序无法运行,因为它找不到所依赖的模块。
示例
导入语句中的拼写错误
import QtQuicky // not ok: typo in module name
Item {
}要解决此警告,请更正拼写错误:
import QtQuick // ok: no typo in module name
Item {
}未构建的用户自定义模块
某些工具(如 QML Language Server 或qmllint等工具在用户定义模块未构建时无法找到它们。如果您的项目定义了您试图导入的 QML 模块,那么在构建该模块之前,QML 工具将无法找到它。
注意:如果 在使用 QML Language Server时,仍无法解决此问题,请按照QML Language Server 中的设置说明操作,并确保向QML Language Server 提供正确的构建文件夹路径。
导入路径错误
有关导入路径的更多信息,请参阅QML 导入路径文档和调试模块导入文档。
模块缺失
如果前面的章节未能帮助您找到被导入的模块,则该模块可能缺失。这可能是由于依赖项缺失所致。在使用外部库时,请确认它们确实已安装,且其模块位于导入路径中。
未找到组件
发生了什么?
未找到某个组件。
这有什么问题?
应用程序无法运行,因为无法实例化该未找到的组件。
示例
组件名称中的拼写错误
import QtQuick
Item {
Itemy {} // not ok: typo in name
}要解决此警告,请更正拼写错误:
缺少导入语句
Item { // not ok: must be imported from QtQuick first
}要修复此警告,请添加缺失的模块导入:
import QtQuick
Item { // ok: was imported from QtQuick
}导入限定符必须以大写字母开头
发生了什么?
某些导入的模块具有无效的限定符。
这有什么问题?
使用此无效限定符导入的模块无法使用。
示例
import QtQuick as qq
qq.Item {
}要解决此警告,请确保导入限定符以大写字母开头:
import QtQuick as Qq
Qq.Item {
}未知导入语法
发生了什么?
某条导入语句使用了无效的导入语法。
这有什么问题?
应用程序无法运行,因为它无法导入其依赖的模块。
示例
import "¯\(ツ)/¯:/path/to/Module"
import QtQuick
Item {
}要解决此警告,请使用具有允许方案的 URL:
import "qrc:/path/to/Module"
import QtQuick
Item {
}注意:此 示例假设您未使用URL handlers 。
另请参阅 “导入语句”。
© 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.