Qml JS Array callBackFn
-
Hey Guys,
as https://doc.qt.io/qt-5/qtqml-javascript-functionlist.html states, theoreticly all these functions are usable in qml.
Unfortunately there are no examples and no sample code.
Could someone please provide sample code on how to use Array.forEach(callbackfn [, thisArg]). Or callbackfn in general?
A brief look in the internet had not helped me.My understanding would be to do some like this:
onSlot:{ someArray.forEach(element => console.log(element)); }
This is of cause not working because the "=>" combo.
Thanks in advance.
-
The
=>
works fine for me at least with Qt 5.14. And this should be fine for some earlier versions:Component.onCompleted: { let array = [1, 2, 3, 4, 5]; array.forEach(e => console.log("Element: " + e)) }
Anyway you can use old style syntax:
Component.onCompleted: { let array = [1, 2, 3, 4, 5]; array.forEach(function (e) { console.log("Element: " + e); }) }
-
Arrow function (
=>
) are available since Qt 5.12 : https://doc.qt.io/qt-5/whatsnew512.html#qt-qml-moduleFor documentation about general JS types, MDN is a good resource : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
-
Thanks guys!
Im on Qt 5.10, that explains why the "=>" is not working. Had not known about the old style syntax.Settings this to closed.