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 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
    • IntruderExcluderI Offline
      IntruderExcluderI Offline
      IntruderExcluder
      wrote on last edited by IntruderExcluder
      #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
      • GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote on 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 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

          • Login

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