PDA

View Full Version : QtDialog Widget event slot sending a SIGNAL to another interface class not working



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

anda_skoa
25th July 2015, 10:57
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!


How do you know that it wouldn't work, you only created a ClientUI object and have no signal/slot connections other then the implicit one from the button to its signal handler slot.

Cheers,
_

RahulY
25th July 2015, 11:41
I have updated the code and please advice me how make it signals emit between each others.


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[])
{
QApplication a(argc, argv);
ClientUI w;
w.show();


return a.exec();
}




//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




//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);
}




//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




//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;

}





//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 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);
}




//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




//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();
}
}

}




Many Thanks,
Rahul

anda_skoa
25th July 2015, 12:58
Before you attempt more random modifications, take a step back and consider when to create which object.

You create a ClientUI obejct in main(), which looks good.
Now take it from there.

Randomly adding code into methods that never get called will not get you any closer to a solution, only frustrate people here on the forum who could help you once you run into actual problems.

Cheers,
_

RahulY
26th July 2015, 23:43
Thank you for the reply and I am in the early stage of Qt SIGNAL and SLOT understanding.
After your comments, I have gone through Qt Documentation and corrected my code and now I have Error shown as below:-

The inferior stopped because it received a signal from the operating system.

Signal name :
SIGSEGV
Signal meaning :
Segmentation fault

Can you please tell me what I am doing wrong here..

Kind Regards,
Rahul


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[])
{
QApplication a(argc, argv);
ClientUi w;
w.show();

return a.exec();
}





#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



#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();
}



#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



#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;
}



#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



#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);
}
}




Many Thanks,
Rahul

RahulY
27th July 2015, 10:44
I have fixed it, thank you anda_skoa for the help to make me Qt SIGNAL and SLOT understanding.


Cheers,
Rahul