How to use new signal/slot syntax in Qt5.6?
-
Hi,
I have a console application, I want to connect a signal from an object to a function in main.cpp. According to new signal/slot syntax it is possible. There is no need to have an object in order to connect signals and slots. How can I do that?main.h
.... void dumpReceivedData(int type, QByteArray *msg);
main.cpp
int main() { ... Server server; QObject::connect(&server, Server::msgOut, dumpReceivedData); .... } void dumpReceivedData(int type, QByteArray *msg) { qDebug() << msg->constData(); }
server.h
... signals: static void msgOut(int type, QByteArray *msg);
server.cpp
void foo() { ... QByteArray *msg = new QByteArray; msg->append(aTcpSocket->readAll()); emit msgOut(1, msg); }
I am getting compile error:
no matching function for call to QObject::connect(Server *, void(&)(int, QByteArray*), void(&)(int, QByteArray*))
Thanks.
-
Hi,
I have a console application, I want to connect a signal from an object to a function in main.cpp. According to new signal/slot syntax it is possible. There is no need to have an object in order to connect signals and slots. How can I do that?main.h
.... void dumpReceivedData(int type, QByteArray *msg);
main.cpp
int main() { ... Server server; QObject::connect(&server, Server::msgOut, dumpReceivedData); .... } void dumpReceivedData(int type, QByteArray *msg) { qDebug() << msg->constData(); }
server.h
... signals: static void msgOut(int type, QByteArray *msg);
server.cpp
void foo() { ... QByteArray *msg = new QByteArray; msg->append(aTcpSocket->readAll()); emit msgOut(1, msg); }
I am getting compile error:
no matching function for call to QObject::connect(Server *, void(&)(int, QByteArray*), void(&)(int, QByteArray*))
Thanks.
Taken from the docu
QObject::connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type = Qt::AutoConnection)
in the case you describted:
QObject::connect(&server, &Server::msgOut, this, &MyClass::dumpReceivedData);
-
Taken from the docu
QObject::connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type = Qt::AutoConnection)
in the case you describted:
QObject::connect(&server, &Server::msgOut, this, &MyClass::dumpReceivedData);
@J.Hilk
Thanks for reply but as I described above, I don't want to connect signals/slot between two objects, instead, I want to connect one object's signal to a function in main.cpp, where there is no class. -
@J.Hilk
Thanks for reply but as I described above, I don't want to connect signals/slot between two objects, instead, I want to connect one object's signal to a function in main.cpp, where there is no class.@kahlenberg
Ah, I see, you want a lambda connection to a signal, okconnect(&server, &Server::msgOut, [=]{ myFunction();}); //if you want to pass variables too connect(&server, &Server::msgOut, [=](QVariant a){ //do stuff with a or not myFunction(a);});
-
@J.Hilk
Thanks for reply but as I described above, I don't want to connect signals/slot between two objects, instead, I want to connect one object's signal to a function in main.cpp, where there is no class.You can also connect your signal with a static function. It's nearly the same with the connection to a lamba function, like @J-Hilk said.
static void staticSlot() { ... } int main() { ... QObject::connect(&yourObject, &YourObject::yourSignal, staticSlot); ... }
So in your case you just should add static to your void dumpReceivedData(int type, QByteArray *msg) function.
-
You can also connect your signal with a static function. It's nearly the same with the connection to a lamba function, like @J-Hilk said.
static void staticSlot() { ... } int main() { ... QObject::connect(&yourObject, &YourObject::yourSignal, staticSlot); ... }
So in your case you just should add static to your void dumpReceivedData(int type, QByteArray *msg) function.
@beecksche static is of no use in this case. See http://stackoverflow.com/questions/558122/what-is-a-static-function
-
You can also connect your signal with a static function. It's nearly the same with the connection to a lamba function, like @J-Hilk said.
static void staticSlot() { ... } int main() { ... QObject::connect(&yourObject, &YourObject::yourSignal, staticSlot); ... }
So in your case you just should add static to your void dumpReceivedData(int type, QByteArray *msg) function.
@beecksche, @VRonin
with or without static definition of the function dumpReceivedData
Compile error:no matching function call to 'QObject::connect(Server *, void(*)(int, QByteArray*), void (&)(int, QByteArray*))'
@J-Hilk
QObject::connect(&server, &Server::msgOut, [=](int type, QByteArray *msg) { dumpReceivedData(type, msg); } );
compile error:
no matching function for call to QObject::connect(Server *, void(*)(int, QByteArray*), main(int, char**)::__lambda0)
-
this works for me, could you test it?
#include <QApplication> #include <QTimer> #include <QDebug> #include <QDateTime> void printTime(){ qDebug() << QDateTime::currentDateTime(); } int main(int argc, char* argv[]) { QApplication app(argc, argv); QTimer t; t.setInterval(1000); QObject::connect(&t, &QTimer::timeout, printTime); t.start(); return app.exec(); }
-
It worked. I declared the function dumpReceivedData as static mistakenly. I deleted static keyword and it worked.
I also tested lambda function. It also worked.
Thanks a lot.