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
Forum Updated to NodeBB v4.3 + New Features

Dynamically create inline Component

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 3 Posters 485 Views 2 Watching
  • 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 30 Apr 2021, 10:32 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 30 Apr 2021, 10:44 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
      • F Offline
        F Offline
        fcarney
        wrote on 30 Apr 2021, 14:03 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
        • J Offline
          J Offline
          jeremy_k
          wrote on 1 May 2021, 05:10 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

          2/4

          30 Apr 2021, 10:44

          • Login

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