yaodeng cai yaodeng cai - 8 months ago 45
C++ Question

Is there any good way to quickly trigger a signal slot in Qt?

In Qt multiple threads use 'emit' to send a large number of signals, the trigger is very slow. Seemingly there is a queuing mechanism. Is there any good way to quickly trigger a signal slot on the line?


What you are asking about is called Qt::DirectConnection. You need to specify it in connect() to guarantee slot will be invoked immediately.

When it is used:

The slot is invoked immediately when the signal is emitted. The slot is executed in the signalling thread.

Otherwise, the default Qt way is called Qt::AutoConnection.

If the receiver lives in the thread that emits the signal, Qt::DirectConnection is used. Otherwise, Qt::QueuedConnection is used. The connection type is determined when the signal is emitted.

You can specify connection type in connect():

QMetaObject::Connection QObject::connect(const QObject *sender, const    
   char *signal, const QObject *receiver, const char *method,    
   Qt::ConnectionType type = Qt::AutoConnection)

Very good answer about the difference between these two and how to use is here.