c++ - QT signal slot is not working -
i facing strange problem while trying execute following program
main.cpp
#include "sample.h" #include <qlist> #include <unistd.h> int main(int argc, char **argv) { a; a.calla(); while(1) sleep(1); return 0; }
sample.cpp
#include "sample.h" #include <qlist> #include <qmetamethod> #include <unistd.h> thread::thread(a *a) { } void thread::run() { int = 5; while (i){ qdebug()<< i; sleep(2); i--; } emit processingdone(">>> thread"); qdebug()<<"emited signal thread"; } void a::calla() { qdebug()<<"from calla"; //movetothread(thread); thread->start(); //thread->run(); //emit signala(">>> calla"); } void a::slota(qstring arg) { qdebug()<<"from sloata "<< arg; }
sample.h
class a; class thread : public qthread { q_object public: thread(a *a); ~thread(){ qdebug()<<"thread destroyed"; } void run(); signals: void processingdone(qstring arg); }; class : public qobject{ q_object public: a(){ qdebug()<<"registering"; thread = new thread(this); connect(thread, signal(processingdone(qstring)), this, slot(slota(qstring))); connect(this,signal(signala(qstring)), this, slot(slota(qstring))); } public slots: void calla(); void slota(qstring arg); signals: void signala(qstring arg); private: thread *thread; };
when try executed program, slot not getting invoked? if put movetothraed(), code work won't serve puprose. missing?
you not starting main event loop.
your main function should this:
qapplication app(argc, argv); a; a.calla(); return app.exec();
qt queued connections can't work if there no event loop running in receiving thread.
when receiver object lives in thread other thread signal emitted, qt::autoconnection
uses qt::queuedconnection
, see docs.
a qt::queuedconnection
works posting event event loop of target thread (the thread receiver qobject
lives). when event processed (by event loop) call slot invoked in target thread.
in case, main thread stuck in:
while(1) sleep(1);
so, never able execute else.
Comments
Post a Comment