PDA

View Full Version : QEventLoop in QApplication



Qiieha
22nd May 2012, 17:47
Hi,

A Thread emits a signal and the slot is in the mainthread. The slot's widget has a QEventLoop as Member. Is it ok to call loop.exec() in the slot.
If I don't call this, the slot returns and the Thread continue, because of BlockingQueuedConnection. But the user should have some time to decide something.

If the user has decided the I call loop.quit();

It seems to work, but is this ok. What are the cons?

thank u

amleto
22nd May 2012, 19:08
The short answer is there is no need to use eventloop just to make a 'false' block in your code. You could just use signals and slots and refactor your thread code. What you have done is block a thread from potentially doing more work. If that thread *truly* can't do any more work, then I don't think it is doing any harm.


thread code (what you have):


void thread::do_something()
{

do_x();

emit some_signal(); // this is your blocking queued connection

do_some_more_stuff();
}


to this:


void thread::do_something_part1()
{
do_x();
emit some_signal();
}

void thread::do_something_part2() // this is a slot. connect it to a signal that is sent when user has 'decided something'
{
do_some_more_stuff();
}