connect( &objTwo, &Two::emitThisSignal, this, &Controller::mySlot );
Here Two is a separate class. Its object has been created in Controller class. Its signal has to be connected to Controller class's slot.
Signature of connect is:
connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
connect(const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const
connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
connect(const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)
Which signature out of above does my connect call represent?
Why can't I simply write: connect( &objTwo, objTwo.emitThisSignal, this, this->mySlot );
?
Which signature out of above does my connect call represent?
This one:
connect(const QObject *sender, PointerToMemberFunction signal,
const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
The last argument is default(Qt::ConnectionType type = Qt::AutoConnection
), so you don't have explicitly specify it. Link to docs
Why can't I simply write:
connect( &objTwo, objTwo.emitThisSignal, this, this->mySlot );?
Because that is invalid C++ syntax if you want to pass a pointer to member function. objTwo.emitThisSignal
in C++ syntax would mean, access the data member emitThisSignal
inside objTwo
, while the function expects a pointer to member function.
However if you write:
connect( &objTwo, SIGNAL(objTwo.emitThisSignal), this, SLOT(this->mySlot) );
Your code will probably compile but would not work as intended. The reason for this can be understood by look at the first connect
signature:
connect(const QObject *sender, const char *signal,
const QObject *receiver, const char *method, Qt::ConnectionType type)
It takes pointer to char*
as 2nd and 4th arguments. So any syntax would get compiled. The macros SIGNAL()
and SLOT()
convert these into strings and then Qt uses these at runtime to call the right slot(s) when a signal is emitted. The above syntax will likely not even result in a crash or any warnings even. I know this sounds apalling, but such was the state of Qt before Qt 5 came.
These days it is highly recommended that you use these three signatures:
connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
connect(const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)