3

I'm getting an error while trying to get Qt .qml environment working in CLion, cause I don't really want to use 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

No idea what's going on, made a huge research and I haven't found anything. My QML file I'm trying to run is:

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 options:

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

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

I was comparing it with auto-generated code with QtCreator, and my is almost identical as the creator's one. Also, QtWidgets application was working fine in CLion on my current configuration.

Thanks for any help

zeFree
  • 2,129
  • 2
  • 31
  • 39
matez
  • 142
  • 1
  • 11
  • 3
    You probably have to define path to Qt, especially QML modules, `QML2_IMPORT_PATH` ot whatever. The question actually doen't relate to QML but mostly to CLion. I guess there are lots of tutorials across the Internet. – folibis Mar 17 '21 at 18:55
  • Woah that worked! Added environment variable QML2_IMPORT_PATH as "qt\5.15.2\mingw81_64\qml" and now it starts! Thanks ;)) And no there are no tutorials about QML in Clion, at least I couldn't find any. – matez Mar 18 '21 at 19:33
  • @folibis thanks for your comment. I am getting the same error but in different case. I deployed a qml application as an *appimage* it works fine in deployed pc. but when I moved this appimage to another pc, it gives on terminal this same error. What is the problem and how can I solve it ? – Yunus Temurlenk Aug 10 '21 at 09:27

2 Answers2

0

I was having the same issue. Thanks to @folibis' comment above my problem was solved. As suggested by him, I just had to add an environment variable QML2_IMPORT_PATH and set its value to %QTDIR%\qml.

In my case it was not CLion. It was an existing project which was using Qt Widgets for UI. It was to be changed to use QML instead. It was giving me all sorts of troubles while identifying the QtQuick module. I tried many things but nothing worked, but this trick worked like a charm.

zeFree
  • 2,129
  • 2
  • 31
  • 39
0

I am using Clion 2023.1.1 now. Here is my approach without setting environment variable in your computer directly. At first, open Clion and create a new "Qt Widgets Executable" project. Make sure you have set the correct "Qt Cmake prefix path". A correct path should be looked like "E:/Qt/6.5.0/mingw_64/lib/cmake". Then, you should edit your "CMakeLists.txt" file:

set(CMAKE_PREFIX_PATH "E:/Qt/6.5.0/mingw_64/lib/cmake")#If this line is not present, please add it
set(QML2_IMPORT_PATH "E:/Qt/6.5.0/mingw_64/qml")
find_package(Qt6 COMPONENTS
    Core
    Gui
    Widgets
    Qml
    Quick
    REQUIRED)
add_executable(Convenient_Scripts main.cpp qml.qrc)
target_link_libraries(untitled
    Qt::Core
    Qt::Gui
    Qt::Widgets
    Qt::Qml
    Qt::Quick)

Here is my project structure:

Interfaces
    main.qml
CMakeLists.txt
main.cpp
qml.qrc

Here is the content of file "qml.qrc":

<RCC>
    <qresource prefix="/">
        <file>Interfaces/main.qml</file>
    </qresource>
</RCC>

Here is the content of file "main.qml":

import QtQuick
import QtQuick.Controls

ApplicationWindow {
    height: 300
    visible: true
    width: 300
}

By default, the folder "cmake-build-debug" has been created. Then copy all the desired ".dll" files into this folder. You can find them in "E:\Qt\6.5.0\mingw_64\bin". Finally, compile your project and run it!

#include <QApplication>
#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[]) {
    QGuiApplication application(argc,argv);
    QQmlApplicationEngine engine;

    engine.load(QUrl("qrc:/Interfaces/main.qml"));
    if (engine.rootObjects().isEmpty()){
        return -1;
    }

    return QGuiApplication::exec();
}