Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QObject based class has a queued connection to itself

I was digging into some source code I am working on. I found a peculiar statement that someone had coded. The source code is a GUI application with a QML GUI and uses QT 4.7.x.

The snippet below belongs to core application logic.

// connect signal-slots for decoupling
QObject::connect (this, SIGNAL(setCurrentTaskSignal(int)), this, 
    SLOT(SetCurrentTaskSlot(int)), Qt::QueuedConnection);

It's strange that the object connects to itself via a queued connection which essentially means that the object may "live" in different threads at the same time?

At first glance It didn't made any sense to me. Can anyone think of any reason why such a connection would be plausible or needed?. Would this even work?

like image 943
Vikas Bhargava Avatar asked Jun 27 '12 15:06

Vikas Bhargava


People also ask

What is queued connection in Qt?

Queued Connection The slot is invoked when control returns to the event loop of the receiver's thread. The slot is executed in the receiver's thread. Blocking Queued Connection The slot is invoked as for the Queued Connection, except the current thread blocks until the slot returns.

What is QObject class?

QObject is the heart of the Qt Object Model. The central feature in this model is a very powerful mechanism for seamless object communication called signals and slots. You can connect a signal to a slot with connect() and destroy the connection with disconnect().

What is QObject :: connect?

To connect the signal to the slot, we use QObject::connect(). There are several ways to connect signal and slots. The first is to use function pointers: connect(sender, &QObject::destroyed, this, &MyObject::objectDestroyed); There are several advantages to using QObject::connect() with function pointers.

How do I turn off Qt signal?

disconnect(& myConnect);


2 Answers

It will work without any problem. Maybe there was some event loop processing required before calling SetCurrentTaskSlot?

Note that QueuedConnection doesn't mean that something is in different thread. QueuedConnection means only that when signal is emitted, corresponding slot won't be called directly. It will be queued on event loop, and will be processed when control will be given back to event loop

like image 113
Kamil Klimek Avatar answered Sep 17 '22 22:09

Kamil Klimek


The queued connection implies nothing about where the receiver lives. The opposite is true: to safely send signals to an object living in another thread, you must use queued connections. But you can use them for an object living in any thread!

One uses a queued connection to ensure that the signal will be delivered from within the event loop, and not immediately from the emit site as happens with direct connection. Direct connection is conceptually a set of calls to function pointers on a list. Queued connection is conceptually an event sent to a clever receiver who can execute a function call based on the contents of the event.

The event is the internal QMetaCallEvent, and it is QObject::event that acts upon this event and executes the call.

like image 38
Kuba hasn't forgotten Monica Avatar answered Sep 20 '22 22:09

Kuba hasn't forgotten Monica