Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Dynamic QML loading and import path

Dynamic QML loading and import path

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    djoulez
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bobweaver
      wrote on last edited by
      #2

      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 ?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        djoulez
        wrote on last edited by
        #3

        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 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved