QML: Lambda function works unexpectedly
Try something like this:
item.someValueChanged.connect(function(capture) {
return function() {
handler(capture)}
}(item))
Intuitive, right? :D
If JS used "block scope" there would be 3 different item
s being referenced for each loop iteration, and it would "work as expected". But with "function scope" there is only one item
referenced, and it references its final value, thus the need to use that hack to "capture" each value in time.
Just to explain it, in case it isn't immediately obvious, the signal is connected to a handler that is arbitrated by a function which captures the parameter value at the particular time as a discrete object, which is used to feed to the handler.
Hopefully, the incipient Qt 5.12 release will remedy that with the introduction of support for let
, a.k.a block scoped variables.
Update: I can confirm that using 5.12, it now works as expected:
let item = someObj.createObject(); // will produce 3 distinct obj refs