0
votes

I'm working on a project in which all .js and .qml files are stored in the Qt Resource file (.qrc). I've tried to import an external directory in a qml file. The external directory contains other .qml files for different purposes. I don't want to include these external directories into the .qrc file.

I get an error when I add the import path saying:

qrc:\example.qml : cannot find directory

Is there any way to include an external file or directory like this.

1
Why are you storing cpp and qml files in the resource file? It's not designed for that purpose. - TheDarkKnight
You can always use a full path not prefixed by "qrc:" or ":". But as already mentioned, what makes you store cpp and qml files into the resource of this app? Those are not needed at run-time. - Sebastian Lange
Hi Merlin and Sebastian, It was a mistake in my question. I have corrected it. But Regarding qml, i think it is the most effective way to use qrc, as qrc will hold all the qml files in it during program execution. - Ansif
Please can you show some code to how you're accessing a file in the qrc and also the the text of the qrc file? - TheDarkKnight
Hi, I used this line "import "file:/../qml" as QmlDirectory" for importing a directory named "qml", after that i called QmlDirectory.Palette {} to display the Palette.qml file stored in the "qml" directory. These lines are done inside another "main.qml" file, that is stored in the qrc file. - Ansif

1 Answers

0
votes

Found a solution in the Qt forum, http://qt-project.org/forums/viewthread/7047. For accessing any file outside QRC, use "absolute filepath" of the file. For example: In main.cpp file:

QString path = QDir::currentPath(); //path where the exec is present

If your file is in src/file.qml of exec folder, then you can access it like (path += "/src/file.qml";), now path is the absolute file path for file.qml. You can access it in any of the QRC file.

QQuickView view; 
view.rootContext()->setContextProperty("myFile", path);
view.setSource("qrc:/main.qml"); 

In main.qml file:

Loader
{
    id: loadItem
    source: myFile
}
Item
{
   Component.onCompleted: loadItem.item
}