QML: Lambda function works unexpectedly(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
这篇关于QML:Lambda 函数工作异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:QML:Lambda 函数工作异常
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01