Dynamic QML loading and import path



  • 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
    -->...
    --> qmldir

    I 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.0

    and 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



  • 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 ?



  • 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)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.