QML:Lambda 函数工作异常
我认为 QML 支持 lambda 函数是因为 JavaScript 支持匿名函数以及函数是一流对象的事实,但它们并没有按我的预期工作.拿这个代码:
I thought QML supported lambda functions because of JavaScript's support of anonymous functions and the fact that functions are first class objects, but they don't work how I expected. Take this code:
Item {
property var items: []
function handler( item ) {
console.log( item );
}
Component.onCompleted: {
for ( var i = 0; i < 3; ++i ) {
var item = someObj.createObject();
item.someValueChanged.connect( function() {
handler( item ); } );
items.push( item );
console.log( "Adding:", item );
}
}
Component {
id: someObj
Item {
property bool someValue: false
Timer {
running: true
onTriggered: {
parent.someValue = true;
}
}
}
}
}
我正在尝试使用 lambda function() { handler( item );}
以便在发出 someObj::someValueChanged
信号时,将发出项目传递给 handler( item )
函数.
I'm trying to use the lambda function() { handler( item ); }
so that when the someObj::someValueChanged
signal is emitted the emitting item is passed to the handler( item )
function.
我假设每个循环都会创建一个新的 lambda 实例,并且 item
引用将携带 someObj
实例的引用在该循环中创建(即 item
将被 lambda 捕获).但情况似乎并非如此,因为输出是:
I assumed that each loop would create a new instance of the lambda and that the item
reference would carry the reference of the someObj
instance created in that loop (i.e. item
would be captured by the lambda). But that doesn't seem to be the case as the output is:
qml: Adding: QQuickItem_QML_1(0x2442aa0)
qml: Adding: QQuickItem_QML_1(0x2443c00)
qml: Adding: QQuickItem_QML_1(0x2445370)
qml: QQuickItem_QML_1(0x2445370)
qml: QQuickItem_QML_1(0x2445370)
qml: QQuickItem_QML_1(0x2445370)
如您所见,要么在每个循环中替换整个函数,要么仅替换 item
引用,因此最终只引用最后创建的 someObj
.有人可以向我解释为什么 lambdas(如果它就是这样的话)不能按我期望的方式工作吗?这是 QML 问题,还是一般的 JavaScript 问题?
As you can see, either the whole function is being replaced on each loop or just the item
reference, so that ultimately only the last created someObj
is referred to. Can someone explain to me why lambdas (if that's even what it is) don't work the way I expect? And is this a QML issue, or a general JavaScript one?
推荐答案
试试这样的:
item.someValueChanged.connect(function(capture) {
return function() {
handler(capture)}
}(item))
直观,对吧?:D
如果 JS 使用块作用域",则每次循环迭代都会引用 3 个不同的 item
,它会按预期工作".但是对于函数范围",只有一个 item
被引用,并且它引用了它的最终值,因此需要使用该 hack 及时捕获"每个值.
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.
希望最初的 Qt 5.12 版本将通过引入对 let
(也就是块范围变量)的支持来解决这个问题.
Hopefully, the incipient Qt 5.12 release will remedy that with the introduction of support for let
, a.k.a block scoped variables.
更新:我可以确认使用 5.12,它现在可以按预期工作:
Update: I can confirm that using 5.12, it now works as expected:
let item = someObj.createObject(); // will produce 3 distinct obj refs
相关文章