如何在QML中通过objectName调用ImageView(How to call ImageView by objectName in QML)

编程入门 行业动态 更新时间:2024-10-28 10:35:55
如何在QML中通过objectName调用ImageView(How to call ImageView by objectName in QML)

我在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> 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

更多推荐

本文发布于:2023-08-03 20:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1401086.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   QML   objectName   ImageView   call

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!