Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: Return value of signal works, why is the official doc saying it is impossible / forbidden?

Tags:

qt

qt4

qt-signals

Qt documentation says, return values of signals are not possible:

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

Related SO questions:

  1. Can Qt signals return a value?
  2. Qt: meaning of slot return value?

However, from my trials (Qt 4.8.1) I can tell return values do work:

  1. If signal / slot are in the same thread, ConnectionType may be Qt::AutoConnection
  2. With signal / slot in different threads I need to use Qt::BlockingQueuedConnection

So in my code I call a signal by

QString dp = emit WscAircrafts::signalAircraftsJsonArray();

and the signal moc returns a QString,

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

This here is the slot moc where it passes back the QString

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

All of this seems to be pretty much straight forward, so why this contradiction with the documentation? Where would be the problem using the return value? As said, in my code this seems to work.

like image 320
Horst Walter Avatar asked Aug 09 '12 19:08

Horst Walter


People also ask

Can a Qt signal return a value?

All signals and slots have a "void" return value. You cannot return anything that way. This code assumes that there is only one slot that will be connected to the signal.

Can we connect signal to signal in Qt?

This ensures that truly independent components can be created with Qt. You can connect as many signals as you want to a single slot, and a signal can be connected to as many slots as you need. It is even possible to connect a signal directly to another signal.


1 Answers

The problem is that the return types are are not checked for compatibility at connect-time, thus connecting a double-returning slot to a float-returning signal, say, will overflow the stack (no pun intended) space allocated to the float.

like image 82
Marc Mutz - mmutz Avatar answered Sep 16 '22 23:09

Marc Mutz - mmutz