multithreading - Qt C++ - How to pass data from a worker thread to main thread? -
i trying perform interthread communication in qt (c++). have worker thread calculations , want workerthread return results main thread when done. therefor use connect, know debugging, signal being emit slot isn t being executed , don t understand why.
the relevant pieces of code:
webcamclass::webcamclass(qobject *parent) : qobject(parent) { workerthread = new qthread(this); workerclassobj = new workerclass(); //connect image connect(workerclassobj, signal(mysignal(qpixmap)), this, slot(myslot(qpixmap))); //connect(&workerclassobj, workerclass::mysignal(qpixmap), this, webcamclass::myslot(qpixmap)); connect( workerthread, signal(started()), workerclassobj, slot(getimage()) ); workerclassobj->movetothread(workerthread); } void webcamclass:: foo() { workerthread->start(); } void workerclass::getimage() { qint64 successfailwrite; qimage img; qpixmap pixmap; ... stuff pixmap... qdebug()<<"going emit result"; emit mysignal(pixmap); qdebug()<<"emitted"; } void webcamclass::myslot(qpixmap p) {qdebug()<<"this message should displayed"; }
the corresponding header files:
class workerclass : public qobject { q_object private: public: explicit workerclass(qobject *parent = nullptr); signals: void mysignal(qpixmap); }; webcamclass::webcamclass(qobject *parent) : qobject(parent) { q_object public: explicit webcamclass(qobject *parent = nullptr); public slots: void myslot(qpixmap p); private: qthread *workerthread; workerclass *workerclassobj; };
the code above outputs:
going emit result emitted
but unfortunately doesn t output this message should displayed
.
webcamclass belongs parent thread, while workerclass belngs -you guessed it- worker thread.
could explain how setup connect myslot() gets triggered?
thanks!
in code pasted in pastebin.com/uppfrnet have getvideoframe
method uses while (1)
. if method called, runs time , blocks event loop handling signals. can solve in many ways, think best practice replace while(1) else.
wiki
Comments
Post a Comment