Add resource file (*.qrc) to static library. How?
-
Hello all!
Trying to add QML files to static library (need to create universal library within QML). All is working when directly adding QML with 'qt_add_qml_module'. Something like this:
qt_add_qml_module(${A_NAME_TARGET} URI "Library_v1" QML_FILES "ATestQML.qml" )
How to add *.qrc files to library and how to use it in the main project? When adding *.qrc file to the library with 'qt_add_qml_module' return error:
qt_add_qml_module(${A_NAME_TARGET} URI "Library_v1" RESOURCES TestResources.qrc )
error in the main project:
QQmlApplicationEngine failed to load component qrc:/qt/qml/Application_v1/Main.qml:12:2: ATestQML is not a type
-
I don't know anything specific about QML modules, but in general - if you want to load a resource file from a static library, you need explicitly load it by adding the
Q_INIT_RESOURCE
macro somewhere in your main application target. See the docs: https://doc.qt.io/qt-6/qtresource-qtcore-proxy.html#Q_INIT_RESOURCE -
Hello all!
Trying to add QML files to static library (need to create universal library within QML). All is working when directly adding QML with 'qt_add_qml_module'. Something like this:
qt_add_qml_module(${A_NAME_TARGET} URI "Library_v1" QML_FILES "ATestQML.qml" )
How to add *.qrc files to library and how to use it in the main project? When adding *.qrc file to the library with 'qt_add_qml_module' return error:
qt_add_qml_module(${A_NAME_TARGET} URI "Library_v1" RESOURCES TestResources.qrc )
error in the main project:
QQmlApplicationEngine failed to load component qrc:/qt/qml/Application_v1/Main.qml:12:2: ATestQML is not a type
@bogong said in Add resource file (*.qrc) to static library. How?:
How to add *.qrc files to library and how to use it in the main project?
You would use
qt_add_resources()
for *.qrc files: https://doc.qt.io/qt-6/qt-add-resources.htmlHowever, if your *.qml files are inside a *.qrc then you won't get the full benefits of
qt_add_qml_module()
(such as optimization via the Qt Quick Compiler: https://www.qt.io/blog/the-numbers-performance-benefits-of-the-new-qt-quick-compiler).It is better to move all your files out of the *.qrc, and directly into the
qt_add_qml_module()
:qt_add_qml_module(${A_NAME_TARGET} URI Library_v1 QML_FILES ATestQML.qml BTestQML.qml RESOURCES myimage.png myotherimage.png )