How to call ImageView by objectName in QML

162 views Asked by At

I am dynamically creating images in my QML. This is the code I am using:

for (var n = 0; n < 3 * numberOfTiles; n ++) {
            var image = imageDefinition.createObject();
            image.translationX = getX(n);
            image.translationY = getY(n);
            image.objectName = ("image"+n) 
            drawContainer.add(image);
        }

Image creating works well, except I don't know how to call those images afterdawrds. I can't set them an ID, and I don't know if setting objectName like that works.

I don't get any errors, and if this work, how can I call "image3" from QML to move it? I don't want to use c++.

1

There are 1 answers

0
Bojan Kogoj On BEST ANSWER

Found out a solution on my own. First I add this to have global access

property list<ImageView> images

And then I create images in array and copy them over

onCreationCompleted: {
   //you need a tmp variable, since you can't do that with property variant
   var imagesTmp = Array();
    for (var n = 0; n < 3 * numberOfTiles; n ++) {
        imagesTmp[n] = imageDefinition.createObject()
        imagesTmp[n].translationX = getX(n);
        imagesTmp[n].translationY = getY(n);
        drawContainer.add(imagesTmp[n]);
    }
    images = imagesTmp;
    images[3].translationX = 10; //as example, this works!
}

As it turns out images keeps reference to the image