Hello
I have integrated a third-party code in Qt which is running in different thread apart from the main thread.The third party code opens a port and reads a data from the connected client via the socket and I have to display all those data on my Ui the third party code does not have any Qt application background.I have created a thread which run the third party application without disturbing my main thread.
But the problem is displaying the read data from the third party to my UI.I am writing the below prototype code for more understanding
QtGUiApplication.cpp
Qt Code:
  1. QtGuiApplication::QtGuiApplication(QWidget *parent)
  2. : QMainWindow(parent)
  3. {
  4. ui.setupUi(this);
  5. LoggerThread *thread = new LoggerThread;
  6. QObject::connect(ui.buttonStart, SIGNAL(clicked(bool)), this, SLOT(runthread()));
  7. }
  8.  
  9. void QtGuiApplication1::runthread() //where runthread() is declear a private slots in .h file
  10. {
  11. thread->start();
  12. }
To copy to clipboard, switch view to plain text mode 
LoggerThread.cpp
Qt Code:
  1. LoggerThread::LoggerThread(QObject *parent) :
  2. QThread(parent)
  3. {
  4.  
  5. }
  6. void LoggerThread::run()
  7. {
  8. while(1)
  9. {
  10. ClientThread *thr = ClientThread(serverSocket.accept());
  11. thr->start();
  12. }
  13. }
To copy to clipboard, switch view to plain text mode 
ClientThread.cpp (third party code)
Qt Code:
  1. ClientThread::ClientThread(Socket clientstock_):Clientsock(clientstock_)
  2. {
  3. cout << "Received a client connection!!!!" << endl;
  4. }
  5. void ClientThread::run()
  6. {
  7. while (1)
  8. {
  9. if (!clientsock.isOpen())
  10. {
  11. return;
  12. }
  13. SocketBuffer msgSizeBuffer(sizeof(unsigned int));
  14. if (!clientsock.read(msgSizeBuffer))
  15. {
  16. return;
  17. }
  18. unsigned int msgSize = msgSizeBuffer.readInt();
  19. SocketBuffer buffer(msgSize);
  20. if (!clientsock.read(buffer))
  21. {
  22. return;
  23. }
  24. spi::InternalLoggingEvent event = readFromBuffer(buffer);
  25. string value = event.getMessage(); //This value i have to display on TextEdit
  26.  
  27. }
  28. }
To copy to clipboard, switch view to plain text mode 

I tried emitting the signals to get the value on my Ui, find the below code.

QtGUiApplication.cpp
Qt Code:
  1. QtGuiApplication::QtGuiApplication(QWidget *parent)
  2. : QMainWindow(parent)
  3. {
  4. ui.setupUi(this);
  5. LoggerThread *thread = new LoggerThread;
  6. QObject::connect(ui.pb_start, SIGNAL(clicked(bool)), this, SLOT(runthread()));
  7. }
  8.  
  9. void QtGuiApplication1::runthread() //where runthread() is declear a private slots in .h file
  10. {
  11. QObject::connect(thread, SIGNAL(SendResult(QString)), this, SLOT(Displaydata(QString))); //new Line added
  12. thread->start();
  13. }
  14. void QtGuiApplication1::Displaydata(const QString &data1) //where Displaydata(const QString &) is declear a private slots in .h file,added new function
  15. {
  16. ui.lineEdit->setText(data1);
  17. }
To copy to clipboard, switch view to plain text mode 
LoggerThread.cpp
Qt Code:
  1. LoggerThread::LoggerThread(QObject *parent) :
  2. QThread(parent)
  3. {
  4.  
  5. }
  6. void LoggerThread::run()
  7. {
  8. while(1)
  9. {
  10. ClientThread *thr = ClientThread(serverSocket.accept(), reaper);
  11. thr->start();
  12. }
  13. void LoggerThread::CallbackData(string value) // added new function
  14. {
  15. QString result = QString::fromStdString(value);
  16. emit SendResult(result); //where SendResult(const QString &result) is declear as signals: in .h file
  17. }
  18. }
To copy to clipboard, switch view to plain text mode 
ClientThread.cpp (third party code)
Qt Code:
  1. LoggerThread data;
  2. ClientThread::ClientThread(Socket clientstock_):Clientsock(clientstock_)
  3. {
  4. cout << "Received a client connection!!!!" << endl;
  5. }
  6. void ClientThread::run()
  7. {
  8. while (1)
  9. {
  10. if (!clientsock.isOpen())
  11. {
  12. return;
  13. }
  14. SocketBuffer msgSizeBuffer(sizeof(unsigned int));
  15. if (!clientsock.read(msgSizeBuffer))
  16. {
  17. return;
  18. }
  19. unsigned int msgSize = msgSizeBuffer.readInt();
  20. SocketBuffer buffer(msgSize);
  21. if (!clientsock.read(buffer))
  22. {
  23. return;
  24. }
  25. spi::InternalLoggingEvent event = readFromBuffer(buffer);
  26. string value = event.getMessage(); //This value has to be display on TextEdit
  27. data.CallbackData(s);// added new line
  28.  
  29. }
  30. }
To copy to clipboard, switch view to plain text mode 

But i found that emit SendResult(result) is not getting emitted and my slot is not getting called.