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 creation of a "standard" QML object (not from my own .qml file)
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 3 Posters 3.1k 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.
  • F Offline
    F Offline
    frankiefrank
    wrote on last edited by
    #1

    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
    0
    • T Offline
      T Offline
      task_struct
      wrote on last edited by
      #2

      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
      0
      • F Offline
        F Offline
        frankiefrank
        wrote on last edited by
        #3

        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
        0
        • T Offline
          T Offline
          task_struct
          wrote on last edited by
          #4

          @
          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
          0
          • F Offline
            F Offline
            frankiefrank
            wrote on last edited by
            #5

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

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

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mbrasser
              wrote on last edited by
              #6

              Another option I've heard suggested is:

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

              1 Reply Last reply
              0
              • F Offline
                F Offline
                frankiefrank
                wrote on last edited by
                #7

                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
                0

                • Login

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