PDA

View Full Version : how to emit from thread



saman_artorious
20th April 2013, 14:52
emits included in customized thread inside my program does not work. it seems my thread has no communication with the main UI thread. in c# we have thread dispatcher to handle this. what about QT?



void myThread::run(){

while(1)
{
//do something
emit display("show the result"); //NOT WORKING, THOUGH CONNECTED
}
}

Lesiok
20th April 2013, 15:24
Qt signals operate between threads. Show some code.

Santosh Reddy
20th April 2013, 16:46
does this work?


void myThread::run(){

while(1)
{
//do something
emit display("show the result"); //NOT WORKING, THOUGH CONNECTED
msleep(1);
}
}

Lesiok
20th April 2013, 16:57
In standard multithread connection signal and slot are connected in Qt::QueuedConnection mode. To work this it must be working event loop in thread. In your case there is no working event loop. Try change code to something like this :

void myThread::run()
{
QTimer::singleShot( 0,this,SLOT(doSomethingOne()));
QThread::run();
}

void myThread::doSomethingOne()
{
//do something
emit display("show the result"); //NOW IS WORKING :)
QTimer::singleShot( 0,this,SLOT(doSomethingOne()));
}
Standard QThread::run method starts event loop.

Santosh Reddy
20th April 2013, 17:17
In standard multithread connection signal and slot are connected in Qt::QueuedConnection mode. To work this it must be working event loop in thread. In your case there is no working event loop.
Event loop is required only to execute the slot connected in Qt::QueuedConnection type, event loop is not required for the object to emit signal. (not even in different thread)