50 signal/slots
-
Hello. What will run faster, 50 signals connected to their own slot, or 1 signal reused 50 times with 1 slot? I prefer the first option because it is a lot less redundant code writing for me. I setup the 50 signals to emit their object name as a QString and call findChild on it to update the text on a label in the slot (it's for a hardware status page). As I understand it, an emit signal is just like calling the slot function directly.
-
it's equal, one signal emitted means one function call. the count of slots connected to one signal (or conversely) has no impact on performance
-
Hi @servant-0 , I think your scenario in practice will be the same as each signal contains a look up to its connected slot.
So each emit is only connected to one slot - i.e: each emit only iterates its own connection list (one slot).
The other way round is 50 signals each with their own slot incurs exactly the same per-signal overhead - i.e: one slot per emit. -
Awesome! Thank you so much @ankou29666 and @Ben-Campbell-Wallis. I will avoid redundant code and use the dynamic 1 slot/signal approach then.
-
S servant 0 has marked this topic as solved
-
Hello. What will run faster, 50 signals connected to their own slot, or 1 signal reused 50 times with 1 slot? I prefer the first option because it is a lot less redundant code writing for me. I setup the 50 signals to emit their object name as a QString and call findChild on it to update the text on a label in the slot (it's for a hardware status page). As I understand it, an emit signal is just like calling the slot function directly.
Usually both variants should be about the same time. You would have to benchmark it.
That said:
@servant-0 said in 50 signal/slots:I setup the 50 signals to emit their object name as a QString and call findChild on it to update the text on a label in the slot
findChild is incredibly slow, depending on your UI magnitudes slower than other parts of your code. I would recommend using it as little as possible and not regularly in slots
-
Hello. What will run faster, 50 signals connected to their own slot, or 1 signal reused 50 times with 1 slot? I prefer the first option because it is a lot less redundant code writing for me. I setup the 50 signals to emit their object name as a QString and call findChild on it to update the text on a label in the slot (it's for a hardware status page). As I understand it, an emit signal is just like calling the slot function directly.
@servant-0 said in 50 signal/slots:
I setup the 50 signals to emit their object name as a QString and call findChild on it to update the text on a label in the slot
All as @J.Hilk has written about
findChild()
. Also it depends hugely on whether your target widget is "close" in the hierarchy to the widget you callfindChild()
on. If that is "high up" it has a lot of searching to do if there are a lot of descendent widgets.It sounds like you have a backend data layer which emits a signal when it changes and passes the value of what will be used as a widget's
objectName()
to indicate which widget to update? If so that is not ideal for decoupling data from UI.You might like to look at QDataWidgetMapper for a means of tying widgets to data values (works in both directions, though you may only need data->widget direction). For that you would store your data values in anything derived from a
QAbstractItemModel
and any time that data changes internally a signal is sent and the corresponding widget is updated (without lookup).