Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Return value from QObject in another thread via signals/slots

    General and Desktop
    4
    14
    624
    Loading More Posts
    • 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.
    • S
      St.Stanislav last edited by

      Hello!

      I have faced with the following situation: I have ProcessingObject in the one thread and its interface ProcessingObjectInterface in another. ProcessingObject has some inner structure and process data. I want to send data from ProcessingObjectInterface to ProcessingObject, process it there and receive result in ProcessingObjectInterface.
      Sending data is simple, just using signas/slots mechanism, but what is the simplest and correct way to receive result in sender?

      jsulm 1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

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

        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 Reply Quote 0
        • jsulm
          jsulm Lifetime Qt Champion @St.Stanislav last edited by

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

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          S 1 Reply Last reply Reply Quote 1
          • S
            St.Stanislav @SGaist last edited by St.Stanislav

            @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 Reply Quote 0
            • S
              St.Stanislav @jsulm last edited by

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

              JonB 1 Reply Last reply Reply Quote 0
              • JonB
                JonB @St.Stanislav last edited by

                @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 Reply Quote 1
                • S
                  St.Stanislav @JonB last edited by

                  @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 Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    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 Reply Quote 0
                    • S
                      St.Stanislav @SGaist last edited by St.Stanislav

                      @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 Reply Quote 0
                      • SGaist
                        SGaist Lifetime Qt Champion last edited by

                        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 Reply Quote 0
                        • S
                          St.Stanislav @SGaist last edited by

                          @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 Reply Quote 0
                          • SGaist
                            SGaist Lifetime Qt Champion last edited by

                            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 Reply Quote 0
                            • S
                              St.Stanislav @SGaist last edited by

                              @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 Reply Quote 0
                              • SGaist
                                SGaist Lifetime Qt Champion last edited by

                                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 Reply Quote 0
                                • First post
                                  Last post