Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Solved Qml JS Array callBackFn

    QML and Qt Quick
    qml qml + js javascript foreach array
    3
    4
    653
    Loading More Posts
    • 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
      Konstantin Schraubek last edited by

      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 Reply Quote 0
      • IntruderExcluder
        IntruderExcluder last edited by IntruderExcluder

        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 Reply Quote 2
        • GrecKo
          GrecKo Qt Champions 2018 last edited by

          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 Reply Quote 1
          • K
            Konstantin Schraubek last edited by

            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 Reply Quote 0
            • First post
              Last post