Change signal-slot connection order
Asked Answered
H

2

7

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...

Historicity answered 24/12, 2011 at 14:4 Comment(0)
B
10

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.

Bedplate answered 24/12, 2011 at 15:9 Comment(2)
Agreed. Like I noted in my question, I recognise that it is design flaw, but API developers should give us everything we need to get out of sticky situations whether it's classy or not. I would have thought that this would be an obvious one, but judging by the lack of even querying about it on the internet, shows I'm clearly wrong there!Historicity
Just as there are two kinds of political freedoms ("freedom to" and "freedom from") there's two kinds of powerfulness in API design. In this case I think it's fine to not offer an ability to reorder the signals, it just seems incongruous with an assertion that there's a contractual order. shrug hostilefork.com/2005/07/04/freedom-to-and-freedom-fromBedplate
T
0

QObject internally stores the connections as a list. It's a simple matter to use the <private/qobject_p.h> header to acquire a lock on the sender's connection list and reorder its entries. A public API would make this detail fixed for the duration of each major Qt revision, and this was seen as too limiting.

Telegraphic answered 13/6, 2017 at 13:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.