Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change signal-slot connection order

I recently came across a problem with my code that was caused by certain behaviours being dependent upon the signal-slot connection order in a particular object. This is a design flaw on my behalf (the connections were always meant to be dynamic so this flaw was inevitable), but it got me thinking.

Is it possible to re-order the signal-slot connections in an object? And/or specify the "index" of a connection when creating one?

I appreciate that you can simulate this effect by destroying all the connections and recreating them in a new order, but that is not what I am asking. 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...

like image 262
cmannett85 Avatar asked Dec 24 '11 14:12

cmannett85


1 Answers

Contrary to what seemed to be the past understanding, I'm surprised to read an update that (at least in recent versions) Qt has not left the order of slot calling as undefined:

If several slots are connected to one signal, the slots will be executed one after the other, in the order they have been connected, when the signal is emitted.

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

(Though one might argue that one sentence in that one document isn't enough to represent a "strong" guarantee for all Qt 4.X versions past and future.)

There does not appear to be an API for reordering the signals and slots. Even if there were, I'd feel relying on the order isn't a very good idea. I'd suggest rethinking the design.

One thing you might investigate would be to make it so that your slots would queue their actions instead of taking action directly. Then when all the slots had been called, you'd process that queue...taking some priority attributes into account.

like image 67
HostileFork says dont trust SE Avatar answered Nov 03 '22 01:11

HostileFork says dont trust SE