PDA

View Full Version : QML Date set problem



lqsa
21st October 2015, 15:22
In this code:


import QtQuick 2.0
import QtQuick.Controls 1.4

Item {
id: root
property date value: new Date(2000, 0, 1)
Button {
onClicked: {
root.value.setFullYear(2001)
console.log(root.value)
}
}
}

When push the button, why date doesn't change? On the log appears 2000, not 2001

anda_skoa
24th October 2015, 16:56
onClicked: {
var d = root.value
d.setFullYear(2001)
root.value = d
console.log(root.value)
}


Cheers,
_

P.S.: the other posting wasn't a real reply, it was just spam.

lqsa
25th October 2015, 21:55
Thank you very much for your response.

Could you explain why my code doesn't work? I would like to understand it.

Best regards.

anda_skoa
26th October 2015, 08:35
I don't know why exactly either.

It seems that the JavaScript context treats the property object more like a local object then a property.
Only writes to the property itself are actual modifications of the property.

Same happens, for example, if the property is a JavaScript array. I.e. you need to write a modified array value to the property in order for the property to change.

My guess is that the QML engine can't easily track if the object has changed (JavaScript objects don't have change notification for their contents), so it requires and actual assignment to the property to be sure it can properly (re-)evaluate all bindings on that property.

Cheers,
_