I have updated the code and please advice me how make it signals emit between each others.

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. //main Ui view
  2.  
  3. #include "clientui.h"
  4. #include <QApplication>
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. QApplication a(argc, argv);
  9. ClientUI w;
  10. w.show();
  11.  
  12.  
  13. return a.exec();
  14. }
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. //Client UI class header
  2.  
  3. #ifndef CLIENTUI_H
  4. #define CLIENTUI_H
  5.  
  6. #include <QDialog>
  7.  
  8. namespace Ui {
  9. class ClientUI;
  10. }
  11.  
  12. class ClientUI : public QDialog
  13. {
  14. Q_OBJECT
  15.  
  16. public:
  17. explicit ClientUI(QWidget *parent = 0);
  18. ~ClientUI();
  19.  
  20. void messageToSendOver();
  21. public slots:
  22. void receivedMessage(QByteArray& msg);
  23. signals:
  24. void messageToInterface(QString ipAddress, QByteArray& msg);
  25.  
  26. private slots:
  27. void on_sendBtn_clicked();
  28.  
  29. private:
  30. Ui::ClientUI *ui;
  31. QList<QString> sList;
  32. };
  33.  
  34. #endif // CLIENTUI_H
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. //Client UI class cpp
  2.  
  3. #include "clientui.h"
  4. #include "ui_clientui.h"
  5. #include "messageengine.h"
  6. #include "tcpconnection.h"
  7. #include "tcpinterface.h"
  8.  
  9. ClientUI::ClientUI(QWidget *parent) :
  10. QDialog(parent),
  11. ui(new Ui::ClientUI)
  12. {
  13. ui->setupUi(this);
  14. sList.append("Chat");
  15. sList.append("Mail");
  16. sList.append("Real time Stream");
  17.  
  18. ui->listBox->addItems(sList);
  19. }
  20.  
  21. ClientUI::~ClientUI()
  22. {
  23. delete ui;
  24. }
  25.  
  26. void ClientUI::messageToSendOver()
  27. {
  28. MessageEngine *msg = new MessageEngine();
  29. QByteArray dataOut = msg->sendAssociate();
  30.  
  31. if(dataOut.isEmpty())
  32. {
  33. ui->textBrowser->append("Error on message creation");
  34. }
  35. else
  36. {
  37.  
  38. emit messageToInterface("127.0.0.1", dataOut);
  39.  
  40. ui->textBrowser->append("message action from UI");
  41. }
  42.  
  43. }
  44.  
  45. void ClientUI::sendToMessageEngine()
  46. {
  47. TcpInterface *ti = new TcpInterface();
  48. connect(ti, SIGNAL(sendMessageToUI(QByteArray&)), this, SLOT(receivedMessage(QByteArray&)), Qt::DirectConnection);
  49. }
  50.  
  51. void ClientUI::on_sendBtn_clicked()
  52. {
  53. messageToSendOver();
  54.  
  55. }
  56.  
  57. void ClientUI::receivedMessage(QByteArray &msg)
  58. {
  59. qDebug() << "Incoming Message on UI: " << msg;
  60. ui->textBrowser->append("Incoming Message: " + msg);
  61. }
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. //message engine header
  2. #ifndef MESSAGEENGINE_H
  3. #define MESSAGEENGINE_H
  4.  
  5. #include <QObject>
  6.  
  7. class MessageEngine : public QObject
  8. {
  9. Q_OBJECT
  10. public:
  11. explicit MessageEngine(QObject *parent = 0);
  12.  
  13. QByteArray sendAssociate();
  14.  
  15. signals:
  16.  
  17. public slots:
  18. };
  19.  
  20. #endif // MESSAGEENGINE_H
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. //message engine CPP
  2. #include "messageengine.h"
  3.  
  4. MessageEngine::MessageEngine(QObject *parent) : QObject(parent)
  5. {
  6.  
  7. }
  8.  
  9. QByteArray MessageEngine::sendAssociate()
  10. {
  11. QByteArray dataOut = "In this section created from some creator";
  12. return dataOut;
  13.  
  14. }
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. //tcpInterface header
  2.  
  3. #ifndef TCPINTERFACE_H
  4. #define TCPINTERFACE_H
  5.  
  6. #include <QObject>
  7.  
  8. class TcpInterface : public QObject
  9. {
  10. Q_OBJECT
  11. public:
  12. explicit TcpInterface(QObject *parent = 0);
  13.  
  14. signals:
  15. void sendMessageToServer(QString ipAddres, QByteArray& message);
  16. void sendMessageToUI(QByteArray& message);
  17.  
  18. public slots:
  19. void sendToTcpConnection(QString ipAddress, QByteArray& message);
  20. void receivedFromTcpConnection(QByteArray& message);
  21. };
  22.  
  23. #endif // TCPINTERFACE_H
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. //tcpinterface cpp
  2. #include "tcpinterface.h"
  3. #include "clientui.h"
  4. #include "tcpconnection.h"
  5.  
  6. TcpInterface::TcpInterface(QObject *parent) : QObject(parent)
  7. {
  8. ClientUI *cui = new ClientUI();
  9. connect(cui, SIGNAL(messageToInterface(QString,QByteArray&)), this, SLOT(sendToTcpConnection(QString,QByteArray&)), Qt::DirectConnection);
  10. TcpConnection *tc = new TcpConnection();
  11. connect(tc, SIGNAL(receivedMessagefromServer(QByteArray&)), this, SLOT(receivedFromTcpConnection(QByteArray&)), Qt::DirectConnection);
  12.  
  13. }
  14.  
  15.  
  16. void TcpInterface::sendToTcpConnection(QString ipAddress, QByteArray &message)
  17. {
  18. emit sendMessageToServer(ipAddress, message);
  19. }
  20.  
  21. void TcpInterface::receivedFromTcpConnection(QByteArray &message)
  22. {
  23. emit sendMessageToUI(message);
  24. }
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. //tcpconnection header
  2.  
  3. #ifndef TCPCONNECTION_H
  4. #define TCPCONNECTION_H
  5.  
  6. #include <QObject>
  7. #include <QTcpSocket>
  8.  
  9. class TcpConnection : public QObject
  10. {
  11. Q_OBJECT
  12. public:
  13. explicit TcpConnection(QObject *parent = 0);
  14.  
  15. signals:
  16. // void sendToMessage(QByteArray &msg);
  17. void receivedMessagefromServer(QByteArray &msg);
  18.  
  19. public slots:
  20. void tcpConnected();
  21. void tcpReadyRead();
  22. void tcpDisconnected();
  23. void sendItOver(QString ipAddress, QByteArray& msg);
  24.  
  25. protected:
  26. QTcpSocket *m_socket;
  27. };
  28.  
  29. #endif // TCPCONNECTION_H
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. //tcpconnection cpp
  2.  
  3. #include "tcpconnection.h"
  4. #include "tcpinterface.h"
  5. #include "clientui.h"
  6.  
  7. TcpConnection::TcpConnection(QObject *parent) : QObject(parent)
  8. {
  9. m_socket = new QTcpSocket(this);
  10.  
  11. TcpInterface *ti = new TcpInterface();
  12. connect(ti, SIGNAL(sendMessageToServer(QString,QByteArray&)), this, SLOT(sendItOver(QString,QByteArray&)), Qt::DirectConnection);
  13.  
  14. connect(m_socket, SIGNAL(connected()), this, SLOT(tcpConnected()));
  15. connect(m_socket, SIGNAL(readyRead()), this, SLOT(tcpReadyRead()));
  16. connect(m_socket, SIGNAL(disconnected()), this, SLOT(tcpDisconnected()));
  17. }
  18.  
  19. void TcpConnection::tcpConnected()
  20. {
  21. qDebug() << "Ui Client connected with a Server";
  22. }
  23.  
  24. void TcpConnection::tcpReadyRead()
  25. {
  26. qDebug() << "Ui Client ready to read for incoming data from Server";
  27. QByteArray dataIn = m_socket->readAll();
  28. qDebug() << "Incoming message" << dataIn.toHex();
  29. emit receivedMessagefromServer(dataIn);
  30. }
  31.  
  32. void TcpConnection::tcpDisconnected()
  33. {
  34. qDebug() << "Ui Client disconnected from Server";
  35. m_socket->close();
  36. }
  37.  
  38.  
  39. void TcpConnection::sendItOver(QString ipAddress, QByteArray &msg)
  40. {
  41. m_socket->connectToHost(ipAddress, 6000);
  42. if(!m_socket->waitForConnected(3000))
  43. {
  44. qDebug() << "Connection error: " << m_socket->errorString();
  45. }
  46. else
  47. {
  48. qDebug() << "Connected to server ";
  49. if(m_socket->bytesAvailable())
  50. {
  51. m_socket->write(msg);
  52. if(!m_socket->waitForBytesWritten(30000))
  53. {
  54. qDebug() << "Error occured: " << m_socket->errorString();
  55. }
  56. }
  57. else
  58. {
  59. qDebug() << "Error occured: " << m_socket->errorString();
  60. }
  61. }
  62.  
  63. }
To copy to clipboard, switch view to plain text mode 



Many Thanks,
Rahul