PDA

View Full Version : QML / JS value/reference problem



sedi
7th March 2016, 20:02
Hi,

i am trying to implement a menu with several sub-menus. Therefore when changing to a sub-menu, I want to push the current color information into an array / var property, like I do with the actions themselves. The problem: I cannot copy the value of a color. It's always the reference that is copied, so the whole storing process becomes pointless, the stored color changes as soon as I assign the new color.

the QML properties are:

property alias color: actionBarMainRect.color;
property var colorsUpperLevel: []

In JS I use this code

var color = actionBarLeft.color

actionBarLeft.colorsUpperLevel.push(color)

console.log("pushed color1: "+ actionBarLeft.colorsUpperLevel[actionBarLeft.colorsUpperLevel.length-1])
console.log("actionBarLeft.color1" + actionBarLeft.color)
console.log("menuColor1: "+menuColor)
console.log("color1: "+color)

actionBarLeft.color = menuColor

console.log("pushed color2: "+ actionBarLeft.colorsUpperLevel[actionBarLeft.colorsUpperLevel.length-1])
console.log("actionBarLeft.color2" + actionBarLeft.color)
console.log("menuColor2: "+menuColor)
console.log("color2: "+color)

with this result:

qml: pushed color1: #008400 // old color, stored correctly
qml: actionBarLeft.color1#008400 // still old
qml: menuColor1: #8400ff // the new color
qml: color1: #008400 // everything ok
//---> Now the assignment happens
qml: pushed color2: #8400ff // ok, we wanted to change this.
qml: actionBarLeft.color2#8400ff // but not this!!
qml: menuColor2: #8400ff
qml: color2: #8400ff // and not this!!

Any ideas?

anda_skoa
7th March 2016, 22:59
Just to be sure, you have tried using a variable name that is not the same as the property, right?

Maybe try creating a new color, e.g. using Qt.lighter() with factor 1.0

Cheers,
_

sedi
7th March 2016, 23:16
Actually, I hadn't tried that before, I'm afraid - but for sure I have by now. That didn't help.

But... fortunately

var myColor = Qt.lighter(actionBarLeft.color,1.0)
Does help indeed! Cool approach.
Thank you for this birthday present :-)

Cheeeers!!