0

I feel a bit ashamed but I simply don't manage to add a file to my QML project in Qt Creator.

I use Qt 6.2.3 and my build system is qmake.

First off, I created a file MyButton.qml by doing a right click on the folder named QML of a project which only contains a main.qml file.

Then, when I type MyButton in main.qml I can use the autocompletion so I see Qt Creator detects my new type.

But during executation, I got this error :

QQmlApplicationEngine failed to load component
qrc:/MyProject/main.qml:9:5: MyButton is not a type

Anyone knows how to add a file to a project ?

I carefuly followed the official documentation (https://doc.qt.io/qt-6/qtqml-documents-definetypes.html) and several tutorials but nothing works. That's crazy.

EDIT :

I did what JarMan advises but I still get the same error.

Here is an image of my project arborescence

And the content of the qml.qrc file :

<RCC>
    <qresource prefix="/">
        <file>MyButton.qml</file>
        <file>main.qml</file>
    </qresource>
</RCC>
Fila2016
  • 53
  • 1
  • 6

3 Answers3

0

I believe the problem you're running into is simply that your executable doesn't know where to find your .qml file because the build folder is usually not the same as the source folder. To fix this, you should create a resource file (you can name it something like qml.qrc) and list all of your QML files in there. This allows the QML file to get bundled into the executable.

An example would look like this:

<RCC>
    <qresource prefix="/">
        <file>MyButton.qml</file>
    </qresource>
</RCC>

Then in your .pro file, add this line:

RESOURCES += qml.qrc

EDIT:

You can do the same thing in Qt Creator.

Step 1:

You might already have a resource file in your project. If so, you'll see it in the Project view on the left side of the window. If you do, you can skip to Step 2. Otherwise, go to File->New File... and select Qt Resource File. Name it qml.qrc (or whatever you like).

Step 2:

Once you've created that file, it should now show up in the Project view on the left. You can right-click on the file name and select Add Existing Files. Choose your MyButton.qml file. That should do exactly what I mentioned in my original answer, but without manually typing any code.

JarMan
  • 7,589
  • 1
  • 10
  • 25
  • I have this file qmake_resources.qrc in this folder : build-MyProject-Qt_6_2_3_for_macOS-Debug Do you know a way to modify it without hard-coding it ? In it I just see : – Fila2016 Apr 16 '22 at 16:07
  • Don't mess with anything in the build folder. That's the output from your build. I'll make an edit to my answer with instructions for Qt Creator. – JarMan Apr 16 '22 at 16:11
0

I got the same error with Qt 6.3.1

I found the Qt Creator doesn't generate the qml.qrc file when you select Minimum required Qt version: to Qt 6.2.

So I switched the Minimum required Qt version: to Qt 5.15 in Qt Creator Wizard, the qml.qrc file will generated and my custom component qml file would be included automatically in it.

Also, the .pro configure file has different items between these two Qt version.

0

Solution 1: add qml in the pro file: resources.files = main.qml MyButton.qml

Solution 2: add a qrc file into project and add qml as resource file

Jack
  • 1
  • 2