Important: Please read the 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)
    int main(int argc, char argv[])
    QCoreApplication a(argc, argv);
    int i=0;
    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?

  • Lifetime Qt Champion

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

  • Lifetime Qt Champion

    @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