Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Qml JS Array callBackFn

Qml JS Array callBackFn

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlqml + jsjavascriptforeacharray
4 Posts 3 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Konstantin Schraubek
    wrote on 3 Feb 2020, 13:08 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • I Offline
      I Offline
      IntruderExcluder
      wrote on 3 Feb 2020, 13:20 last edited by IntruderExcluder 2 Mar 2020, 13:21
      #2

      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); })
          }
      
      1 Reply Last reply
      2
      • G Offline
        G Offline
        GrecKo
        Qt Champions 2018
        wrote on 3 Feb 2020, 13:35 last edited by
        #3

        Arrow function (=>) are available since Qt 5.12 : https://doc.qt.io/qt-5/whatsnew512.html#qt-qml-module

        For documentation about general JS types, MDN is a good resource : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

        1 Reply Last reply
        1
        • K Offline
          K Offline
          Konstantin Schraubek
          wrote on 4 Feb 2020, 06:39 last edited by
          #4

          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.

          1 Reply Last reply
          0

          1/4

          3 Feb 2020, 13:08

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved