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. Dynamically create inline Component

Dynamically create inline Component

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 3 Posters 473 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.
  • S Offline
    S Offline
    skaldesh
    wrote on last edited by
    #1

    I would like to know if its possible to instantiate an inline Component via createObject() as with regular Component.

    Example code:

    Item {
        id: root
        component MyText: Text {
            font.pixelSize: 50
        }
    
        Component.onCompleted: {
            // This does not work
            MyText.createObject(root)
        }
    }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      skaldesh
      wrote on last edited by
      #2

      I think I found a workaround, at least:

      Component {
          id: cmpt
          MyText {}
      }
      component MyText: Text {
          font.pixelSize: 50
      }
      
      1 Reply Last reply
      0
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by
        #3

        Start with a Loader, Repeater, Instantiator, and then if those don't do what you want, look into creating objects by script. Most of the time the first 3 will suffice.

        C++ is a perfectly valid school of magic.

        1 Reply Last reply
        0
        • jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote on last edited by
          #4

          I hadn't noticed the addition of the component keyword. Thanks for pointing it out.

          The syntax I've used previously is:

          Item {
              property Component myComponent: Text { ... }
          
              Component.onComplete: myComponent.createObject()
          }
          

          Asking a question about code? http://eel.is/iso-c++/testcase/

          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