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. [ SOLVED ] QML : how to pass javascript function as argument in another function
Forum Updated to NodeBB v4.3 + New Features

[ SOLVED ] QML : how to pass javascript function as argument in another function

Scheduled Pinned Locked Moved QML and Qt Quick
12 Posts 2 Posters 6.9k Views 1 Watching
  • 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.
  • shavS Offline
    shavS Offline
    shav
    wrote on last edited by
    #2

    Hi,

    If I understood correct you want call JS function from other function, right? If so did you try something like this:
    @
    Component.onCompleted: {
    fn34(fn);
    }

    function fn(){
       console.log("test function");
    }
    
    function fn34(fnc)
    {
        fnc();
    }
    

    @

    or
    @
    Component.onCompleted: {
    fn34( function() {
    console.log("Test function");
    });
    }
    @

    Mac OS and iOS Developer

    1 Reply Last reply
    0
    • L Offline
      L Offline
      laithbasildotnetgmail.com
      wrote on last edited by
      #3

      This is what I already do , if you see the link I post.

      but the type of fnc is undefined always !

      somehow Its feels like QtQuick does not support javascript well which is not what they say in the documentation

      1 Reply Last reply
      0
      • shavS Offline
        shavS Offline
        shav
        wrote on last edited by
        #4

        hm....

        In my case I've got this:
        @
        qml: fnc: function() { [code] }
        qml: ljkljlk
        @

        Mac OS and iOS Developer

        1 Reply Last reply
        0
        • L Offline
          L Offline
          laithbasildotnetgmail.com
          wrote on last edited by
          #5

          What Qt version you use ? I use Qt 5.4 maybe this is a specific bug

          1 Reply Last reply
          0
          • shavS Offline
            shavS Offline
            shav
            wrote on last edited by
            #6

            I've use Qt 5.4.

            Also I've check code from link which you share in your first post. Are you use self before call function? Could you remove it and call function without self?

            If you need to set QML component as a object with function you need to set id of element.

            Mac OS and iOS Developer

            1 Reply Last reply
            0
            • L Offline
              L Offline
              laithbasildotnetgmail.com
              wrote on last edited by
              #7

              I already did that, I even separated the code in standalone the js file.
              but still not work !

              Do you use Qt 5.4 on windows , mac or linux ? I use it on windows 8.1

              1 Reply Last reply
              0
              • shavS Offline
                shavS Offline
                shav
                wrote on last edited by
                #8

                I use Mac version. Could you share your JS code and QML file where you trying use code?

                Mac OS and iOS Developer

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  laithbasildotnetgmail.com
                  wrote on last edited by
                  #9

                  this is in the standalone GameHelperJS.js file

                  @
                  function innerTraversal(obj, visitFun){
                  console.log(typeof visitFun);
                  if(obj!== self){
                  visitFun.func(obj);
                  }

                  if(hasChilderns(obj)){
                      var objChilderns = obj.children;
                  
                      for(var i=0 ; i< objChilderns.length ; i++){
                          innerTraversal(objChilderns[i]);
                      }
                  }
                  

                  }

                  function hasChilderns(obj){
                  if(typeof obj.children !== 'undefined')
                  return true;
                  else
                  return false;
                  }

                  function applyReset(obj){
                  if(typeof obj.reset === 'function')
                  obj.reset();
                  }

                  function applyRefresh(obj){
                  if(typeof obj.refresh === 'function')
                  obj.refresh();
                  }

                  @

                  and this is in the item I try to call the function with called Game.qml

                  @
                  import QtQuick 2.0
                  import "Scripts/GameHelperJS.js" as GameJS

                  Item{
                  id: self;
                  function refresh(){
                  GameJS.innerTraversal(self,GameJS.applyRefresh);
                  }

                  function reset(){
                      GameJS.innerTraversalWithReset(self,GameJS.applyReset);
                  }
                  

                  }
                  @

                  assume this in the main.qml

                  @
                  import QtQuick 2.2

                  ApplicationWindow {
                  visible: true
                  width: 1024
                  height: 600
                  title: qsTr("Hello World")
                  color: "#FF000000"

                  Game{
                      id: gameId;
                     anchors.fill : parent;
                  
                     Rectangle{
                        id: clickToRestRectId;
                        color:"blue"
                        width: 100; height:100
                  
                        MouseArea{
                           anchors.fill:parent;
                           onClicked:{
                                 gameId.reset();
                           }
                        }
                     }
                  
                     Rectangle{
                  
                         function reset(){
                              console.log("this should work");
                         }
                     }
                  }
                  

                  }
                  @

                  1 Reply Last reply
                  0
                  • shavS Offline
                    shavS Offline
                    shav
                    wrote on last edited by
                    #10

                    I've check your code and all works on my Mac OS 10.10 with Qt 5.4. The code which I used:
                    @
                    import QtQuick 2.4
                    import QtQuick.Controls 1.3
                    import QtQuick.Window 2.2
                    import QtQuick.Dialogs 1.2
                    import "GameHelperJS.js" as GameJS

                    ApplicationWindow {
                    title: qsTr("Hello World")
                    width: 640
                    height: 480
                    visible: true

                    menuBar: MenuBar {
                        Menu {
                            title: qsTr("&File")
                            MenuItem {
                                text: qsTr("&Open")
                                onTriggered: {
                                    dialog1.show();
                                }
                            }
                            MenuItem {
                                text: qsTr("E&xit")
                                onTriggered: Qt.quit();
                            }
                        }
                    }
                    
                    Item{
                       id: self;
                    
                       function refresh(){
                            GameJS.innerTraversal(self,GameJS.applyRefresh);
                        }
                    
                        function reset(){
                            GameJS.innerTraversalWithReset(self,GameJS.applyReset);
                        }
                    }
                    
                    Timer {
                        id: timer
                        running: true
                        repeat: true
                        interval: 1000
                    
                        property bool flag: false
                    
                        onTriggered: {
                            if(flag === false)
                            {
                                flag = true;
                                self.refresh();
                            }
                            else
                            {
                                flag = false;
                                self.reset();
                            }
                        }
                    }
                    

                    }
                    @

                    I've use timer to call function with period. In console log I've got this:
                    @
                    Starting /Volumes/Documents/MyWork/PROJECTS/QtProjects/Local/build-sddd-Desktop_Qt_5_4_0_clang_64bit-Debug/sddd.app/Contents/MacOS/sddd...
                    QML debugging is enabled. Only use this in a safe environment.
                    qml: function
                    qrc:/main.qml:83: TypeError: Property 'innerTraversalWithReset' of object [object Object] is not a function
                    qml: function
                    qrc:/main.qml:83: TypeError: Property 'innerTraversalWithReset' of object [object Object] is not a function
                    qml: function
                    qrc:/main.qml:83: TypeError: Property 'innerTraversalWithReset' of object [object Object] is not a function
                    qml: function
                    qrc:/main.qml:83: TypeError: Property 'innerTraversalWithReset' of object [object Object] is not a function
                    qml: function
                    qrc:/main.qml:83: TypeError: Property 'innerTraversalWithReset' of object [object Object] is not a function
                    qml: function
                    qrc:/main.qml:83: TypeError: Property 'innerTraversalWithReset' of object [object Object] is not a function
                    @

                    As you can see first function (with name innerTraversal) is fire correct and you could see the log message like 'function'.

                    Also I've checked this code on my virtual machine with Windows 7 and all works fine.

                    P.S. The second function is not define in your JS.

                    Mac OS and iOS Developer

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      laithbasildotnetgmail.com
                      wrote on last edited by
                      #11

                      Thank you, innerTraversalWithReset is just typing error,
                      anyway it still not work for me, I will remove and install Qt again with hope its will works. I tested this on QML Creator app on android and it does work perfect.

                      which mean my Qt edition has a big problem for some reason.

                      thank you man, I really appreciate your time and help .

                      have a nice day !

                      1 Reply Last reply
                      0
                      • shavS Offline
                        shavS Offline
                        shav
                        wrote on last edited by
                        #12

                        I glad to help you with your problem. If your problem was fix please mark thread as SOLVED.

                        Mac OS and iOS Developer

                        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