首页 > 解决方案 > Is there Lambda support in QML files?

问题描述

I have something like this:

var arr = []
arr.indexOf(element => element.includes(''))

QtCreator gives error for this. My Qt version is 5.12 and I'm using QtCreator 4.8.

Can I use this kind of lambda expressions in QML?

标签: javascriptlambdaqmlqtquick2

解决方案


这只是一个 QtCreator 错误,箭头函数在 Qt 5.12 及更高版本中受支持。QtCreator 从 4.9 版本开始正确支持它们并停止显示警告(QTCREATORBUG-21301QTCREATORBUG-20341。请参阅4.9 更改日志)。

但是,在您的情况下,正如 folibis 所说,findIndex如果您想提供谓词,则需要。在 5.12 中确实可以使用,支持的功能的页面不是很可靠。

Qt 5.12 应该支持大多数标准的 ECMAScript 函数,查看MDN以获取参考并尝试使用您的 Qt 代码。

{
    let arr = ['a', 'ab', 'abc', 'abcd'];
    let index = arr.findIndex(element => element.includes('c'));
    let abcIsPresent = arr.includes('abc');
    print(index, abcIsPresent);
}

此代码在 Qt 5.12 中运行并输出2 true


推荐阅读