Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to receive other thread's return value in main thread



  • testthread::testthread(worker* w, QObject parent):QObject(parent),result(0)
    {
    worker
    _w=w;
    _w->moveToThread(&workerThread);
    connect(&workerThread,SIGNAL(started()),_w,SLOT(dowork()));
    connect(&workerThread,SIGNAL(finished()),_w,SLOT(deleteLater()));
    connect(_w,SIGNAL(finished()),&workerThread,SLOT(deleteLater()));
    connect(_w,SIGNAL(finished()),&workerThread,SLOT(quit()));
    connect(_w,SIGNAL(resultReady(int)),this,SLOT(handleResult(int)));
    workerThread.start();
    }
    int main(int argc, char argv[])
    {
    QCoreApplication a(argc, argv);
    int i=0;
    realworker
    w=new realworker(i);
    testthread t(w,nullptr);
    t.thread()->wait(); //testthread has Qthread member value.
    return a.exec();
    }

    I want to receive result value after handleResult() call in main()
    So I used wait() in main() , but it prints as below:

    QThread::wait: Thread tried to wait on itself

    How can I get the thread return value?


  • Qt Champions 2019

    @name101 What do you mean by "return value"? Doesn't your worker already return the result as parameter in resultReady(int) signal?



  • I want to wait for resultReady(int) to complete and then get it from main thread like C's WaitForSingleObject function



  • As "testthread" is a class and you have an object of this class, you can define another signal from "testthread" class and connect it to a main thread slot.


  • Qt Champions 2019

    @name101 Why do you want to wait for it? Just connect a slot to it and do what you need to do when the signal is emitted.


Log in to reply