[Interest] Does cross thread signal/slot connection order matter?

Murphy, Sean smurphy at walbro.com
Wed Jun 11 16:00:59 CEST 2014


I've got an object that I want to live in another thread, so in a class in my main thread I've got:

//  pseudo-code, may not compile
QThread* thread = new QThread(this);
myObject* foo =  new myObject();
foo ->moveToThread(thread);
thread->start();

But when I go to connect the signals/slots between objects in the main thread and the "foo" object, should I do that before I move that object to a different thread, or after, or does it not matter?

So should it be either:
QThread* thread = new QThread(this);
myObject* foo =  new myObject();
// make calls to connect() before calling moveToThread()
connect(this, SIGNAL(...),foo, SLOT(...)); // repeat as necessary for all my signal/slot connections
foo->moveToThread(thread);
thread->start();

Or:
QThread* thread = new QThread(this);
myObject* foo =  new myObject();
foo->moveToThread(thread);
thread->start();
// make calls to connect() after calling moveToThread()
connect(this, SIGNAL(...),foo, SLOT(...));// repeat as necessary for all my signal/slot connections

Or even delaying the thread->start() call until after everything else:
QThread* thread = new QThread(this);
myObject* foo =  new myObject();
foo->moveToThread(thread);
// make calls to connect() after calling moveToThread() then call thread->start()
connect(this, SIGNAL(...),foo, SLOT(...));// repeat as necessary for all my signal/slot connections
thread->start();

I can't find any authoritative documentation that says to do it one way or the other, so either it doesn't exist or more likely, I'm not googling well.  I can find lots of docs on the merits of inheriting from QThread vs. using the moveToThread(), but the ones I've found don't address this question in detail.

Sean

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140611/29737629/attachment.html>


More information about the Interest mailing list