function as variable?



  • Is there a way to define a function as a variable in pure QML?
    Something like

    property function saySomething: void
    property string animalType: "duck"
    
    Component.onCompleted:
    {
    if (animalType== "duck") saySomething = sayQuack
    if (animalType== "snake") saySomething = sayHiss
    
    saySomething()//outputs "quack"
    }
    
    function sayQuack() { console.log("quack") }
    
    function sayHiss() { console.log("hiss") }
    


  • @JeTSpice Hi friend. Welcome.

    Yes, it is. Just use jsvascript pointer of function.

    like below snippet:

    Window {
        id:id_win
        property var f;
    
        Component.onCompleted: {
            function dog(){
                console.log("dog");
            }
            function cup(){
                console.log("cup");
            }
    
            if(false){
                id_win.f = dog;
            }else{
                id_win.f = cup;
            }
            id_win.f();
        }
    }
    
    


  • Perfect. Thank you, joeQ.


Log in to reply
 

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