Take the 2-minute tour ×

I have following code:

class A : public QObject
    A() : QObject()

    void doSomething()
public slots:
    void doSomethingSlot()
         //do something
         emit ready();
    void ready();
    QThread t;

The question why from doSomething it must be call via QMetaObject::invokeMethod. I know that there is something with connection type. Could some one explain what is under the hood?

share|improve this question
ecatmur gave you perfect answer. You have another problem, having thread as a member of object which is moved to that thread is very bad idea, during destruction (when using deleteLater) you can have strange problems. –  Marek R Dec 19 '12 at 9:05
For complete explanation read this document –  Amit Tomar Dec 19 '12 at 9:06
@MarekR: what problems? I've get this solution from stackoverflow.com/questions/13878745/… –  krzych Dec 19 '12 at 9:09
delateLater invokes destructor in proper thread. When you are destroying this you will also will try to destroy thread. So code will keep thread alive and code will try to destroy the thread: it will hang. So this solution just corrupts functionality of delateLater. –  Marek R Dec 19 '12 at 9:21
Where is deleteLater used? –  krzych Dec 19 '12 at 9:36

2 Answers 2

up vote 8 down vote accepted

As you haven't specified a Qt::ConnectionType, the method will be invoked as Qt::AutoConnection, which means that it will be invoked synchronously (like a normal function call) if the object's thread affinity is to the current thread, and asynchronously otherwise. "Asynchronously" means that a QEvent is constructed and pushed onto the message queue, and will be processed when the event loop reaches it.

The reason to use QMetaObject::invokeMethod if the recipient object might be in another thread is that attempting to call a slot directly on an object in another thread can lead to corruption or worse if it accesses or modifies non-thread-safe data.

share|improve this answer
You mean that if I use A.doSomething() from another thread it may lead to corruption? Why so? Could you describe the mechanism also? –  krzych Dec 19 '12 at 9:06
@krzych for example, if you read from a QString at the same time another thread is modifying it then it may appear to be in an inconsistent state, leading to indirecting an invalid pointer. –  ecatmur Dec 19 '12 at 9:14

I like this trick:

void A:doSomethingSlot()
     if (thread()!=QThread::currentThread()) {
         QMetaObject::invokeMethod(this,"doSomethingSlot", Qt::QueuedConnection);
     // this is done always in same thread
     emit ready();
share|improve this answer
Wow. Thats very nice solution. –  krzych Dec 19 '12 at 9:40

Your Answer


