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:-
//main Ui view
#include "clientui.h"
#include <QApplication>
int main(int argc, char *argv[])
{
ClientUI w;
w.show();
return a.exec();
}
//main Ui view
#include "clientui.h"
#include <QApplication>
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
//Client UI class header
#ifndef CLIENTUI_H
#define CLIENTUI_H
#include <QDialog>
namespace Ui {
class ClientUI;
}
{
Q_OBJECT
public:
explicit ClientUI
(QWidget *parent
= 0);
~ClientUI();
void messageToSendOver();
public slots:
void receivedMessage(QByteArray& msg);
signals:
void messageToInterface
(QString ipAddress, QByteArray
& msg
);
private slots:
void on_sendBtn_clicked();
private:
Ui::ClientUI *ui;
QList<QString> sList;
};
#endif // CLIENTUI_H
//Client UI class header
#ifndef CLIENTUI_H
#define CLIENTUI_H
#include <QDialog>
namespace Ui {
class ClientUI;
}
class ClientUI : public QDialog
{
Q_OBJECT
public:
explicit ClientUI(QWidget *parent = 0);
~ClientUI();
void messageToSendOver();
public slots:
void receivedMessage(QByteArray& msg);
signals:
void messageToInterface(QString ipAddress, QByteArray& msg);
private slots:
void on_sendBtn_clicked();
private:
Ui::ClientUI *ui;
QList<QString> sList;
};
#endif // CLIENTUI_H
To copy to clipboard, switch view to plain text mode
//Client UI class cpp
#include "clientui.h"
#include "ui_clientui.h"
#include "messageengine.h"
#include "tcpconnection.h"
#include "tcpinterface.h"
ClientUI
::ClientUI(QWidget *parent
) : ui(new Ui::ClientUI)
{
ui->setupUi(this);
sList.append("Chat");
sList.append("Mail");
sList.append("Real time Stream");
ui->listBox->addItems(sList);
}
ClientUI::~ClientUI()
{
delete ui;
}
void ClientUI::messageToSendOver()
{
MessageEngine *msg = new MessageEngine();
if(dataOut.isEmpty())
{
ui->textBrowser->append("Error on message creation");
}
else
{
emit messageToInterface("127.0.0.1", dataOut);
ui->textBrowser->append("message action from UI");
}
}
void ClientUI::sendToMessageEngine()
{
TcpInterface *ti = new TcpInterface();
connect(ti, SIGNAL(sendMessageToUI(QByteArray&)), this, SLOT(receivedMessage(QByteArray&)), Qt::DirectConnection);
}
void ClientUI::on_sendBtn_clicked()
{
messageToSendOver();
}
{
qDebug() << "Incoming Message on UI: " << msg;
ui->textBrowser->append("Incoming Message: " + msg);
}
//Client UI class cpp
#include "clientui.h"
#include "ui_clientui.h"
#include "messageengine.h"
#include "tcpconnection.h"
#include "tcpinterface.h"
ClientUI::ClientUI(QWidget *parent) :
QDialog(parent),
ui(new Ui::ClientUI)
{
ui->setupUi(this);
sList.append("Chat");
sList.append("Mail");
sList.append("Real time Stream");
ui->listBox->addItems(sList);
}
ClientUI::~ClientUI()
{
delete ui;
}
void ClientUI::messageToSendOver()
{
MessageEngine *msg = new MessageEngine();
QByteArray dataOut = msg->sendAssociate();
if(dataOut.isEmpty())
{
ui->textBrowser->append("Error on message creation");
}
else
{
emit messageToInterface("127.0.0.1", dataOut);
ui->textBrowser->append("message action from UI");
}
}
void ClientUI::sendToMessageEngine()
{
TcpInterface *ti = new TcpInterface();
connect(ti, SIGNAL(sendMessageToUI(QByteArray&)), this, SLOT(receivedMessage(QByteArray&)), Qt::DirectConnection);
}
void ClientUI::on_sendBtn_clicked()
{
messageToSendOver();
}
void ClientUI::receivedMessage(QByteArray &msg)
{
qDebug() << "Incoming Message on UI: " << msg;
ui->textBrowser->append("Incoming Message: " + msg);
}
To copy to clipboard, switch view to plain text mode
//message engine header
#ifndef MESSAGEENGINE_H
#define MESSAGEENGINE_H
#include <QObject>
class MessageEngine
: public QObject{
Q_OBJECT
public:
explicit MessageEngine
(QObject *parent
= 0);
signals:
public slots:
};
#endif // MESSAGEENGINE_H
//message engine header
#ifndef MESSAGEENGINE_H
#define MESSAGEENGINE_H
#include <QObject>
class MessageEngine : public QObject
{
Q_OBJECT
public:
explicit MessageEngine(QObject *parent = 0);
QByteArray sendAssociate();
signals:
public slots:
};
#endif // MESSAGEENGINE_H
To copy to clipboard, switch view to plain text mode
//message engine CPP
#include "messageengine.h"
{
}
{
QByteArray dataOut
= "In this section created from some creator";
return dataOut;
}
//message engine CPP
#include "messageengine.h"
MessageEngine::MessageEngine(QObject *parent) : QObject(parent)
{
}
QByteArray MessageEngine::sendAssociate()
{
QByteArray dataOut = "In this section created from some creator";
return dataOut;
}
To copy to clipboard, switch view to plain text mode
//tcpInterface header
#ifndef TCPINTERFACE_H
#define TCPINTERFACE_H
#include <QObject>
class TcpInterface
: public QObject{
Q_OBJECT
public:
explicit TcpInterface
(QObject *parent
= 0);
signals:
void sendMessageToServer
(QString ipAddres, QByteArray
& message
);
void sendMessageToUI(QByteArray& message);
public slots:
void sendToTcpConnection
(QString ipAddress, QByteArray
& message
);
void receivedFromTcpConnection(QByteArray& message);
};
#endif // TCPINTERFACE_H
//tcpInterface header
#ifndef TCPINTERFACE_H
#define TCPINTERFACE_H
#include <QObject>
class TcpInterface : public QObject
{
Q_OBJECT
public:
explicit TcpInterface(QObject *parent = 0);
signals:
void sendMessageToServer(QString ipAddres, QByteArray& message);
void sendMessageToUI(QByteArray& message);
public slots:
void sendToTcpConnection(QString ipAddress, QByteArray& message);
void receivedFromTcpConnection(QByteArray& message);
};
#endif // TCPINTERFACE_H
To copy to clipboard, switch view to plain text mode
//tcpinterface cpp
#include "tcpinterface.h"
#include "clientui.h"
#include "tcpconnection.h"
{
ClientUI *cui = new ClientUI();
connect(cui,
SIGNAL(messageToInterface
(QString,QByteArray
&)),
this,
SLOT(sendToTcpConnection
(QString,QByteArray
&)), Qt
::DirectConnection);
TcpConnection *tc = new TcpConnection();
connect(tc, SIGNAL(receivedMessagefromServer(QByteArray&)), this, SLOT(receivedFromTcpConnection(QByteArray&)), Qt::DirectConnection);
}
{
emit sendMessageToServer(ipAddress, message);
}
void TcpInterface
::receivedFromTcpConnection(QByteArray &message
) {
emit sendMessageToUI(message);
}
//tcpinterface cpp
#include "tcpinterface.h"
#include "clientui.h"
#include "tcpconnection.h"
TcpInterface::TcpInterface(QObject *parent) : QObject(parent)
{
ClientUI *cui = new ClientUI();
connect(cui, SIGNAL(messageToInterface(QString,QByteArray&)), this, SLOT(sendToTcpConnection(QString,QByteArray&)), Qt::DirectConnection);
TcpConnection *tc = new TcpConnection();
connect(tc, SIGNAL(receivedMessagefromServer(QByteArray&)), this, SLOT(receivedFromTcpConnection(QByteArray&)), Qt::DirectConnection);
}
void TcpInterface::sendToTcpConnection(QString ipAddress, QByteArray &message)
{
emit sendMessageToServer(ipAddress, message);
}
void TcpInterface::receivedFromTcpConnection(QByteArray &message)
{
emit sendMessageToUI(message);
}
To copy to clipboard, switch view to plain text mode
//tcpconnection header
#ifndef TCPCONNECTION_H
#define TCPCONNECTION_H
#include <QObject>
#include <QTcpSocket>
class TcpConnection
: public QObject{
Q_OBJECT
public:
explicit TcpConnection
(QObject *parent
= 0);
signals:
// void sendToMessage(QByteArray &msg);
public slots:
void tcpConnected();
void tcpReadyRead();
void tcpDisconnected();
void sendItOver
(QString ipAddress, QByteArray
& msg
);
protected:
};
#endif // TCPCONNECTION_H
//tcpconnection header
#ifndef TCPCONNECTION_H
#define TCPCONNECTION_H
#include <QObject>
#include <QTcpSocket>
class TcpConnection : public QObject
{
Q_OBJECT
public:
explicit TcpConnection(QObject *parent = 0);
signals:
// void sendToMessage(QByteArray &msg);
void receivedMessagefromServer(QByteArray &msg);
public slots:
void tcpConnected();
void tcpReadyRead();
void tcpDisconnected();
void sendItOver(QString ipAddress, QByteArray& msg);
protected:
QTcpSocket *m_socket;
};
#endif // TCPCONNECTION_H
To copy to clipboard, switch view to plain text mode
//tcpconnection cpp
#include "tcpconnection.h"
#include "tcpinterface.h"
#include "clientui.h"
{
TcpInterface *ti = new TcpInterface();
connect(ti,
SIGNAL(sendMessageToServer
(QString,QByteArray
&)),
this,
SLOT(sendItOver
(QString,QByteArray
&)), Qt
::DirectConnection);
connect(m_socket, SIGNAL(connected()), this, SLOT(tcpConnected()));
connect(m_socket, SIGNAL(readyRead()), this, SLOT(tcpReadyRead()));
connect(m_socket, SIGNAL(disconnected()), this, SLOT(tcpDisconnected()));
}
void TcpConnection::tcpConnected()
{
qDebug() << "Ui Client connected with a Server";
}
void TcpConnection::tcpReadyRead()
{
qDebug() << "Ui Client ready to read for incoming data from Server";
qDebug() << "Incoming message" << dataIn.toHex();
emit receivedMessagefromServer(dataIn);
}
void TcpConnection::tcpDisconnected()
{
qDebug() << "Ui Client disconnected from Server";
m_socket->close();
}
{
m_socket->connectToHost(ipAddress, 6000);
if(!m_socket->waitForConnected(3000))
{
qDebug() << "Connection error: " << m_socket->errorString();
}
else
{
qDebug() << "Connected to server ";
if(m_socket->bytesAvailable())
{
m_socket->write(msg);
if(!m_socket->waitForBytesWritten(30000))
{
qDebug() << "Error occured: " << m_socket->errorString();
}
}
else
{
qDebug() << "Error occured: " << m_socket->errorString();
}
}
}
//tcpconnection cpp
#include "tcpconnection.h"
#include "tcpinterface.h"
#include "clientui.h"
TcpConnection::TcpConnection(QObject *parent) : QObject(parent)
{
m_socket = new QTcpSocket(this);
TcpInterface *ti = new TcpInterface();
connect(ti, SIGNAL(sendMessageToServer(QString,QByteArray&)), this, SLOT(sendItOver(QString,QByteArray&)), Qt::DirectConnection);
connect(m_socket, SIGNAL(connected()), this, SLOT(tcpConnected()));
connect(m_socket, SIGNAL(readyRead()), this, SLOT(tcpReadyRead()));
connect(m_socket, SIGNAL(disconnected()), this, SLOT(tcpDisconnected()));
}
void TcpConnection::tcpConnected()
{
qDebug() << "Ui Client connected with a Server";
}
void TcpConnection::tcpReadyRead()
{
qDebug() << "Ui Client ready to read for incoming data from Server";
QByteArray dataIn = m_socket->readAll();
qDebug() << "Incoming message" << dataIn.toHex();
emit receivedMessagefromServer(dataIn);
}
void TcpConnection::tcpDisconnected()
{
qDebug() << "Ui Client disconnected from Server";
m_socket->close();
}
void TcpConnection::sendItOver(QString ipAddress, QByteArray &msg)
{
m_socket->connectToHost(ipAddress, 6000);
if(!m_socket->waitForConnected(3000))
{
qDebug() << "Connection error: " << m_socket->errorString();
}
else
{
qDebug() << "Connected to server ";
if(m_socket->bytesAvailable())
{
m_socket->write(msg);
if(!m_socket->waitForBytesWritten(30000))
{
qDebug() << "Error occured: " << m_socket->errorString();
}
}
else
{
qDebug() << "Error occured: " << m_socket->errorString();
}
}
}
To copy to clipboard, switch view to plain text mode
Many Thanks,
Rahul
Bookmarks