Qml hierarchy with qrc

I am programming a C ++ / qml application. Since I have many qml files, I wanted to create a directory structure. I have already implemented the structure in my file system as follows:

project |- qml |- main.qml |- widgets |- Button.qml |- Label.qml 

Now I want to use qrc prefixes to create the same hierarchy:

 main.qrc / main.qml /widgets Button.qml Label.qml 

This is my qml file example:

 import QtQuick 2.2 import "widgets" Item { id: window width: 800 height: 480 Button { id: button anchors.centerIn parent text: "click me" } } 

The problem I have is that the compiler does not know Button!

EDIT:

Error message: qrc: ///qml/main.qml: 4 widgets: there is no such directory

+5
source share
2 answers

The QML interpreter is trying to load your directory in the wrong place (file system instead of qrc file). Try

 import "qrc:/widgets" 

to solve your problem.

+6
source

Make sure you add directories to the qrc file. The easiest way to do this is to right-click on the qml.qrc file in QtCreator and select "Add Existing Directory". By following this prompt, you should also help the QML interpreter find your files.

+1
source

Source: https://habr.com/ru/post/1203769/


All Articles