Thank you for the reply and I am in the early stage of Qt SIGNAL and SLOT understanding.
After your comments, I have gone through Qt Documentation and corrected my code and now I have Error shown as below:-

The inferior stopped because it received a signal from the operating system.

Signal name :
SIGSEGV
Signal meaning :
Segmentation fault

Can you please tell me what I am doing wrong here..

Kind Regards,
Rahul

Quote Originally Posted by RahulY View Post
HI I tried to implement chat client and I am finding hard to make the signal and slot work as per my expectation.

Please help me on this...

Expected working order:-

UI call the message engine to create message and emit the message with IP address via tcp interface and interface emit it to TcpConnection to send it over to server but currently It doesn't work!
my codes are:-
Qt Code:
  1. #include "clientui.h"
  2. #include <QApplication>
  3. #include "../SytemsLogs/easylogging++.h"
  4.  
  5.  
  6. INITIALIZE_EASYLOGGINGPP
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10. QApplication a(argc, argv);
  11. ClientUi w;
  12. w.show();
  13.  
  14. return a.exec();
  15. }
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. #ifndef CLIENTUI_H
  2. #define CLIENTUI_H
  3.  
  4. #include <QWidget>
  5. #include <QDebug>
  6. #include "mdsclient.h"
  7. #include <QtConcurrent/QtConcurrent>
  8. #include "tcpcommunication.h"
  9.  
  10. namespace Ui {
  11. class ClientUi;
  12. }
  13.  
  14. class ClientUi : public QWidget
  15. {
  16. Q_OBJECT
  17.  
  18. public:
  19. explicit ClientUi(QWidget *parent = 0);
  20. ~ClientUi();
  21.  
  22. signals:
  23. void sendCommunicationMessage(QString ipAddr, QByteArray& message);
  24.  
  25. public slots:
  26. void sendServerMessage(QString ipAddr, QByteArray& message);
  27.  
  28. void receivedServerMessage(QByteArray& msg);
  29.  
  30. private slots:
  31. void on_powerBtn_clicked();
  32.  
  33. void on_sendBtn_clicked();
  34. protected:
  35. MdsClient mds;
  36. TcpCommunication tc;
  37.  
  38. private:
  39. Ui::ClientUi *ui;
  40. QList<QString> m_list;
  41. };
  42.  
  43. #endif // CLIENTUI_H
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. #include "clientui.h"
  2. #include "ui_clientui.h"
  3.  
  4. ClientUi::ClientUi(QWidget *parent) :
  5. QWidget(parent),
  6. ui(new Ui::ClientUi)
  7. {
  8. ui->setupUi(this);
  9. m_list.append("Associate");
  10. m_list.append("Update");
  11.  
  12. ui->messageTypeCom->addItems(m_list);
  13. }
  14.  
  15. ClientUi::~ClientUi()
  16. {
  17. delete ui;
  18. }
  19.  
  20. void ClientUi::sendServerMessage(QString ipAddr, QByteArray &message)
  21. {
  22. qDebug() << "Sender IP addres: " << ipAddr << "Message: "<< message;
  23. emit sendCommunicationMessage(ipAddr, message);
  24. }
  25.  
  26. void ClientUi::receivedServerMessage(QByteArray &msg)
  27. {
  28. ui->textBrowser->append("Received server Message: " + msg);
  29. }
  30. void ClientUi::on_powerBtn_clicked()
  31. {
  32. qDebug() << "Device powered on";
  33. ui->textBrowser->append("Device powered on");
  34. }
  35.  
  36. void ClientUi::on_sendBtn_clicked()
  37. {
  38. qDebug() << "Send button clicked";
  39. ui->textBrowser->append("Send button clicked");
  40. connect(&mds, &MdsClient::sendAssociateMessage, this, &ClientUi::sendServerMessage);
  41. connect(this, &ClientUi::sendCommunicationMessage, &tc, &TcpCommunication::clientSendMessageToServer);
  42. connect(&tc, &TcpCommunication::sendToUI, this, &ClientUi::receivedServerMessage);
  43. mds.createAssociateMessage();
  44. tc.startTcpCommunication();
  45. }
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. #ifndef MDSCLIENT_H
  2. #define MDSCLIENT_H
  3.  
  4. #include <QObject>
  5. #include <QThread>
  6. #include <QHostAddress>
  7.  
  8.  
  9. class MdsClient : public QObject
  10. {
  11. Q_OBJECT
  12. public:
  13. explicit MdsClient(QObject *parent = 0);
  14.  
  15. void createAssociateMessage();
  16.  
  17. signals:
  18. void sendAssociateMessage(QString ipAddr, QByteArray& message);
  19.  
  20. public slots:
  21. void receivedMessage(QString ipAddr, QByteArray& msg);
  22.  
  23. };
  24.  
  25. #endif // MDSCLIENT_H
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. #include "mdsclient.h"
  2.  
  3. MdsClient::MdsClient(QObject *parent) : QObject(parent)
  4. {
  5.  
  6. }
  7.  
  8.  
  9. void MdsClient::createAssociateMessage()
  10. {
  11.  
  12. //I have seperate message creator to create message
  13. QByteArray dataOut = "This message created by message creator";
  14. emit sendAssociateMessage("127.0.0.1", dataOut);
  15. QThread::currentThread()->msleep(100);
  16. qDebug() << "message creation emited back to UI";
  17. }
  18.  
  19.  
  20. void MdsClient::receivedMessage(QString ipAddr, QByteArray& msg)
  21. {
  22.  
  23. qDebug() << "Ip addres: " << ipAddr << "Message: " << msg;
  24. }
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. #ifndef TCPCOMMUNICATION_H
  2. #define TCPCOMMUNICATION_H
  3.  
  4. #include <QObject>
  5. #include <QTcpSocket>
  6. #include "../SytemsLogs/easylogging++.h"
  7.  
  8. class TcpCommunication : public QObject
  9. {
  10. Q_OBJECT
  11. public:
  12. explicit TcpCommunication(QObject *parent = 0);
  13.  
  14. void bytesWritten(qint64 bytes);
  15. bool newConnection(QString iAddres);
  16. void messageExchanges();
  17. void startTcpCommunication();
  18.  
  19. signals:
  20. void sendToUI(QByteArray& msg);
  21.  
  22. public slots:
  23. void clientConnected();
  24. void clientDisconnected();
  25. void clientReadyRead();
  26. void clientSendMessageToServer(const QString ipAddress, QByteArray& msg);
  27.  
  28. private:
  29. QTcpSocket *m_socket;
  30. };
  31.  
  32. #endif // TCPCOMMUNICATION_H
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. #include "tcpcommunication.h"
  2.  
  3. TcpCommunication::TcpCommunication(QObject *parent) : QObject(parent)
  4. {
  5.  
  6. }
  7.  
  8. void TcpCommunication::startTcpCommunication()
  9. {
  10. m_socket = new QTcpSocket(this);
  11. connect(m_socket, SIGNAL(connected()), this, SLOT(clientConnected()));
  12. connect(m_socket,SIGNAL(disconnected()), this, SLOT(clientDisConnected()));
  13. connect(m_socket,SIGNAL(readyRead()), this, SLOT(clientReadyRead()));
  14. }
  15.  
  16. void TcpCommunication::clientConnected()
  17. {
  18. LOG(INFO) << "connected " ;
  19. }
  20.  
  21. void TcpCommunication::clientDisconnected()
  22. {
  23. LOG(INFO) << "Disconnected " ;
  24. }
  25.  
  26. void TcpCommunication::clientReadyRead()
  27. {
  28. QByteArray dataIn = m_socket->readAll();
  29. LOG(INFO) << "Data in: " << dataIn ;
  30. emit sendToUI(dataIn);
  31.  
  32. }
  33.  
  34. void TcpCommunication::clientSendMessageToServer(const QString ipAddress, QByteArray &msg)
  35. {
  36. m_socket->connectToHost(ipAddress, 6000);
  37.  
  38. if(!m_socket->waitForConnected(3000))
  39. {
  40. LOG(ERROR) << "Error occured " << m_socket->errorString();
  41. }
  42. else
  43. {
  44. LOG(INFO) << "connected " ;
  45. m_socket->write(msg);
  46. }
  47. }
To copy to clipboard, switch view to plain text mode 

Many Thanks,
Rahul