Important: Please read the Qt Code of Conduct -

Return value from QObject in another thread via signals/slots

  • 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?

  • Lifetime Qt Champion


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

  • Lifetime Qt Champion

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

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

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

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

  • @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:

    void processingRequested(float& result, float value);
    void multiply(float &result, float value) {
        result = value * 2;

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

  • Lifetime Qt Champion

    Can you describe what exactly you are trying to achieve ?

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

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

  • Lifetime Qt Champion

    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.

  • @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 :)

  • Lifetime Qt Champion

    Do you have an example of such a case ?

  • @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?

  • Lifetime Qt Champion

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

Log in to reply