Loader: how to setSource when target qmlfile resides in qml module
-
Hello,
I have a Client application which imports several qml modules.
Im working on a menu bar for my application. In total I have four buttons right now. Each button is supposed to load a different qml component.
The qml components generally get imported via following code. I just providedMyQml
module. For the other modules it should be the same.engine.addImportPath(QStringLiteral(":/MyQml/imports"));
which works fine. When I do
MyQml {}
everything works fine and the component is displayed correctly.
All the needed qml imports are present.MyButton { id: btn1 backgroundColor: "white" icon: "qrc:/icon/factory.svg" view: "MyQml.qml" onClicked: root.viewSelected(view) }
This button is part of the menu bar. I omited the rest of the implementation, because I think the general flow is clear.
Unfortunately, when given the raw string of
MyQml.qml
to the Loader I get following errorqrc:/qt/qml/Client/qml/MyQml.qml: No such file or directory
. Obviously qrc can not find the qml there, bc it resides in the module.How am I supposed to reference a qml-file by string that resides in a module to be able to pass it to the Loader so its gets loaded successfully?
-
The import path is irrelevant to the
Loader
. It'ssource
has to be set to a validQUrl
.
If I interpret it correctly, that's:/MyQml/imports/MyQml.qml
in your case.If it's not, you can add the following to
main.cpp
to dump all available resource files:#include <QDirIterator> // at the top, unless already included. QDirIterator it(":", QDirIterator::Subdirectories); while (it.hasNext()) qInfo() << it.next();
-
@Axel-Spoerl said in Loader: how to setSource when target qmlfile resides in qml module:
The import path is irrelevant to the Loader. It's source has to be set to a valid QUrl.
If I interpret it correctly, that's :/MyQml/imports/MyQml.qml in your case.
Thank you very much!
The final solution was to import the file as following:
"qrc:/MyQml/imports/core/plugins/MyQml/qml/MyQml.qml"
qrc:/RESOURCE_PREFIX/URI/MyQml.qml
Which is concatenated from following cmake configuration:
qt_add_qml_module(${PROJECT_NAME} STATIC URI core.plugins.myqml VERSION 1.0 RESOURCE_PREFIX myqml/imports SOURCES ... )
-