Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Dynamic creation of a "standard" QML object (not from my own .qml file)

    QML and Qt Quick
    3
    7
    2763
    Loading More Posts
    • 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.
    • F
      frankiefrank last edited by

      All the articles/posts I found show the use of Qt.createComponent() to load a Component from a QML url, and then to instantiate it:
      @
      component = Qt.createComponent(sourceQml);
      myControl = component.createObject(parentElement);
      @

      I want to dynamically create an instance of a "known" QML object that I don't have the path to. In my case it's RadioButton from the Qt-Components library but it can be anything else.

      What's the syntax for it?

      "Roads? Where we're going, we don't need roads."

      1 Reply Last reply Reply Quote 0
      • T
        task_struct last edited by

        You can use "createQmlObject()":http://doc.qt.nokia.com/4.7-snapshot/qml-qt.html#createQmlObject-method

        "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

        • Linu...
        1 Reply Last reply Reply Quote 0
        • F
          frankiefrank last edited by

          That would help me if I wanted to code the QML itself on the fly.
          But I want to use an existing QML object. Like <Image> or <Text>.

          "Roads? Where we're going, we don't need roads."

          1 Reply Last reply Reply Quote 0
          • T
            task_struct last edited by

            @
            var newObject = Qt.createQmlObject('import QtQuick 1.0; Rectangle {}', parentItem, "dynamicSnippet1");
            @

            This will create a Rectangle item.

            "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

            • Linu...
            1 Reply Last reply Reply Quote 0
            • F
              frankiefrank last edited by

              That's a cool way of doing it. Thanks!

              "Roads? Where we're going, we don't need roads."

              1 Reply Last reply Reply Quote 0
              • M
                mbrasser last edited by

                Another option I've heard suggested is:

                @
                Component {
                id: component
                RadioButton {}
                }
                ...
                myControl = component.createObject(parentElement);
                @

                1 Reply Last reply Reply Quote 0
                • F
                  frankiefrank last edited by

                  I ended up using a wrapper QML file anyway for other needs, but I will actually need to update it now to another component. So I think I'll try this new method too. Thanks!

                  "Roads? Where we're going, we don't need roads."

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post