RahulY
24th July 2015, 14:55
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[])
{
QApplication a(argc, argv);
ClientUI w;
w.show();
return a.exec();
}
#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
#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();
}
#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
#include "messageengine.h"
MessageEngine::MessageEngine(QObject *parent) : QObject(parent)
{
}
QByteArray MessageEngine::sendAssociate()
{
QByteArray dataOut = "In this section created from some creator";
return dataOut;
}
#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
#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);
}
#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
#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);
}
}
Many Thanks,
Rahul
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[])
{
QApplication a(argc, argv);
ClientUI w;
w.show();
return a.exec();
}
#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
#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();
}
#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
#include "messageengine.h"
MessageEngine::MessageEngine(QObject *parent) : QObject(parent)
{
}
QByteArray MessageEngine::sendAssociate()
{
QByteArray dataOut = "In this section created from some creator";
return dataOut;
}
#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
#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);
}
#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
#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);
}
}
Many Thanks,
Rahul