我在QML中动态创建图像。 这是我正在使用的代码:
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); }图像创建效果很好,除了我不知道如何在afterdawrds之后调用这些图像。 我无法为它们设置ID,我不知道设置objectName是否有效。
我没有得到任何错误,如果这项工作,我如何从QML调用“image3”来移动它? 我不想使用c ++。
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++.
最满意答案
我自己找到了解决方案。 首先,我添加它以获得全局访问权限
property list<ImageView> images然后我在数组中创建图像并将其复制
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! }事实证明,图像始终参考图像
Found out a solution on my own. First I add this to have global access
property list<ImageView> imagesAnd 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
更多推荐
发布评论