Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Return value from QObject in another thread via signals/slots
Forum Update on Monday, May 27th 2025

Return value from QObject in another thread via signals/slots

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 4 Posters 1.3k Views
  • 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.
  • SGaistS SGaist

    Hi,

    Do you mean get the result in the returned value of the function call ?

    S Offline
    S Offline
    St.Stanislav
    wrote on last edited by St.Stanislav
    #4

    @SGaist Yes, exactly. So in example I suppose that ProcessingObject just multiplies argument by 2. So I want to send signal

    void processingRequested(float value = 2);
    

    To slot

    float multiply(float value) {
        return value * 2;
    }
    

    and receive last function's returning value.

    1 Reply Last reply
    0
    • jsulmJ jsulm

      @St-Stanislav said in Return value from QObject in another thread via signals/slots:

      but what is the simplest and correct way to receive result in sender?

      In the same way you send data?
      ProcessingObject would emit a signal when it has finished its work and send the result as parameter in a signal.

      S Offline
      S Offline
      St.Stanislav
      wrote on last edited by
      #5

      @jsulm Yes, I thought about this approach, but what if I want to process something twice? In example above I want to process numbers 2 and 4. In this case ProcessingObject will emit the same signal twice but with the different arguments. So I need to add some queue of values that I'm processing. Then I need some identifiers for prevent mixing up results. It's not very complicated, but I hope that more simple built-in approach is existed.

      JonBJ 1 Reply Last reply
      0
      • S St.Stanislav

        @jsulm Yes, I thought about this approach, but what if I want to process something twice? In example above I want to process numbers 2 and 4. In this case ProcessingObject will emit the same signal twice but with the different arguments. So I need to add some queue of values that I'm processing. Then I need some identifiers for prevent mixing up results. It's not very complicated, but I hope that more simple built-in approach is existed.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #6

        @St-Stanislav
        Hang on! IMHO at least, slots should not return a value. The assumption you are making is that there will only be one slot attached to the signal, and that is unwarranted/not guaranteed at all. Strange things happen when multiple slots are attached and you rely on the return result.

        I agree it can be a "shame" that you cannot use signals/slots to get a return value, but that's not what they are for. Not to mention, it completely breaks if the connection is queued, for whatever reason.

        I wouldn't advise it, but a better way (to allow for multiple slots) would be to pass a reference parameter for the "result".

        S 1 Reply Last reply
        1
        • JonBJ JonB

          @St-Stanislav
          Hang on! IMHO at least, slots should not return a value. The assumption you are making is that there will only be one slot attached to the signal, and that is unwarranted/not guaranteed at all. Strange things happen when multiple slots are attached and you rely on the return result.

          I agree it can be a "shame" that you cannot use signals/slots to get a return value, but that's not what they are for. Not to mention, it completely breaks if the connection is queued, for whatever reason.

          I wouldn't advise it, but a better way (to allow for multiple slots) would be to pass a reference parameter for the "result".

          S Offline
          S Offline
          St.Stanislav
          wrote on last edited by
          #7

          @JonB Thank you for reply! Yes, I understand signas/slots nature and its purpose, so that's why I try to find correct workaround :) So if I understand correctly, you assume something like this:

          signals:
          void processingRequested(float& result, float value);
          
          slots:
          void multiply(float &result, float value) {
              result = value * 2;
          }
          

          Am I right? So how sender will know, when result will be changed by receiver?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #8

            Can you describe what exactly you are trying to achieve ?

            Understanding what you want to do will help us with the design.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            S 1 Reply Last reply
            0
            • SGaistS SGaist

              Can you describe what exactly you are trying to achieve ?

              Understanding what you want to do will help us with the design.

              S Offline
              S Offline
              St.Stanislav
              wrote on last edited by St.Stanislav
              #9

              @SGaist So I have program that displays data from external device. I have two threads (with appropriate objects): one is for data processing and second is for interface and displayed data holding. If in processing object one parameter is changed, I need to replot data from the displaying object based on that parameter. This parameter is something like calibration for X-axis and object has needed methods. So in my suggest I need to send X-coordinate from data in the displaying object to the processing object, compute new result there and apply it in displaying object with replacing the previous one. I hope I explained it clearly enough, anyway I've tried :)

              Hm, so maybe I can emit signal that parameter was changed, send it to displaying object and apply it there. I need implement calibration method in displaying object as well, so it will be implemented twice. Don't know, is it good way, but it should be as workaround.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #10

                The propagation of the change through signal and slots is the correct way to do it.

                Why would you need to implement things twice ? You should have one object responsible for these mathematical operations rather than duplicate them in several places.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                S 1 Reply Last reply
                0
                • SGaistS SGaist

                  The propagation of the change through signal and slots is the correct way to do it.

                  Why would you need to implement things twice ? You should have one object responsible for these mathematical operations rather than duplicate them in several places.

                  S Offline
                  S Offline
                  St.Stanislav
                  wrote on last edited by
                  #11

                  @SGaist Yes, and math operations are provided by Processing object: I read raw data from serial port, send raw array to Processing object and Processing Object resend them further to display:
                  SerialPort => Processing (math) => Display
                  And everything is done with signals/slots: Serial port emits data, Processing object catch this data, process it and emits processed data, Display catch it and display. But sometimes I need to change already displayed data with the same math processing:
                  Display => Processing (math) => Display
                  So that's why I'm not sure, how to do it correctly :)

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    Do you have an example of such a case ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    S 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Do you have an example of such a case ?

                      S Offline
                      S Offline
                      St.Stanislav
                      wrote on last edited by
                      #13

                      @SGaist Sorry for my late reply. What kind of examples do you mean? Which code I use for processing and emitting or just an abstract description of what emit and catch in slots?

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #14

                        An example of what could trigger that double evaluation that is giving you trouble.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        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