Results 1 to 6 of 6

Thread: QtDialog Widget event slot sending a SIGNAL to another interface class not working

  1. #1
    Join Date
    Jul 2015
    Posts
    8
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default 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:-
    Qt Code:
    1. #include "clientui.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. ClientUI w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef CLIENTUI_H
    2. #define CLIENTUI_H
    3.  
    4. #include <QDialog>
    5.  
    6. namespace Ui {
    7. class ClientUI;
    8. }
    9.  
    10. class ClientUI : public QDialog
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit ClientUI(QWidget *parent = 0);
    16. ~ClientUI();
    17.  
    18. void messageToSendOver();
    19. signals:
    20. void messageToSend(QString ipAddress, QByteArray& msg);
    21.  
    22. private slots:
    23. void on_sendBtn_clicked();
    24.  
    25. private:
    26. Ui::ClientUI *ui;
    27. QList<QString> sList;
    28. };
    29.  
    30. #endif // CLIENTUI_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "clientui.h"
    2. #include "ui_clientui.h"
    3. #include "messageengine.h"
    4.  
    5. ClientUI::ClientUI(QWidget *parent) :
    6. QDialog(parent),
    7. ui(new Ui::ClientUI)
    8. {
    9. ui->setupUi(this);
    10. sList.append("Chat");
    11. sList.append("Mail");
    12. sList.append("Real time Stream");
    13. ui->listBox->addItems(sList);
    14. }
    15.  
    16. ClientUI::~ClientUI()
    17. {
    18. delete ui;
    19. }
    20.  
    21. void ClientUI::messageToSendOver()
    22. {
    23. MessageEngine *msg = new MessageEngine();
    24. QByteArray dataOut = msg->sendAssociate();
    25.  
    26. emit messageToSend("127.0.0.1", dataOut);
    27.  
    28. ui->textBrowser->append("message action from UI");
    29. }
    30.  
    31. void ClientUI::on_sendBtn_clicked()
    32. {
    33. messageToSendOver();
    34. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef MESSAGEENGINE_H
    2. #define MESSAGEENGINE_H
    3.  
    4. #include <QObject>
    5.  
    6. class MessageEngine : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit MessageEngine(QObject *parent = 0);
    11.  
    12. QByteArray sendAssociate();
    13. signals:
    14.  
    15. public slots:
    16. };
    17.  
    18. #endif // MESSAGEENGINE_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "messageengine.h"
    2.  
    3. MessageEngine::MessageEngine(QObject *parent) : QObject(parent)
    4. {
    5.  
    6. }
    7.  
    8. QByteArray MessageEngine::sendAssociate()
    9. {
    10. QByteArray dataOut = "In this section created from some creator";
    11. return dataOut;
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef TCPINTERFACE_H
    2. #define TCPINTERFACE_H
    3.  
    4. #include <QObject>
    5.  
    6. class TcpInterface : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit TcpInterface(QObject *parent = 0);
    11.  
    12. void sendMessageViaTcpConnection();
    13. signals:
    14. void sendMessageToServer(QString ipAddres, QByteArray& message);
    15. void receivedMessageFromServer(QByteArray& message);
    16.  
    17. public slots:
    18. void sendToTcpConnection(QString ipAddress, QByteArray& message);
    19. };
    20.  
    21. #endif // TCPINTERFACE_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "tcpinterface.h"
    2. #include "clientui.h"
    3.  
    4. TcpInterface::TcpInterface(QObject *parent) : QObject(parent)
    5. {
    6. ClientUI *cui = new ClientUI();
    7. connect(cui, SIGNAL(messageToSend(QString,QByteArray&)), this, SLOT(sendToTcpConnection(QString,QByteArray&)), Qt::DirectConnection);
    8. }
    9.  
    10. void TcpInterface::sendMessageViaTcpConnection()
    11. {
    12.  
    13. }
    14.  
    15. void TcpInterface::sendToTcpConnection(QString ipAddress, QByteArray &message)
    16. {
    17. emit sendMessageToServer(ipAddress, message);
    18. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef TCPCONNECTION_H
    2. #define TCPCONNECTION_H
    3.  
    4. #include <QObject>
    5. #include <QTcpSocket>
    6.  
    7. class TcpConnection : public QObject
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit TcpConnection(QObject *parent = 0);
    12.  
    13. signals:
    14.  
    15. public slots:
    16. void tcpConnected();
    17. void tcpReadyRead();
    18. void tcpDisconnected();
    19. void sendItOver(QString ipAddress, QByteArray& msg);
    20.  
    21. protected:
    22. QTcpSocket *m_socket;
    23. };
    24.  
    25. #endif // TCPCONNECTION_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "tcpconnection.h"
    2. #include "tcpinterface.h"
    3.  
    4. TcpConnection::TcpConnection(QObject *parent) : QObject(parent)
    5. {
    6. m_socket = new QTcpSocket(this);
    7. connect(m_socket, SIGNAL(connected()), this, SLOT(tcpConnected()));
    8. connect(m_socket, SIGNAL(readyRead()), this, SLOT(tcpReadyRead()));
    9. connect(m_socket, SIGNAL(disconnected()), this, SLOT(tcpDisconnected()));
    10.  
    11. TcpInterface *ti = new TcpInterface();
    12. connect(ti, SIGNAL(sendMessageToServer(QString,QByteArray&)), this, SLOT(sendItOver(QString,QByteArray&)), Qt::DirectConnection);
    13. }
    14.  
    15. void TcpConnection::tcpConnected()
    16. {
    17. qDebug() << "Ui Client connected with a Server";
    18. }
    19.  
    20. void TcpConnection::tcpReadyRead()
    21. {
    22. qDebug() << "Ui Client ready to read for incoming data from Server";
    23. }
    24.  
    25. void TcpConnection::tcpDisconnected()
    26. {
    27. qDebug() << "Ui Client disconnected from Server";
    28. m_socket->close();
    29. }
    30.  
    31. void TcpConnection::sendItOver(QString ipAddress, QByteArray &msg)
    32. {
    33. m_socket->connectToHost(ipAddress, 6000);
    34. if(!m_socket->waitForConnected(3000))
    35. {
    36. qDebug() << "Connection error: " << m_socket->errorString();
    37. }
    38. else
    39. {
    40. qDebug() << "Connected to server ";
    41. m_socket->write(msg);
    42. }
    43.  
    44. }
    To copy to clipboard, switch view to plain text mode 

    Many Thanks,
    Rahul
    Last edited by anda_skoa; 25th July 2015 at 09:52. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QtDialog Widget event slot sending a SIGNAL to another interface class not workin

    Quote Originally Posted by RahulY View Post
    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,
    _

  3. #3
    Join Date
    Jul 2015
    Posts
    8
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Post 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 View Post
    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:-
    Qt Code:
    1. //main Ui view
    2.  
    3. #include "clientui.h"
    4. #include <QApplication>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9. ClientUI w;
    10. w.show();
    11.  
    12.  
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //Client UI class header
    2.  
    3. #ifndef CLIENTUI_H
    4. #define CLIENTUI_H
    5.  
    6. #include <QDialog>
    7.  
    8. namespace Ui {
    9. class ClientUI;
    10. }
    11.  
    12. class ClientUI : public QDialog
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit ClientUI(QWidget *parent = 0);
    18. ~ClientUI();
    19.  
    20. void messageToSendOver();
    21. public slots:
    22. void receivedMessage(QByteArray& msg);
    23. signals:
    24. void messageToInterface(QString ipAddress, QByteArray& msg);
    25.  
    26. private slots:
    27. void on_sendBtn_clicked();
    28.  
    29. private:
    30. Ui::ClientUI *ui;
    31. QList<QString> sList;
    32. };
    33.  
    34. #endif // CLIENTUI_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //Client UI class cpp
    2.  
    3. #include "clientui.h"
    4. #include "ui_clientui.h"
    5. #include "messageengine.h"
    6. #include "tcpconnection.h"
    7. #include "tcpinterface.h"
    8.  
    9. ClientUI::ClientUI(QWidget *parent) :
    10. QDialog(parent),
    11. ui(new Ui::ClientUI)
    12. {
    13. ui->setupUi(this);
    14. sList.append("Chat");
    15. sList.append("Mail");
    16. sList.append("Real time Stream");
    17.  
    18. ui->listBox->addItems(sList);
    19. }
    20.  
    21. ClientUI::~ClientUI()
    22. {
    23. delete ui;
    24. }
    25.  
    26. void ClientUI::messageToSendOver()
    27. {
    28. MessageEngine *msg = new MessageEngine();
    29. QByteArray dataOut = msg->sendAssociate();
    30.  
    31. if(dataOut.isEmpty())
    32. {
    33. ui->textBrowser->append("Error on message creation");
    34. }
    35. else
    36. {
    37.  
    38. emit messageToInterface("127.0.0.1", dataOut);
    39.  
    40. ui->textBrowser->append("message action from UI");
    41. }
    42.  
    43. }
    44.  
    45. void ClientUI::sendToMessageEngine()
    46. {
    47. TcpInterface *ti = new TcpInterface();
    48. connect(ti, SIGNAL(sendMessageToUI(QByteArray&)), this, SLOT(receivedMessage(QByteArray&)), Qt::DirectConnection);
    49. }
    50.  
    51. void ClientUI::on_sendBtn_clicked()
    52. {
    53. messageToSendOver();
    54.  
    55. }
    56.  
    57. void ClientUI::receivedMessage(QByteArray &msg)
    58. {
    59. qDebug() << "Incoming Message on UI: " << msg;
    60. ui->textBrowser->append("Incoming Message: " + msg);
    61. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //message engine header
    2. #ifndef MESSAGEENGINE_H
    3. #define MESSAGEENGINE_H
    4.  
    5. #include <QObject>
    6.  
    7. class MessageEngine : public QObject
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit MessageEngine(QObject *parent = 0);
    12.  
    13. QByteArray sendAssociate();
    14.  
    15. signals:
    16.  
    17. public slots:
    18. };
    19.  
    20. #endif // MESSAGEENGINE_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //message engine CPP
    2. #include "messageengine.h"
    3.  
    4. MessageEngine::MessageEngine(QObject *parent) : QObject(parent)
    5. {
    6.  
    7. }
    8.  
    9. QByteArray MessageEngine::sendAssociate()
    10. {
    11. QByteArray dataOut = "In this section created from some creator";
    12. return dataOut;
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //tcpInterface header
    2.  
    3. #ifndef TCPINTERFACE_H
    4. #define TCPINTERFACE_H
    5.  
    6. #include <QObject>
    7.  
    8. class TcpInterface : public QObject
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit TcpInterface(QObject *parent = 0);
    13.  
    14. signals:
    15. void sendMessageToServer(QString ipAddres, QByteArray& message);
    16. void sendMessageToUI(QByteArray& message);
    17.  
    18. public slots:
    19. void sendToTcpConnection(QString ipAddress, QByteArray& message);
    20. void receivedFromTcpConnection(QByteArray& message);
    21. };
    22.  
    23. #endif // TCPINTERFACE_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //tcpinterface cpp
    2. #include "tcpinterface.h"
    3. #include "clientui.h"
    4. #include "tcpconnection.h"
    5.  
    6. TcpInterface::TcpInterface(QObject *parent) : QObject(parent)
    7. {
    8. ClientUI *cui = new ClientUI();
    9. connect(cui, SIGNAL(messageToInterface(QString,QByteArray&)), this, SLOT(sendToTcpConnection(QString,QByteArray&)), Qt::DirectConnection);
    10. TcpConnection *tc = new TcpConnection();
    11. connect(tc, SIGNAL(receivedMessagefromServer(QByteArray&)), this, SLOT(receivedFromTcpConnection(QByteArray&)), Qt::DirectConnection);
    12.  
    13. }
    14.  
    15.  
    16. void TcpInterface::sendToTcpConnection(QString ipAddress, QByteArray &message)
    17. {
    18. emit sendMessageToServer(ipAddress, message);
    19. }
    20.  
    21. void TcpInterface::receivedFromTcpConnection(QByteArray &message)
    22. {
    23. emit sendMessageToUI(message);
    24. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //tcpconnection header
    2.  
    3. #ifndef TCPCONNECTION_H
    4. #define TCPCONNECTION_H
    5.  
    6. #include <QObject>
    7. #include <QTcpSocket>
    8.  
    9. class TcpConnection : public QObject
    10. {
    11. Q_OBJECT
    12. public:
    13. explicit TcpConnection(QObject *parent = 0);
    14.  
    15. signals:
    16. // void sendToMessage(QByteArray &msg);
    17. void receivedMessagefromServer(QByteArray &msg);
    18.  
    19. public slots:
    20. void tcpConnected();
    21. void tcpReadyRead();
    22. void tcpDisconnected();
    23. void sendItOver(QString ipAddress, QByteArray& msg);
    24.  
    25. protected:
    26. QTcpSocket *m_socket;
    27. };
    28.  
    29. #endif // TCPCONNECTION_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //tcpconnection cpp
    2.  
    3. #include "tcpconnection.h"
    4. #include "tcpinterface.h"
    5. #include "clientui.h"
    6.  
    7. TcpConnection::TcpConnection(QObject *parent) : QObject(parent)
    8. {
    9. m_socket = new QTcpSocket(this);
    10.  
    11. TcpInterface *ti = new TcpInterface();
    12. connect(ti, SIGNAL(sendMessageToServer(QString,QByteArray&)), this, SLOT(sendItOver(QString,QByteArray&)), Qt::DirectConnection);
    13.  
    14. connect(m_socket, SIGNAL(connected()), this, SLOT(tcpConnected()));
    15. connect(m_socket, SIGNAL(readyRead()), this, SLOT(tcpReadyRead()));
    16. connect(m_socket, SIGNAL(disconnected()), this, SLOT(tcpDisconnected()));
    17. }
    18.  
    19. void TcpConnection::tcpConnected()
    20. {
    21. qDebug() << "Ui Client connected with a Server";
    22. }
    23.  
    24. void TcpConnection::tcpReadyRead()
    25. {
    26. qDebug() << "Ui Client ready to read for incoming data from Server";
    27. QByteArray dataIn = m_socket->readAll();
    28. qDebug() << "Incoming message" << dataIn.toHex();
    29. emit receivedMessagefromServer(dataIn);
    30. }
    31.  
    32. void TcpConnection::tcpDisconnected()
    33. {
    34. qDebug() << "Ui Client disconnected from Server";
    35. m_socket->close();
    36. }
    37.  
    38.  
    39. void TcpConnection::sendItOver(QString ipAddress, QByteArray &msg)
    40. {
    41. m_socket->connectToHost(ipAddress, 6000);
    42. if(!m_socket->waitForConnected(3000))
    43. {
    44. qDebug() << "Connection error: " << m_socket->errorString();
    45. }
    46. else
    47. {
    48. qDebug() << "Connected to server ";
    49. if(m_socket->bytesAvailable())
    50. {
    51. m_socket->write(msg);
    52. if(!m_socket->waitForBytesWritten(30000))
    53. {
    54. qDebug() << "Error occured: " << m_socket->errorString();
    55. }
    56. }
    57. else
    58. {
    59. qDebug() << "Error occured: " << m_socket->errorString();
    60. }
    61. }
    62.  
    63. }
    To copy to clipboard, switch view to plain text mode 



    Many Thanks,
    Rahul

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  5. #5
    Join Date
    Jul 2015
    Posts
    8
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Post 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 View Post
    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:-
    Qt Code:
    1. #include "clientui.h"
    2. #include <QApplication>
    3. #include "../SytemsLogs/easylogging++.h"
    4.  
    5.  
    6. INITIALIZE_EASYLOGGINGPP
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication a(argc, argv);
    11. ClientUi w;
    12. w.show();
    13.  
    14. return a.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef CLIENTUI_H
    2. #define CLIENTUI_H
    3.  
    4. #include <QWidget>
    5. #include <QDebug>
    6. #include "mdsclient.h"
    7. #include <QtConcurrent/QtConcurrent>
    8. #include "tcpcommunication.h"
    9.  
    10. namespace Ui {
    11. class ClientUi;
    12. }
    13.  
    14. class ClientUi : public QWidget
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19. explicit ClientUi(QWidget *parent = 0);
    20. ~ClientUi();
    21.  
    22. signals:
    23. void sendCommunicationMessage(QString ipAddr, QByteArray& message);
    24.  
    25. public slots:
    26. void sendServerMessage(QString ipAddr, QByteArray& message);
    27.  
    28. void receivedServerMessage(QByteArray& msg);
    29.  
    30. private slots:
    31. void on_powerBtn_clicked();
    32.  
    33. void on_sendBtn_clicked();
    34. protected:
    35. MdsClient mds;
    36. TcpCommunication tc;
    37.  
    38. private:
    39. Ui::ClientUi *ui;
    40. QList<QString> m_list;
    41. };
    42.  
    43. #endif // CLIENTUI_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "clientui.h"
    2. #include "ui_clientui.h"
    3.  
    4. ClientUi::ClientUi(QWidget *parent) :
    5. QWidget(parent),
    6. ui(new Ui::ClientUi)
    7. {
    8. ui->setupUi(this);
    9. m_list.append("Associate");
    10. m_list.append("Update");
    11.  
    12. ui->messageTypeCom->addItems(m_list);
    13. }
    14.  
    15. ClientUi::~ClientUi()
    16. {
    17. delete ui;
    18. }
    19.  
    20. void ClientUi::sendServerMessage(QString ipAddr, QByteArray &message)
    21. {
    22. qDebug() << "Sender IP addres: " << ipAddr << "Message: "<< message;
    23. emit sendCommunicationMessage(ipAddr, message);
    24. }
    25.  
    26. void ClientUi::receivedServerMessage(QByteArray &msg)
    27. {
    28. ui->textBrowser->append("Received server Message: " + msg);
    29. }
    30. void ClientUi::on_powerBtn_clicked()
    31. {
    32. qDebug() << "Device powered on";
    33. ui->textBrowser->append("Device powered on");
    34. }
    35.  
    36. void ClientUi::on_sendBtn_clicked()
    37. {
    38. qDebug() << "Send button clicked";
    39. ui->textBrowser->append("Send button clicked");
    40. connect(&mds, &MdsClient::sendAssociateMessage, this, &ClientUi::sendServerMessage);
    41. connect(this, &ClientUi::sendCommunicationMessage, &tc, &TcpCommunication::clientSendMessageToServer);
    42. connect(&tc, &TcpCommunication::sendToUI, this, &ClientUi::receivedServerMessage);
    43. mds.createAssociateMessage();
    44. tc.startTcpCommunication();
    45. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef MDSCLIENT_H
    2. #define MDSCLIENT_H
    3.  
    4. #include <QObject>
    5. #include <QThread>
    6. #include <QHostAddress>
    7.  
    8.  
    9. class MdsClient : public QObject
    10. {
    11. Q_OBJECT
    12. public:
    13. explicit MdsClient(QObject *parent = 0);
    14.  
    15. void createAssociateMessage();
    16.  
    17. signals:
    18. void sendAssociateMessage(QString ipAddr, QByteArray& message);
    19.  
    20. public slots:
    21. void receivedMessage(QString ipAddr, QByteArray& msg);
    22.  
    23. };
    24.  
    25. #endif // MDSCLIENT_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "mdsclient.h"
    2.  
    3. MdsClient::MdsClient(QObject *parent) : QObject(parent)
    4. {
    5.  
    6. }
    7.  
    8.  
    9. void MdsClient::createAssociateMessage()
    10. {
    11.  
    12. //I have seperate message creator to create message
    13. QByteArray dataOut = "This message created by message creator";
    14. emit sendAssociateMessage("127.0.0.1", dataOut);
    15. QThread::currentThread()->msleep(100);
    16. qDebug() << "message creation emited back to UI";
    17. }
    18.  
    19.  
    20. void MdsClient::receivedMessage(QString ipAddr, QByteArray& msg)
    21. {
    22.  
    23. qDebug() << "Ip addres: " << ipAddr << "Message: " << msg;
    24. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef TCPCOMMUNICATION_H
    2. #define TCPCOMMUNICATION_H
    3.  
    4. #include <QObject>
    5. #include <QTcpSocket>
    6. #include "../SytemsLogs/easylogging++.h"
    7.  
    8. class TcpCommunication : public QObject
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit TcpCommunication(QObject *parent = 0);
    13.  
    14. void bytesWritten(qint64 bytes);
    15. bool newConnection(QString iAddres);
    16. void messageExchanges();
    17. void startTcpCommunication();
    18.  
    19. signals:
    20. void sendToUI(QByteArray& msg);
    21.  
    22. public slots:
    23. void clientConnected();
    24. void clientDisconnected();
    25. void clientReadyRead();
    26. void clientSendMessageToServer(const QString ipAddress, QByteArray& msg);
    27.  
    28. private:
    29. QTcpSocket *m_socket;
    30. };
    31.  
    32. #endif // TCPCOMMUNICATION_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "tcpcommunication.h"
    2.  
    3. TcpCommunication::TcpCommunication(QObject *parent) : QObject(parent)
    4. {
    5.  
    6. }
    7.  
    8. void TcpCommunication::startTcpCommunication()
    9. {
    10. m_socket = new QTcpSocket(this);
    11. connect(m_socket, SIGNAL(connected()), this, SLOT(clientConnected()));
    12. connect(m_socket,SIGNAL(disconnected()), this, SLOT(clientDisConnected()));
    13. connect(m_socket,SIGNAL(readyRead()), this, SLOT(clientReadyRead()));
    14. }
    15.  
    16. void TcpCommunication::clientConnected()
    17. {
    18. LOG(INFO) << "connected " ;
    19. }
    20.  
    21. void TcpCommunication::clientDisconnected()
    22. {
    23. LOG(INFO) << "Disconnected " ;
    24. }
    25.  
    26. void TcpCommunication::clientReadyRead()
    27. {
    28. QByteArray dataIn = m_socket->readAll();
    29. LOG(INFO) << "Data in: " << dataIn ;
    30. emit sendToUI(dataIn);
    31.  
    32. }
    33.  
    34. void TcpCommunication::clientSendMessageToServer(const QString ipAddress, QByteArray &msg)
    35. {
    36. m_socket->connectToHost(ipAddress, 6000);
    37.  
    38. if(!m_socket->waitForConnected(3000))
    39. {
    40. LOG(ERROR) << "Error occured " << m_socket->errorString();
    41. }
    42. else
    43. {
    44. LOG(INFO) << "connected " ;
    45. m_socket->write(msg);
    46. }
    47. }
    To copy to clipboard, switch view to plain text mode 

    Many Thanks,
    Rahul

  6. #6
    Join Date
    Jul 2015
    Posts
    8
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default 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

Similar Threads

  1. Sending event from child widget
    By scascio in forum Qt Programming
    Replies: 6
    Last Post: 28th September 2009, 21:18
  2. QObject signal/slot not working
    By Msnforum in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2009, 22:50
  3. Replies: 12
    Last Post: 18th September 2008, 15:04
  4. Signal Slot not working
    By munna in forum Qt Programming
    Replies: 8
    Last Post: 6th November 2006, 10:36
  5. Signal/slot looking in base class, not derived class
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 07:36

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.