Load shared library with QML files in resource file
-
Hi,
I have the following use-case:I want to protect the QML code of a dynamic library and thus add the QML files to a qrc file. The library is created by compiling the resource file into the library which is a QDeclarativeExtensionPlugin, so it is also possible to mix C++ code and QML code in the library.
From my application, I then load the library by importing the plugin. In order the QML components are found, I also added the qmldir file into the qrc file of the library.
If i import the plugin now from the application, it only works if a QML component of the library is not created in the main.qml file. When I load it dynamically either with Qt.createComponent() or the Loader element it works fine, but if the component is in the main.qml file the error output is
@file::/qml/PluginApplication/main.qml: TestItem is not a type@
So to clarify, here is an overview what works and what not:
@
// main.qml:import QtQuick 1.1
import MyPlugin 1.0Item {
id: window// does NOT work, TestItem is a QML component in the dynamic library
TestItem {}Component.onCompleted: {
// this works
var comp = Qt.createComponent("qrc:/plugins/MyPlugin/TestItem.qml")
var obj = comp.createObject(window)
}// this also works
Loader {
id: loader
x: 100
y: 100
}
MouseArea {
anchors.fill: parent
onClicked: {
// the DynamicallyLoadedTestItem.qml contains a TestItem as its root element
loader.source = "DynamicallyLoadedTestItem.qml"
}
}
}
@It looks like the dynamic library is not loaded when qmlViewer.setMainQmlFile() is called, although the import works as the later creation of TestItem works. From what I've read in the docs, the Q_INIT_RESOURCE_EXTERN() macro should be used when dynamic libraries are used which contain resource files. However, it has no effect.
Similar threads like "this one":http://qt-project.org/forums/viewthread/25461 also did not solve the problem.
Any ideas how to properly initialize the plugin?
-
I found out, that putting a DynamicallyLoadedTestItem into the main.qml file did work! But only if the import of the plugin is added in the main.qml, otherwise the same error with type not being found occurs.
Here is the source code:
@
// main.qml:import QtQuick 1.1
import MyPlugin 1.0Item {
// this DOES WORK! but only if import MyPlugin 1.0 is added in main.qml
DynamicallyLoadedTestItem {}// this causes the error "TestItem is not a Type"
TestItem {}
}// DynamicallyLoadedTestItem.qml:
import MyPlugin 1.0
TestItem { }
@It seems that in the first loaded qml file the dynamic library is not loaded, but when another file is loaded it is available. Shouldn't the QML engine take care of completely loading the library before initializing it? Or how can I load it in advance so the library is accessible when the first qml file gets parsed?