Results 1 to 6 of 6

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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
  •  
Qt is a trademark of The Qt Company.