3
votes

I have several questions about signals and slots. I am using Qt 4.8.

  1. Do signals and slots have to be void functions? A signal is of course no function, but it is declared like a normal member function.

  2. I often use call-by-const-reference to avoid creating too many copies of objects. Can I use call-by-const-reference with signals and slots? The object which was sent could go out of scope after the emission of the signal. Or does the signal store a copy of what was sent? Then call-by-const-reference would be the same as call-by-value.

  3. What if the slot manipulates the object it got via call-by-reference from the sender? Does the sender notice this?

  4. It is possible to force the signal to wait until the connected slots have finished? How do I do that?

1
is this Qt4 "traditional" signals or qt5 signals ?UmNyobe
I use Qt 4.8, but I would be thankful if you could also explain the differences made between Qt4 and Qt5.Fabian
the differences betwen qt4 and qt5 is another topic, see doc.qt.io/qt-5/signalsandslots-syntaxes.htmlUmNyobe
"It is possible to force the signal to wait until the connected slots have finished? How do I do that?” That’s the default for signal/slot connections inside the same thread (DirectConnection). A signal is just a function that calls all slots connected to it synchronously.Frank Osterfeld

1 Answers

2
votes

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.