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:-
#include "clientui.h"
#include <QApplication>
#include "../SytemsLogs/easylogging++.h"
INITIALIZE_EASYLOGGINGPP
int main(int argc, char *argv[])
{
ClientUi w;
w.show();
return a.exec();
}
#include "clientui.h"
#include <QApplication>
#include "../SytemsLogs/easylogging++.h"
INITIALIZE_EASYLOGGINGPP
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ClientUi w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
#ifndef CLIENTUI_H
#define CLIENTUI_H
#include <QWidget>
#include <QDebug>
#include "mdsclient.h"
#include <QtConcurrent/QtConcurrent>
#include "tcpcommunication.h"
namespace Ui {
class ClientUi;
}
{
Q_OBJECT
public:
explicit ClientUi
(QWidget *parent
= 0);
~ClientUi();
signals:
void sendCommunicationMessage
(QString ipAddr, QByteArray
& message
);
public slots:
void sendServerMessage
(QString ipAddr, QByteArray
& message
);
void receivedServerMessage(QByteArray& msg);
private slots:
void on_powerBtn_clicked();
void on_sendBtn_clicked();
protected:
MdsClient mds;
TcpCommunication tc;
private:
Ui::ClientUi *ui;
QList<QString> m_list;
};
#endif // CLIENTUI_H
#ifndef CLIENTUI_H
#define CLIENTUI_H
#include <QWidget>
#include <QDebug>
#include "mdsclient.h"
#include <QtConcurrent/QtConcurrent>
#include "tcpcommunication.h"
namespace Ui {
class ClientUi;
}
class ClientUi : public QWidget
{
Q_OBJECT
public:
explicit ClientUi(QWidget *parent = 0);
~ClientUi();
signals:
void sendCommunicationMessage(QString ipAddr, QByteArray& message);
public slots:
void sendServerMessage(QString ipAddr, QByteArray& message);
void receivedServerMessage(QByteArray& msg);
private slots:
void on_powerBtn_clicked();
void on_sendBtn_clicked();
protected:
MdsClient mds;
TcpCommunication tc;
private:
Ui::ClientUi *ui;
QList<QString> m_list;
};
#endif // CLIENTUI_H
To copy to clipboard, switch view to plain text mode
#include "clientui.h"
#include "ui_clientui.h"
ClientUi
::ClientUi(QWidget *parent
) : ui(new Ui::ClientUi)
{
ui->setupUi(this);
m_list.append("Associate");
m_list.append("Update");
ui->messageTypeCom->addItems(m_list);
}
ClientUi::~ClientUi()
{
delete ui;
}
{
qDebug() << "Sender IP addres: " << ipAddr << "Message: "<< message;
emit sendCommunicationMessage(ipAddr, message);
}
void ClientUi
::receivedServerMessage(QByteArray &msg
) {
ui->textBrowser->append("Received server Message: " + msg);
}
void ClientUi::on_powerBtn_clicked()
{
qDebug() << "Device powered on";
ui->textBrowser->append("Device powered on");
}
void ClientUi::on_sendBtn_clicked()
{
qDebug() << "Send button clicked";
ui->textBrowser->append("Send button clicked");
connect(&mds, &MdsClient::sendAssociateMessage, this, &ClientUi::sendServerMessage);
connect(this, &ClientUi::sendCommunicationMessage, &tc, &TcpCommunication::clientSendMessageToServer);
connect(&tc, &TcpCommunication::sendToUI, this, &ClientUi::receivedServerMessage);
mds.createAssociateMessage();
tc.startTcpCommunication();
}
#include "clientui.h"
#include "ui_clientui.h"
ClientUi::ClientUi(QWidget *parent) :
QWidget(parent),
ui(new Ui::ClientUi)
{
ui->setupUi(this);
m_list.append("Associate");
m_list.append("Update");
ui->messageTypeCom->addItems(m_list);
}
ClientUi::~ClientUi()
{
delete ui;
}
void ClientUi::sendServerMessage(QString ipAddr, QByteArray &message)
{
qDebug() << "Sender IP addres: " << ipAddr << "Message: "<< message;
emit sendCommunicationMessage(ipAddr, message);
}
void ClientUi::receivedServerMessage(QByteArray &msg)
{
ui->textBrowser->append("Received server Message: " + msg);
}
void ClientUi::on_powerBtn_clicked()
{
qDebug() << "Device powered on";
ui->textBrowser->append("Device powered on");
}
void ClientUi::on_sendBtn_clicked()
{
qDebug() << "Send button clicked";
ui->textBrowser->append("Send button clicked");
connect(&mds, &MdsClient::sendAssociateMessage, this, &ClientUi::sendServerMessage);
connect(this, &ClientUi::sendCommunicationMessage, &tc, &TcpCommunication::clientSendMessageToServer);
connect(&tc, &TcpCommunication::sendToUI, this, &ClientUi::receivedServerMessage);
mds.createAssociateMessage();
tc.startTcpCommunication();
}
To copy to clipboard, switch view to plain text mode
#ifndef MDSCLIENT_H
#define MDSCLIENT_H
#include <QObject>
#include <QThread>
#include <QHostAddress>
{
Q_OBJECT
public:
explicit MdsClient
(QObject *parent
= 0);
void createAssociateMessage();
signals:
void sendAssociateMessage
(QString ipAddr, QByteArray
& message
);
public slots:
void receivedMessage
(QString ipAddr, QByteArray
& msg
);
};
#endif // MDSCLIENT_H
#ifndef MDSCLIENT_H
#define MDSCLIENT_H
#include <QObject>
#include <QThread>
#include <QHostAddress>
class MdsClient : public QObject
{
Q_OBJECT
public:
explicit MdsClient(QObject *parent = 0);
void createAssociateMessage();
signals:
void sendAssociateMessage(QString ipAddr, QByteArray& message);
public slots:
void receivedMessage(QString ipAddr, QByteArray& msg);
};
#endif // MDSCLIENT_H
To copy to clipboard, switch view to plain text mode
#include "mdsclient.h"
{
}
void MdsClient::createAssociateMessage()
{
//I have seperate message creator to create message
QByteArray dataOut
= "This message created by message creator";
emit sendAssociateMessage("127.0.0.1", dataOut);
QThread::currentThread()->msleep
(100);
qDebug() << "message creation emited back to UI";
}
void MdsClient
::receivedMessage(QString ipAddr, QByteArray
& msg
) {
qDebug() << "Ip addres: " << ipAddr << "Message: " << msg;
}
#include "mdsclient.h"
MdsClient::MdsClient(QObject *parent) : QObject(parent)
{
}
void MdsClient::createAssociateMessage()
{
//I have seperate message creator to create message
QByteArray dataOut = "This message created by message creator";
emit sendAssociateMessage("127.0.0.1", dataOut);
QThread::currentThread()->msleep(100);
qDebug() << "message creation emited back to UI";
}
void MdsClient::receivedMessage(QString ipAddr, QByteArray& msg)
{
qDebug() << "Ip addres: " << ipAddr << "Message: " << msg;
}
To copy to clipboard, switch view to plain text mode
#ifndef TCPCOMMUNICATION_H
#define TCPCOMMUNICATION_H
#include <QObject>
#include <QTcpSocket>
#include "../SytemsLogs/easylogging++.h"
class TcpCommunication
: public QObject{
Q_OBJECT
public:
explicit TcpCommunication
(QObject *parent
= 0);
void bytesWritten(qint64 bytes);
bool newConnection
(QString iAddres
);
void messageExchanges();
void startTcpCommunication();
signals:
void sendToUI(QByteArray& msg);
public slots:
void clientConnected();
void clientDisconnected();
void clientReadyRead();
void clientSendMessageToServer
(const QString ipAddress, QByteArray
& msg
);
private:
};
#endif // TCPCOMMUNICATION_H
#ifndef TCPCOMMUNICATION_H
#define TCPCOMMUNICATION_H
#include <QObject>
#include <QTcpSocket>
#include "../SytemsLogs/easylogging++.h"
class TcpCommunication : public QObject
{
Q_OBJECT
public:
explicit TcpCommunication(QObject *parent = 0);
void bytesWritten(qint64 bytes);
bool newConnection(QString iAddres);
void messageExchanges();
void startTcpCommunication();
signals:
void sendToUI(QByteArray& msg);
public slots:
void clientConnected();
void clientDisconnected();
void clientReadyRead();
void clientSendMessageToServer(const QString ipAddress, QByteArray& msg);
private:
QTcpSocket *m_socket;
};
#endif // TCPCOMMUNICATION_H
To copy to clipboard, switch view to plain text mode
#include "tcpcommunication.h"
{
}
void TcpCommunication::startTcpCommunication()
{
connect(m_socket, SIGNAL(connected()), this, SLOT(clientConnected()));
connect(m_socket,SIGNAL(disconnected()), this, SLOT(clientDisConnected()));
connect(m_socket,SIGNAL(readyRead()), this, SLOT(clientReadyRead()));
}
void TcpCommunication::clientConnected()
{
LOG(INFO) << "connected " ;
}
void TcpCommunication::clientDisconnected()
{
LOG(INFO) << "Disconnected " ;
}
void TcpCommunication::clientReadyRead()
{
LOG(INFO) << "Data in: " << dataIn ;
emit sendToUI(dataIn);
}
void TcpCommunication
::clientSendMessageToServer(const QString ipAddress,
QByteArray &msg
) {
m_socket->connectToHost(ipAddress, 6000);
if(!m_socket->waitForConnected(3000))
{
LOG(ERROR) << "Error occured " << m_socket->errorString();
}
else
{
LOG(INFO) << "connected " ;
m_socket->write(msg);
}
}
#include "tcpcommunication.h"
TcpCommunication::TcpCommunication(QObject *parent) : QObject(parent)
{
}
void TcpCommunication::startTcpCommunication()
{
m_socket = new QTcpSocket(this);
connect(m_socket, SIGNAL(connected()), this, SLOT(clientConnected()));
connect(m_socket,SIGNAL(disconnected()), this, SLOT(clientDisConnected()));
connect(m_socket,SIGNAL(readyRead()), this, SLOT(clientReadyRead()));
}
void TcpCommunication::clientConnected()
{
LOG(INFO) << "connected " ;
}
void TcpCommunication::clientDisconnected()
{
LOG(INFO) << "Disconnected " ;
}
void TcpCommunication::clientReadyRead()
{
QByteArray dataIn = m_socket->readAll();
LOG(INFO) << "Data in: " << dataIn ;
emit sendToUI(dataIn);
}
void TcpCommunication::clientSendMessageToServer(const QString ipAddress, QByteArray &msg)
{
m_socket->connectToHost(ipAddress, 6000);
if(!m_socket->waitForConnected(3000))
{
LOG(ERROR) << "Error occured " << m_socket->errorString();
}
else
{
LOG(INFO) << "connected " ;
m_socket->write(msg);
}
}
To copy to clipboard, switch view to plain text mode
Many Thanks,
Rahul
Bookmarks