Dynamic QML loading and import path
-
wrote on 24 Sept 2013, 12:40 last edited by
Hello,
Is there a way to dynamically create a QML object coming from an imported directory?
Let's consider the directory as follows:
/home/user/qml/data
-->MyObject1.qml
-->MyObject2.qml
-->...
--> qmldirI added QML_IMPORT_PATH=/home/user/qml in the pro file and in put this in the main.qml:
import QtQuick 2.0
import data 1.0and in the Component.onCompleted block I try to call Qt.createComponent("MyObject1.qml") but it doesn't work.
What is the correct way of doing this ?
Thanks
-
wrote on 24 Sept 2013, 16:31 last edited by
not sure if this is what you mean. but here you go
@ function createComponentObjects() {
component = Qt.createComponent("Foo.qml");
// here we create more things about are component
// sprite = component.createObject(appWindow, {"x": 100, "y": 100});
if (sprite == null) {
// Error Handling
console.log("Error creating object");
}
}/// other ways
var newObject = Qt.createQmlObject('import QtQuick 2.0;
Rectangle {
color: "red";
width: 20;
height: 20}',
parentItem, "dynamicSnippet1");@resources http://qt-project.org/doc/qt-4.8/qdeclarativedynamicobjects.html
or are you talking about using a Loader ?
-
wrote on 24 Sept 2013, 18:42 last edited by
Hello,
I use the createComponent function as you described:
@ function createComponentObjects() {
component = Qt.createComponent("Foo.qml");
// here we create more things about are component
// sprite = component.createObject(appWindow, {"x": 100, "y": 100});
if (sprite == null) {
// Error Handling
console.log("Error creating object");
}
@It works fine when the loaded Foo.qml file is in the same folder than my main.qml. But the goal is to have the Foo.qml in another directory than the main.qml file. The idea is to load some qmls that are only available at runtime (i.e downloaded from a remote server to a directory)
1/3