QMetaObject :: invokeMethod returns true, but the method is never called

I am trying to run a method in a GUI thread using QMetaObject :: invokeMethod, which returns true. But if I use Qt :: QueuedConnection, my method will never be called (even if invokeMethod returns true).

This is what I use:

QMetaObject::invokeMethod(this, "draw_widgets", Qt::QueuedConnection)

I have no error messages or anything else ... If I use Qt :: AutoConnection or Qt :: DirectConnection, the method is actually called, but from the same stream, of course. Not from the GUI thread I need.

draw_widgets is a public slot like void draw_widgets () and my class inherits QObject and also uses the Q_OBJECT macro.

I would appreciate any help on this or how to check why the method is not being called.

Thanks.

+5
source share
1 answer

True tells you that the message has been successfully queued. This does not mean that the message in the queue has been processed ...

Say your program has 10 threads (Thread1-Thread10). You are sending a message to the queue from Thread7. What thread will be queued? And when will the items in this queue be processed?

The answer is that everyone QObjecthas something called Affinity , and this is the thread in which the queue will be executed in the queue. The default affinity refers to the thread in which the object was created (but you can change it with QObject::moveToThread().)

- GUI, , this, GUI. QObject::thread().

, , ... - , . , , QThread::exec(). , , , , exec.

( QMetaObject::invokeMethod . , .)

+10

All Articles