@Pl45m4 said in When to use signals and slots, versus direct function calls, from parent to child?:
I've never seen this before in working code. Proof please ;-)
#ifndef SIGNALTEST_H
#define SIGNALTEST_H
#include <QObject>
class SignalTest : public QObject
{
Q_OBJECT
public:
explicit SignalTest(QObject *parent = nullptr);
public slots:
int intSlot() { return 123456; }
signals:
int intSignal();
};
#endif // SIGNALTEST_H
SignalTest *obj = new SignalTest;
QObject::connect(obj, &SignalTest::intSignal, obj, &SignalTest::intSlot, Qt::DirectConnection);
qDebug() << /*emit*/ obj->intSignal();
We do indeed get 123456 printed as return value from signal call, as per @GrecKo.
I do not think any of the (documented, at least) signals used by Qt have a return value, but it does work. Of course, if I change to Qt::QueuedConnection it does not work, I get 0 back.
Googling, even @SGaist wrote in https://forum.qt.io/post/596362
Signals have no return value.
And in the "other" forum someone wrote in https://www.qtcentre.org/threads/70910-How-to-get-the-return-value-when-signal-emit?p=307681#post307681
All signals and slots have a "void" return value. You cannot return anything that way.
But over at stackoverflow the accepted answer at https://stackoverflow.com/a/5903082/489865 shows same as mine but with a QString return and discusses it a bit further.
So it seems this is a little known "feature". I do not see the Qt docs saying anything about this, neither that signals should be void nor what happens if they return a value.