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>
int main(int argc, char *argv[])
{
ClientUI w;
w.show();
return a.exec();
}
#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
#ifndef CLIENTUI_H
#define CLIENTUI_H
#include <QDialog>
namespace Ui {
class ClientUI;
}
{
Q_OBJECT
public:
explicit ClientUI
(QWidget *parent
= 0);
~ClientUI();
void messageToSendOver();
signals:
void messageToSend
(QString ipAddress, QByteArray
& msg
);
private slots:
void on_sendBtn_clicked();
private:
Ui::ClientUI *ui;
QList<QString> sList;
};
#endif // CLIENTUI_H
#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();
signals:
void messageToSend(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
#include "clientui.h"
#include "ui_clientui.h"
#include "messageengine.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();
emit messageToSend("127.0.0.1", dataOut);
ui->textBrowser->append("message action from UI");
}
void ClientUI::on_sendBtn_clicked()
{
messageToSendOver();
}
#include "clientui.h"
#include "ui_clientui.h"
#include "messageengine.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();
emit messageToSend("127.0.0.1", dataOut);
ui->textBrowser->append("message action from UI");
}
void ClientUI::on_sendBtn_clicked()
{
messageToSendOver();
}
To copy to clipboard, switch view to plain text mode
#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
#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
#include "messageengine.h"
{
}
{
QByteArray dataOut
= "In this section created from some creator";
return dataOut;
}
#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
#ifndef TCPINTERFACE_H
#define TCPINTERFACE_H
#include <QObject>
class TcpInterface
: public QObject{
Q_OBJECT
public:
explicit TcpInterface
(QObject *parent
= 0);
void sendMessageViaTcpConnection();
signals:
void sendMessageToServer
(QString ipAddres, QByteArray
& message
);
void receivedMessageFromServer(QByteArray& message);
public slots:
void sendToTcpConnection
(QString ipAddress, QByteArray
& message
);
};
#endif // TCPINTERFACE_H
#ifndef TCPINTERFACE_H
#define TCPINTERFACE_H
#include <QObject>
class TcpInterface : public QObject
{
Q_OBJECT
public:
explicit TcpInterface(QObject *parent = 0);
void sendMessageViaTcpConnection();
signals:
void sendMessageToServer(QString ipAddres, QByteArray& message);
void receivedMessageFromServer(QByteArray& message);
public slots:
void sendToTcpConnection(QString ipAddress, QByteArray& message);
};
#endif // TCPINTERFACE_H
To copy to clipboard, switch view to plain text mode
#include "tcpinterface.h"
#include "clientui.h"
{
ClientUI *cui = new ClientUI();
connect(cui,
SIGNAL(messageToSend
(QString,QByteArray
&)),
this,
SLOT(sendToTcpConnection
(QString,QByteArray
&)), Qt
::DirectConnection);
}
void TcpInterface::sendMessageViaTcpConnection()
{
}
{
emit sendMessageToServer(ipAddress, message);
}
#include "tcpinterface.h"
#include "clientui.h"
TcpInterface::TcpInterface(QObject *parent) : QObject(parent)
{
ClientUI *cui = new ClientUI();
connect(cui, SIGNAL(messageToSend(QString,QByteArray&)), this, SLOT(sendToTcpConnection(QString,QByteArray&)), Qt::DirectConnection);
}
void TcpInterface::sendMessageViaTcpConnection()
{
}
void TcpInterface::sendToTcpConnection(QString ipAddress, QByteArray &message)
{
emit sendMessageToServer(ipAddress, message);
}
To copy to clipboard, switch view to plain text mode
#ifndef TCPCONNECTION_H
#define TCPCONNECTION_H
#include <QObject>
#include <QTcpSocket>
class TcpConnection
: public QObject{
Q_OBJECT
public:
explicit TcpConnection
(QObject *parent
= 0);
signals:
public slots:
void tcpConnected();
void tcpReadyRead();
void tcpDisconnected();
void sendItOver
(QString ipAddress, QByteArray
& msg
);
protected:
};
#endif // TCPCONNECTION_H
#ifndef TCPCONNECTION_H
#define TCPCONNECTION_H
#include <QObject>
#include <QTcpSocket>
class TcpConnection : public QObject
{
Q_OBJECT
public:
explicit TcpConnection(QObject *parent = 0);
signals:
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
#include "tcpconnection.h"
#include "tcpinterface.h"
{
connect(m_socket, SIGNAL(connected()), this, SLOT(tcpConnected()));
connect(m_socket, SIGNAL(readyRead()), this, SLOT(tcpReadyRead()));
connect(m_socket, SIGNAL(disconnected()), this, SLOT(tcpDisconnected()));
TcpInterface *ti = new TcpInterface();
connect(ti,
SIGNAL(sendMessageToServer
(QString,QByteArray
&)),
this,
SLOT(sendItOver
(QString,QByteArray
&)), Qt
::DirectConnection);
}
void TcpConnection::tcpConnected()
{
qDebug() << "Ui Client connected with a Server";
}
void TcpConnection::tcpReadyRead()
{
qDebug() << "Ui Client ready to read for incoming data from Server";
}
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 ";
m_socket->write(msg);
}
}
#include "tcpconnection.h"
#include "tcpinterface.h"
TcpConnection::TcpConnection(QObject *parent) : QObject(parent)
{
m_socket = new QTcpSocket(this);
connect(m_socket, SIGNAL(connected()), this, SLOT(tcpConnected()));
connect(m_socket, SIGNAL(readyRead()), this, SLOT(tcpReadyRead()));
connect(m_socket, SIGNAL(disconnected()), this, SLOT(tcpDisconnected()));
TcpInterface *ti = new TcpInterface();
connect(ti, SIGNAL(sendMessageToServer(QString,QByteArray&)), this, SLOT(sendItOver(QString,QByteArray&)), Qt::DirectConnection);
}
void TcpConnection::tcpConnected()
{
qDebug() << "Ui Client connected with a Server";
}
void TcpConnection::tcpReadyRead()
{
qDebug() << "Ui Client ready to read for incoming data from Server";
}
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 ";
m_socket->write(msg);
}
}
To copy to clipboard, switch view to plain text mode
Many Thanks,
Rahul
Bookmarks