Qt Forum

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

    Unsolved Dynamically create inline Component

    QML and Qt Quick
    3
    4
    147
    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.
    • S
      skaldesh last edited by

      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 Reply Quote 0
      • S
        skaldesh last edited by

        I think I found a workaround, at least:

        Component {
            id: cmpt
            MyText {}
        }
        component MyText: Text {
            font.pixelSize: 50
        }
        
        1 Reply Last reply Reply Quote 0
        • fcarney
          fcarney last edited by

          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 Reply Quote 0
          • jeremy_k
            jeremy_k last edited by

            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 Reply Quote 0
            • First post
              Last post