Qt: The return value of the signal works, why does the white paper say this is not possible / forbidden?

The Qt documentation says that returning signal values ​​is not possible:

Signals are automatically generated by moc and should not be implemented in a .cpp file. They can never have return types (i.e. use void).

Related SO questions:

  • Can Qt signals return a value?
  • Qt: slot return value?

However, from my tests (Qt 4.8.1), I can say that the return values ​​do work:

  • If the signal / slot is on the same thread, ConnectionType can be Qt::AutoConnection
  • With a signal / slot in different threads, I need to use Qt::BlockingQueuedConnection

So in my code I call a signal

 QString dp = emit WscAircrafts::signalAircraftsJsonArray(); 

and the moc signal returns a QString ,

 QString _t0; void *_a[] = { const_cast<void*>(reinterpret_cast<const void*>(&_t0)) }; QMetaObject::activate(this, &staticMetaObject, 0, _a); return _t0; 

Here is the moc slot in which it returns a QString

 case 4: { QString _r = _t->slotAircraftJsonArray(); if (_a[0]) *reinterpret_cast< QString*>(_a[0]) = _r; } break; 

All this seems pretty straightforward, so why is this a contradiction to the documentation? Where will the problem of using the return value be? As said, in my code this seems to work.

+8
qt qt4 qt-signals
source share
1 answer

The problem is that the types of the returned data are not checked for compatibility during the connection, so connecting a double-return slot to the swimming-returned signal, for example, will overflow the stack space (not intended for puns) allocated for the float.

+5
source share

All Articles