CLion 中的 Qt5 QML - QQmlApplicationEngine 无法加载组件

Qt5 QML in CLion - QQmlApplicationEngine failed to load component

我在尝试让 Qt .qml 环境在 CLion 中工作时遇到错误,因为我真的不想使用 QtCreator:

QQmlApplicationEngine failed to load component
qrc:/main.qml:2:1: module "QtQuick.Window" is not installed
qrc:/main.qml:1:1: module "QtQuick" is not installed
qrc:/main.qml:2:1: module "QtQuick.Window" is not installed
qrc:/main.qml:1:1: module "QtQuick" is not installed

不知道发生了什么,进行了大量研究,但我没有找到任何东西。 我正在尝试 运行 的 QML 文件是:

import QtQuick 2.12
import QtQuick.Window 2.12

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")
}

resources.qrc:

<RCC>
    <qresource>
        <file>main.qml</file>
    </qresource>
</RCC>

CMakeLists.txt: https://gist.github.com/zmatez/52d7a552d10d82beed8d86783b1322eb

CMake 选项:

-DCMAKE_PREFIX_PATH="E:/ProgramFiles/qt/5.15.2/mingw81_64/lib/cmake/Qt5"

main.cpp: https://gist.github.com/zmatez/52d7a552d10d82beed8d86783b1322eb

我将它与使用 QtCreator 自动生成的代码进行了比较,我的代码与创建者的代码几乎相同。 此外,QtWidgets 应用程序在我当前的配置下在 CLion 中运行良好。

感谢您的帮助

我遇到了同样的问题。感谢上面的 @folibis' 评论,我的问题解决了。按照他的建议,我只需要添加一个环境变量 QML2_IMPORT_PATH 并将其值设置为 %QTDIR%\qml.

就我而言,它不是 CLion。这是一个使用 Qt Widgets for UI 的现有项目。它将改为使用 QML。在识别 QtQuick 模块时给我带来了各种各样的麻烦。我尝试了很多方法,但没有任何效果,但这个技巧非常有效。