QtDialog Widget event slot sending a SIGNAL to another interface class not working
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:-
Code:
#include "clientui.h"
#include <QApplication>
int main(int argc, char *argv[])
{
ClientUI w;
w.show();
return a.exec();
}
Code:
#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
Code:
#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();
}
Code:
#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
Code:
#include "messageengine.h"
{
}
{
QByteArray dataOut
= "In this section created from some creator";
return dataOut;
}
Code:
#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
Code:
#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);
}
Code:
#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
Code:
#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);
}
}
Many Thanks,
Rahul
Re: QtDialog Widget event slot sending a SIGNAL to another interface class not workin
Quote:
Originally Posted by
RahulY
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,
_
Re: QtDialog Widget event slot sending a SIGNAL to another interface class not workin
I have updated the code and please advice me how make it signals emit between each others.
Quote:
Originally Posted by
RahulY
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:-
Code:
//main Ui view
#include "clientui.h"
#include <QApplication>
int main(int argc, char *argv[])
{
ClientUI w;
w.show();
return a.exec();
}
Code:
//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
Code:
//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);
}
Code:
//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
Code:
//message engine CPP
#include "messageengine.h"
{
}
{
QByteArray dataOut
= "In this section created from some creator";
return dataOut;
}
Code:
//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
Code:
//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);
}
Code:
//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
Code:
//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();
}
}
}
Many Thanks,
Rahul
Re: QtDialog Widget event slot sending a SIGNAL to another interface class not workin
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,
_
Re: QtDialog Widget event slot sending a SIGNAL to another interface class not workin
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
Quote:
Originally Posted by
RahulY
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:-
Code:
#include "clientui.h"
#include <QApplication>
#include "../SytemsLogs/easylogging++.h"
INITIALIZE_EASYLOGGINGPP
int main(int argc, char *argv[])
{
ClientUi w;
w.show();
return a.exec();
}
Code:
#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
Code:
#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();
}
Code:
#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
Code:
#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;
}
Code:
#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
Code:
#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);
}
}
Many Thanks,
Rahul
Re: QtDialog Widget event slot sending a SIGNAL to another interface class not workin
I have fixed it, thank you anda_skoa for the help to make me Qt SIGNAL and SLOT understanding.
Cheers,
Rahul