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. Reference arguments in signal
Forum Updated to NodeBB v4.3 + New Features

Reference arguments in signal

Scheduled Pinned Locked Moved QML and Qt Quick
signalreferenc
4 Posts 2 Posters 2.3k Views 2 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.
  • E Offline
    E Offline
    Erakis
    wrote on last edited by
    #1

    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 ?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      Leonardo
      wrote on last edited by
      #2

      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;
         }
      }
      
      E 1 Reply Last reply
      0
      • L Leonardo

        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;
           }
        }
        
        E Offline
        E Offline
        Erakis
        wrote on last edited by
        #3

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

        L 1 Reply Last reply
        0
        • E Erakis

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

          L Offline
          L Offline
          Leonardo
          wrote on last edited by
          #4

          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};
          
          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved