导入时出现警告

此警告类别由 qmllint 拼写为[import]

导入模块失败

发生了什么?

未找到通过import 语句导入的模块。

例如,这可能是由于

为什么会这样?

应用程序无法运行是因为它找不到所依赖的模块。

示例

导入语句中的拼写错误

import QtQuicky // not ok: typo in module name

Item {
}

要修复此警告,请更正错字:

import QtQuick // ok: no typo in module name

Item {
}

未构建的用户自定义模块

某些工具,如 QML Language Serverqmllint等工具无法找到未构建的用户定义模块。如果你的项目定义了你要导入的 QML 模块,那么 QML 工具在你构建它之前是找不到它的。

注: 如果构建模块对使用 QML Language Server时,请遵循QML Language Server 设置说明中的指示,并确保将正确的构建文件夹发送到QML Language Server

错误的导入路径

有关导入路径的更多信息,请参阅QML 导入路径文档调试模块导入文档

QT_QML_GENERATE_QMLLS_INI 未被插入

请参阅QT_QML_GENERATE_QMLLS_INI 文档,了解如何设置 CMake 变量。

缺少模块

如果前面的章节没有帮助找到导入的模块,那么可能是模块丢失了。这可能是由于依赖关系缺失造成的。使用外部库时,请确认它们是否已安装,其模块是否在导入路径中。

未找到组件

发生了什么?

未找到某些组件。

为什么会这样?

应用程序无法运行,因为它无法实例化未找到的组件。

示例

组件名称中有错字

import QtQuick

Item {
    Itemy {} // not ok: typo in name
}

要修复此警告,请更正错字:

import QtQuick

Item {
    Item {} // ok: no 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

另请参阅 导入语句

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