Change the order of connection of the signal slot

Recently, I ran into a problem with my code, which was caused by certain types of behavior, depending on the order of connection of the signal slot in a specific object. This is a design flaw on my behalf (communications have always been dynamic, so this flaw was inevitable), but it made me think.

Is it possible to reorder the signal slot connections in an object? And / or indicate the "index" of the connection when creating one?

I appreciate that you can simulate this effect by destroying all the compounds and re-creating them in a new order, but this is not what I ask. I could not find anything in the API or general docs, so I suspect the answer is no, but I thought I should ask anyway ...

+7
source share
2 answers

Unlike what seemed like a past understanding , I was surprised to read the update, which (at least in recent versions) Qt did not leave an order the slot is called undefined:

If several slots are connected to the same signal, the slots will be executed one after another in the order in which they were connected when the signal is emitted.

http://doc.qt.io/qt-4.8/signalsandslots.html#signals

(Although it can be argued that one sentence in this document is not enough to provide a β€œstrong” guarantee for all versions of Qt 4.X past and future.)

There is no API for reordering signals and slots. Even if that were the case, I would feel that relying on an order is not a good idea. I would suggest revising the design.

One thing you could investigate was to make sure that your slots queue their actions, rather than take actions directly. Then, when all the slots were called, you processed this queue ... taking into account some priority attributes.

+10
source

QObject internally saves joins as a list. Simple use of the <private/qobject_p.h> to obtain a lock on the sender's connection list and change the order of its entries. An open API will capture this detail throughout each major Qt revision, and this has been seen as too restrictive.

0
source

All Articles