Qt Slot With Arguments
Signals and slots are loosely coupled: A class which emits a signal neither knows nor cares which slots receive the signal. Qt's signals and slots mechanism ensures that if you connect a signal to a slot, the slot will be called with the signal's parameters at the right time. Signals and slots can take any number of arguments of any type. If I’m about to modify a slot function I might take an extra minute to look around since most IDEs can’t tell syntactically where it’s used in a SLOT macro. In this case you have to search for it textually.) Thanks to C11 lambdas and Qt’s ongoing evolution, these short slots can be replaced by a more succinct syntax. Qt Signal Slot Arguments An effort has been made, using staticassert to get nice compile errors if the arguments do not match or of you miss a QOBJECT Alternatively, use your main character’s heal spell (gained very early on) to restore one person’s health. Dragon quest vi casino tricks.
How often is a an object copied, if it is emitted by a signal as a const reference and received by a slot as a const reference? How does the behaviour differ for direct and queued signal-slot connections? What changes if we emit the object by value or receive it by value?
Nearly every customer asks this question at some point in a project. The Qt documentation doesn’t say a word about it. There is a good discussion on stackoverflow, which unfortunately leaves it to the reader to pick the right answer from all the answers and comments. So, let’s have a systematic and detailed look at how arguments are passed to signals and slots.
Setting the Stage
For our experiments, we need a copyable class that we will pass by const reference or by value to signals and slots. The class – let’s call it Copy
– looks as follows.
The copy constructor and the assignment operator simply perform a member-wise copy – like the compiler generated versions would do. We implement them explicitly to set breakpoints or to print debugging messages. The default constructor is only required for queued connections. We’ll learn the reason later.
We need another class, MainView
, which ultimately derives from QObject
. MainView provides the following signals and slots.
MainView
provides four signal-slot connections for each connection type.
The above code is used for direct connections. For queued connections, we comment out the first line and uncomment the second and third line.
The code for emitting the signals looks as follows:
Direct Connections
sendConstRef => receiveConstRef
We best set breakpoints in the copy constructor and assignment operator of the Copy
class. If our program only calls emit sendConstRef(c)
, the breakpoints are not hit at all. So, no copies happen. Why?
The result is not really surprising, because this is exactly how passing arguments as const references in C++ works and because a direct signal-slot connection is nothing else but a chain of synchronous or direct C++ function calls.
Nevertheless, it is instructive to look at the chain of function calls executed when the sendConstRef
signal is emitted.
The meta-object code of steps 2, 3 and 4 – for marshalling the arguments of a signal, routing the emitted signal to the connected slots and de-marshalling the arguments for the slot, respectively – is written in such a way that no copying of the arguments occurs. This leaves us with two places, where copying of a Copy
object could potentially occur: when passing the Copy
object to the functions MainView::sendConstRef
or MainView::receiveConstRef
.
These two places are governed by standard C++ behaviour. Copying is not needed, because both functions take their arguments as const references. There are also no life-time issues for the Copy
object, because receiveConstRef
returns before the Copy
object goes out of scope at the end of sendConstRef
.
sendConstRef => receiveValue
Based on the detailed analysis in the last section, we can easily figure out that only one copy is needed in this scenario. When qt_static_meta_call
calls receiveValue(Copy c)
in step 4, the original Copy
object is passed by value and hence must be copied.
sendValue => receiveConstRef
One copy happens, when the Copy
object is passed by value to sendValue
by value.
sendValue => receiveValue
This is the worst case. Two copies happen, one when the Copy
object is passed to sendValue
by value and another one when the Copy
object is passed to receiveValue
by value.
Queued Connections
Qt Virtual Slot
A queued signal-slot connection is nothing else but an asynchronous function call. Conceptually, the routing function QMetaObject::activate
does not call the slot directly any more, but creates a command object from the slot and its arguments and inserts this command object into the event queue. When it is the command object’s turn, the dispatcher of the event loop will remove the command object from the queue and execute it by calling the slot.
When QMetaObject::activate
creates the command object, it stores a copy of the Copy
object in the command object. Therefore, we have one extra copy for every signal-slot combination.
We must register the Copy
class with Qt’s meta-object system with the command qRegisterMetaType('Copy');
in order to make the routing of QMetaObject::activate
work. Any meta type is required to have a public default constructor, copy constructor and destructor. That’s why Copy
has a default constructor.
Queued connections do not only work for situations where the sender of the signal and the receiver of the signal are in the same thread, but also when the sender and receiver are in different threads. Even in a multi-threaded scenario, we should pass arguments to signals and slots by const reference to avoid unnecessary copying of the arguments. Qt makes sure that the arguments are copied before they cross any thread boundaries.
Conclusion
Qt Private Slots
The following table summarises our results. The first line, for example, reads as follows: If the program passes the argument by const reference to the signal and also by const reference to the slot, there are no copies for a direct connection and one copy for a queued connection.
Qt Slot With Arguments Definition
Signal | Slot | Direct | Queued |
---|---|---|---|
const Copy& | const Copy& | 0 | 1 |
const Copy& | Copy | 1 | 2 |
Copy | const Copy& | 1 | 2 |
Copy | Copy | 2 | 3 |
Qt Slot With Multiple Arguments
The conclusion from the above results is that we should pass arguments to signals and slots by const reference and not by value. This advice is true for both direct and queued connections. Even if the sender of the signal and the receiver of the slot are in different threads, we should still pass arguments by const reference. Qt takes care of copying the arguments, before they cross the thread boundaries – and everything is fine.
By the way, it doesn’t matter whether we specify the argument in a connect call as const Copy&
or Copy
. Qt normalises the type to Copy
any way. This normalisation does not imply, however, that arguments of signals and slots are always copied – no matter whether they are passed by const reference or by value.