How to send signal between other files in C++
-
Hello.
I want to send a signal from CppA to CppB, but I don't know how to do it.
How can I do this?CppA
void CppA::sigSend(){ emit sig(); }
CppB
CppB::CppB(QObject *parent): QObject(parent) { CppA* cppA = new CppA(this); connect(cppA, &CppA::sig, this, &CppB::slotRecv); } void CppB::slotRecv() { qDebug() << "OK"; }
-
@KroMignon
I'm asking because I couldn't figure it out after reading...
I tried changing some of the code, but it still doesn't work.CppB::CppB() { CppA cppA; connect(&cppA, &CppA::sig, this, &CppB::slotRecv); }
Also, CppA::sigSend is executed in one second cycle using Timer in QML.
@w-tkm said in How to send signal between other files in C++:
Also, CppA::sigSend is executed in one second cycle using Timer in QML.
I don't think so, you have declared
cppA
as a local variable inCppB
constructor.
At constructor end, this variable will be destroyed, so it cannot generate any signal. -
Hello.
I want to send a signal from CppA to CppB, but I don't know how to do it.
How can I do this?CppA
void CppA::sigSend(){ emit sig(); }
CppB
CppB::CppB(QObject *parent): QObject(parent) { CppA* cppA = new CppA(this); connect(cppA, &CppA::sig, this, &CppB::slotRecv); } void CppB::slotRecv() { qDebug() << "OK"; }
@w-tkm said in How to send signal between other files in C++:
I want to send a signal from CppA to CppB, but I don't know how to do it.
How can I do this?It is not that complicated, but you should first take time to read at least how it works: https://doc.qt.io/qt-5/signalsandslots.html
If you still have trouble, explain us what did not work and you will got help.
Doing in reverse order will not help you. -
@w-tkm said in How to send signal between other files in C++:
I want to send a signal from CppA to CppB, but I don't know how to do it.
How can I do this?It is not that complicated, but you should first take time to read at least how it works: https://doc.qt.io/qt-5/signalsandslots.html
If you still have trouble, explain us what did not work and you will got help.
Doing in reverse order will not help you.@KroMignon
I'm asking because I couldn't figure it out after reading...
I tried changing some of the code, but it still doesn't work.CppB::CppB() { CppA cppA; connect(&cppA, &CppA::sig, this, &CppB::slotRecv); }
Also, CppA::sigSend is executed in one second cycle using Timer in QML.
-
@KroMignon
I'm asking because I couldn't figure it out after reading...
I tried changing some of the code, but it still doesn't work.CppB::CppB() { CppA cppA; connect(&cppA, &CppA::sig, this, &CppB::slotRecv); }
Also, CppA::sigSend is executed in one second cycle using Timer in QML.
@w-tkm said in How to send signal between other files in C++:
CppA cppA;
cppA is a local variable and is destroyed when CppB::CppB() finishes...
-
@KroMignon
I'm asking because I couldn't figure it out after reading...
I tried changing some of the code, but it still doesn't work.CppB::CppB() { CppA cppA; connect(&cppA, &CppA::sig, this, &CppB::slotRecv); }
Also, CppA::sigSend is executed in one second cycle using Timer in QML.
@w-tkm said in How to send signal between other files in C++:
Also, CppA::sigSend is executed in one second cycle using Timer in QML.
I don't think so, you have declared
cppA
as a local variable inCppB
constructor.
At constructor end, this variable will be destroyed, so it cannot generate any signal. -
@w-tkm said in How to send signal between other files in C++:
Also, CppA::sigSend is executed in one second cycle using Timer in QML.
I don't think so, you have declared
cppA
as a local variable inCppB
constructor.
At constructor end, this variable will be destroyed, so it cannot generate any signal.@KroMignon
Thanks,I managed to make it work.
Finally, I realized that I need to connect at the CppA where the signal is sent.CppA::CppA(QObject *parent): QObject(parent) { cppB = new CppB(); connect( this, &CppA::sig, cppB, &CppB::slotRecv); }