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



  • I have QML code, for example this code

    "http://stackoverflow.com/questions/27945427/qml-how-to-pass-javascript-function-as-argument-in-another-function":http://stackoverflow.com/questions/27945427/qml-how-to-pass-javascript-function-as-argument-in-another-function

    in normal javascript this works cool, but when I use this code in QML the bad thing is visitFun always has type of undefined, and it does not work..

    any idea how to make this work ?



  • 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");
    });
    }
    @



  • 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



  • hm....

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



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



  • 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.



  • 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



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



  • 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");
           }
       }
    }
    

    }
    @



  • 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.



  • 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 !



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.