QML - reference to image element
-
Hi everyone,
Is it possible in qml to use some kind of a reference to an image element and then use this reference to set properties?
I have two image elements (id:img1, id:img2).
Depending on the value of a signal i would like to change several properties, either of img1 or of img2.Example:
imgX = <Referenc_to_img1>
imgX.width : 100
imgX.x : 200
imgX.source : <filename>
...Thanks for any help,
Ben
-
Yep, you can do: var imgX = img1 ...assuming you're setting imgX in the same function you're manipulating the properties.
Here's an example but using rectangles:function changeImage() { var imgX = img1; imgX.width = 150; imgX.height = 50; } Rectangle { id: img1 color: "red" width: 100 height: 100 } Rectangle { id: img2 color: "blue" x: 200 width: 100 height: 100 }
-
Cool, thanks. The first solution works well.
When using the "property" solution, why does the following code not work?
-> Error while compiling: "Cannot assign a value directly to a grouped property"property var idX
idX : rectRed
idX.color : "green" -> ERRORRectangle{ id : rectRed color: "red" }
-
We can't tell what you're doing from a couple of lines of code with no context. Where are you declaring the property and trying to do "idX.color: "green"? The colon is a binding operator, not an assignment; you can't use it in place of an = operator everywhere.
-
I'm sorry, i wanted to keep the post short. Here comes my testcode.
Thanks for your help,
Benimport QtQuick 2.3
import QtQuick.Window 2.2Window {
visible: true
width: 360
height: 360Rectangle{ id : top anchors.fill: parent property var idX idX : rectRed idX.color : "green" Rectangle{ id : rectRed width : 100 height: width color: "red" } }
}
-
You can do "idX.color = "green" in a JavaScript context, but I don't think you can do a binding like that.