how to send a signal from the DLL library implemented to the application (.exe)
-
how to send a signal from the DLL library implemented to the application (.exe). that is, once a dll library has been created and I insert a signal it must send using with (emit sendSignal(QString)) and it must reach the executable application (.exe), when the application captures the sendSignalQString) signal using with ( QObject::connec (&myLibDLL, &MyLibDLL::sendSignal, mainWindow, &MainWindow::slotSignal). I've tried it this way but it doesn't do anything. as I create a connection between DLL and application (.exe) which C ++ codes are used? thank you very much for those who help me
-
@Domenico could you please show some code?
- Regarding the library, snippet for how the signal is defined and snippet where the signal is emitted. Project (.pro) could be useful as well.
- Regarding the application using the library, snippet where you load the library, where you do the connection and snippet for the slot that should be called.
-
I give an example similar to my code which does not receive a signal from the dll:
MyLib.h
class MYLIBSHARED_EXPORT MyLib : public QObject { Q_OBJECT public: MyLib (QObject* parent = 0); ~MyLib (); void send(); Q_SIGNALS: void sendSignal(QString); };
MyLib.cpp
MyLib::MyLib(QObject *parent) : QObject(parent) { } MyLib::~MyLib() { } void MyLib::send() { emit sendSignal("ciao"); }
main.cpp of the DLL
#include <QtCore/QCoreApplication> #include "MyLib.h" int main( int argc, char *argv[ ] ) { QCoreApplication a(argc,argv); MyLib lib; return true; }
ApplicationGui.h
#include "MyLib.h" #include <QDebug> class ApplicationGui: public QObject { Q_OBJECT public: ApplicationGui(QObject* parent = 0); ~ApplicationGui(); MyLib lib; public slots: void slotSignal(QString); }
ApplicationGui.cpp
ApplicationGui::ApplicationGui(QObject *parent) : QObject(parent) { lib.send(); QObject::connect(&lib,&lib::sendSignal,this,&ApplicationGui::slotSignal); } ApplicationGui::~ApplicationGui() { } void slotSignal(QString print) { qDebug()<<"receiver signal "<<print; }
what's the problem? what's wrong?
-
@Pablo-J-Rogina
just modified. thanks. what's wrong with the code? -
@Domenico said in how to send a signal from the DLL library implemented to the application (.exe):
what's wrong with the code?
You neither instantiate ApplicationGui nor you run the main event loop.
-
@Christian-Ehrlicher said in how to send a signal from the DLL library implemented to the application (.exe):
You neither instantiate ApplicationGui nor you run the main event loop
sorry for ignorance what do we mean by not creating an instance nor running the main cycle of events? can you give me an example
-
@Domenico said in how to send a signal from the DLL library implemented to the application (.exe):
can you give me an example
When you take a look at your main() you will see that you don't call QApplication::exec() and simply return - so what should happen?
-
so there is no need for this instance
#include <QtCore/QCoreApplication> #include "MyLib .h" int main( int argc, char *argv[ ] ) { QCoreApplication a(argc,argv); MyLib lib; return true; }
and would be fine even without main or:
#include <QtCore/QCoreApplication> #include "MyLib .h" int main( int argc, char *argv[ ] ) { QCoreApplication a(argc,argv); return a.exec(); }
quite right?
-
@Domenico
Hi
Yes that should do it.However
lib.send(); // emits signal. QObject::connect(&lib,&lib::sendSignal,this,&ApplicationGui::slotSignal); // then connect to signal ?
That would not trigger ApplicationGui::slotSignal
unless you call
lib.send();
again. -
thank you all. I solved the problem. the problem is that I made calls after having exported the function from the library DLL that is with:
extern "C" __declspec(dllexport) void send();
I imagine this way it is not connected between dlls and application gui for the use of the signal/slot.
therefore to make the use of the signal / slot work, it is sufficient to make function calls to the dll library with an instance:MyLib lib; lib.send(); ...
thank you!
-
@Domenico said in how to send a signal from the DLL library implemented to the application (.exe):
I solved the problem.
so please don't forget to mark your post as such!