0

I want to instantiate an image in 'sample.qml' multiple times dynamically depending on the count. I am able to create multiple qml instances in run time as below,

Item {
    id: container
    width: 500; height: 100
    z: 100
    Component.onCompleted: {
         var component = Qt.createComponent("sample.qml");
         for (var i=0; i< model.count; i++) {
             var object = component.createObject(container);
             object.x = (object.width + 250) * i;
         }
     }       
}

Creating is not the issue. But I want to assign x property to a particular instance in run time. How to achieve this and also how to destroy a particular instance. Any suggestions would be really helpful. Thanks.

I'm not able to get a particular instance of the component. Also while using destroy, it is destroying only the last instance created.

1

1 Answer 1

0

As mentioned in the comments, a Repeater is a much better approach. And it gives you the ability to reference a particular instance after they are created.

Item {
    id: container
    width: 500; height: 100
    z: 100

    Repeater {
        id: repeater

        // A model can be as simple as an integer
        model: some_number

        // The delegate is the object that you want repeated
        delegate: SomeObject {
            required property int index

            x: (width + 250) * index
        }
    }

    Component.onCompleted: {
        // Update the 5th item
        repeater.itemAt(4).color = "blue";
    } 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.