Reference arguments in signal



  • Hi,

    I'm looking for a way to get back response from a signal in its parameters. Ex :

    // MyComponent.qml
    Rectangle {
       signal closeClick(cancel)
       width : 50; height : 50
       MouseArea {
          anchors.fill : parent
          onClicked : {
             var cancel = false;
             closeClick(cancel);
             if (cancel == true) {
                 console.log("close event has been cancelled !");
                 // cancel is always false !
             }
          }
       }
    }
     
    // Other QML file
    MyComponent {
       onCloseClick : {
          cancel = true; // I set cancel to true here, the breakpoint is reach
       }
    }
    

    I'm struggling with this since yesterday. The only way I found would be to set a bool property to MyComponent and set it to false in the onCloseClick.

    // MyComponent.qml
    Rectangle {
       id : myComponent
       property bool cancel : false
       signal closeClick()
       width : 50; height : 50
       MouseArea {
          anchors.fill : parent
          onClicked : {
             closeClick();
             if (myComponent.cancel == true) {
                 console.log("close event has been cancelled !");
             }
          }
       }
    }
    
    // Other QML file
    MyComponent {
       onCloseClick : {
           cancel = true;
       }
    }
    

    So my question, there is not way to get back data from signal ?



  • I would say that's because you're passing a primitive type (bool). In this case, it is passed by value. To have it passed by reference, you should try passing an object instead.

       signal closeClick(options);
       ...
             var options = {"cancel": false};
             closeClick(options);
             if (options.cancel == true) {
       ...
    
    MyComponent {
       onCloseClick : {
          options.cancel = true;
       }
    }


  • @Leonardo

    When I read the Qt do on QML signal I thought having read that we have to give a type to each parameters of a signal ? Giving no parameter means that we are passing an object?

    So using my first example without specifying the data type of "cancel" parameter should work?

    If Yes it could be better explain on Qt documentation, or maybe there something I don't understand.



  • Hi. You're right. There has to be a type. For objects, you should use "var".

    signal closeClick(var options);
    

    It is an object not because of the parameter type, but because of its content.

    var options = {"cancel": false};

Log in to reply
 

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