First of all, I am talking about Qt4 connections (may also apply to Qt5)
Do signals and slots have to be void functions
No. Both signals and slots with return value will behave as if they were void if you used them normally. But there can be advanced usage for slots (using as normal function, binding, invoke method of metasystem) and signals(receive value from non void slots)
Can I use call-by-const-reference with signals and slots?
It means nothing for qt. But you still enforce that your method doesnt change its parameter. connections in Qt are designed such that the data is copied with some connection types, to the exception of Qt::DirectConnection. A call-by-const-reference signature should be treated by you the programmer exactly as a call by value signature in the traditional qt connections. This is because queued connections and connections between threads required objects to have a public default constructor, a public
copy constructor and a public destructor.
Qt address this issue using implicit data sharing. For instance, a QImage
is a lightweight object which internally refer to the real pixel data, which size can be arbitrarily large.
What if the slot manipulates the object it got via call-by-reference
from the sender? Does the sender notice this
As said above call by reference will pass a copy of the object. The object is copied first outside the function, then passed as reference. But if you pass an object which share data with its copies (eg shared pointers, ), any modification to the object may affect all copies. As peppe pointed out in the comment, most of the time Qt associated implicit sharing with copying on write. It means in the case of a QImage, the modification will not affect others copy.
It is possible to force the signal to wait until the connected slots
have finished? How do I do that?
Uses the connection type Qt::DirectConnection
if both senders and receivers are in the same threads, otherwise Qt::BlockingQueuedConnection
.
As said above, a const reference signature means the Qt::DirectConnection will not copy data while Qt::BlockingQueuedConnection will copy the data.