Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QUdpSocket not emiting readyRead in a multi files project

QUdpSocket not emiting readyRead in a multi files project

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 5 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    febinaf
    wrote on last edited by febinaf
    #1

    The program is to take user input and send it via udp, and the receiver sends acknowledgement. Tried to read the acknowledgement udp using the readyRead signal. But, the signal is not emitted on arrival of package. Checked the status of bind() and it returns true, and the arrival of packet is confirmed through wireshark.
    I'm not really sure if the connect() for readyRead is always in scope (as its written within the constructor for udp class).

    //udp.h
    #ifndef UDP_H
    #define UDP_H
    
    #include <QObject>
    #include <QUdpSocket>
    class udp : public QObject
    {
        Q_OBJECT
    public:
        explicit udp(QObject *parent = nullptr);
        void udpSend();
    
    signals:
    
    public slots:
        void readUDP();
    private:
        QUdpSocket *socket;
    
    };
    
    #endif // UDP_H
    
    
    
    //udp.cpp
    #include "udp.h"
    #include "QDebug"
    #include "QUdpSocket"
    udp::udp(QObject *parent)
        : QObject{parent}
    {
        socket =  new QUdpSocket(this);
        socket->bind(QHostAddress("192.168.43.209"), 1234);
    
        if(socket->state() == QUdpSocket::BoundState)
        {
            qDebug() << "server bound";
        }
        else
        qDebug()<<"not bound";
        bool stat = connect(socket, SIGNAL(readyRead()), this, SLOT(readUDP()));
        qDebug()<<"connect status"<<stat;
    }
    
    
    
    void udp::udpSend()
    {
        QByteArray buffer;
        buffer.append("Hello hey ..... \n I'm up and running");
    
        socket->writeDatagram(buffer, QHostAddress("192.168.43.1"), 1234 );
    }
    
    void udp::readUDP()
    {
        if(socket->hasPendingDatagrams())
            qDebug()<<"data received";
        QByteArray Buffer;
        Buffer.resize(socket->pendingDatagramSize());
        QHostAddress sender;
        quint16 port;
        socket->readDatagram(Buffer.data(), Buffer.size(), &sender, &port);
        qDebug()<<"message is"<<Buffer;
    }
    
    //adduser.cpp (where the object is created and send is called)
    
    #include "adduser.h"
    #include "ui_adduser.h"
    #include "QMessageBox"
    #include "QDebug"
    #include "udp.h"
    
    addUser::addUser(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::addUser)
    {
        ui->setupUi(this);
    }
    
    addUser::~addUser()
    {
        delete ui;
    }
    
    void addUser::on_pushButton_2_clicked() //submit button press
    {
        udp myudp; //UDP object creation
        addUser User;
        QMessageBox mssgBox;
        addUserName = ui->lineEdit->text();
        addNewID = ui->lineEdit_2->text();
        addRFID = ui->lineEdit_3->text();
        IP = ui->lineEdit_4->text();
        port = ui->lineEdit_5->text();
    
        portNum = port.toInt();
    
        int rfidLength = addRFID.length();
        int lengthOfName = addUserName.length();
        strLength = QString::number(lengthOfName); // number to string
    
        if((rfidLength == 10) && (lengthOfName <11))
        {
    
    
            mssgBox.setWindowTitle("Confirmation");
            mssgBox.setText("Confirm the new user details" );
            mssgBox.setInformativeText("\n username: "+addUserName+"\n user ID: "+addNewID+"\n RFID: "+addRFID);
            mssgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Save);
            int returnValue = mssgBox.exec();
    
    //        QDeadlineTimer deadline(5000);
    
    
    
            switch(returnValue)
            {
    
                case QMessageBox::Save :
                    {
                    //packetize and send the details over UDP
                    packetToDevice = "FE0A"+addNewID+strLength+addUserName+addRFID+"0B";
                    qDebug()<<"packet generated";
    
                    myudp.udpSend(); //(packetToDevice, IP, portNum); //UDP SEND Fn CALL
                    
    
                    qDebug()<<"user pressed save";
                    User.show(); // back to the window
                    break;
                    }
                case QMessageBox::Cancel :
                    //continue to the user addition
                    qDebug()<<"user pressed cancel";
                    User.show(); //back to window
                    break;
                default:
                    qDebug()<<"entered default state";
            }
    
    
            qDebug()<<addUserName;
            qDebug()<<packetToDevice;
        }
    
        else
        {
            QMessageBox falseMssg;
            falseMssg.setWindowTitle("Error");
            falseMssg.setText("Invalid RFID/Name");
            falseMssg.setInformativeText("RFID must be 10 characters and Name should contain up to 10 characters");
            falseMssg.setStandardButtons(QMessageBox::Close);
            falseMssg.exec();
    
            addUser User;
            User.show();
    
        }
    
    }
    

    I've tried to create an object in the main.cpp and then create another one in the adduser.cpp to call the sending function, but the issue still persists.

    The same udp source and header when used in another project with just a main.cpp file and object creation in main.cpp works fine

    JonBJ jsulmJ 2 Replies Last reply
    0
    • F febinaf

      The program is to take user input and send it via udp, and the receiver sends acknowledgement. Tried to read the acknowledgement udp using the readyRead signal. But, the signal is not emitted on arrival of package. Checked the status of bind() and it returns true, and the arrival of packet is confirmed through wireshark.
      I'm not really sure if the connect() for readyRead is always in scope (as its written within the constructor for udp class).

      //udp.h
      #ifndef UDP_H
      #define UDP_H
      
      #include <QObject>
      #include <QUdpSocket>
      class udp : public QObject
      {
          Q_OBJECT
      public:
          explicit udp(QObject *parent = nullptr);
          void udpSend();
      
      signals:
      
      public slots:
          void readUDP();
      private:
          QUdpSocket *socket;
      
      };
      
      #endif // UDP_H
      
      
      
      //udp.cpp
      #include "udp.h"
      #include "QDebug"
      #include "QUdpSocket"
      udp::udp(QObject *parent)
          : QObject{parent}
      {
          socket =  new QUdpSocket(this);
          socket->bind(QHostAddress("192.168.43.209"), 1234);
      
          if(socket->state() == QUdpSocket::BoundState)
          {
              qDebug() << "server bound";
          }
          else
          qDebug()<<"not bound";
          bool stat = connect(socket, SIGNAL(readyRead()), this, SLOT(readUDP()));
          qDebug()<<"connect status"<<stat;
      }
      
      
      
      void udp::udpSend()
      {
          QByteArray buffer;
          buffer.append("Hello hey ..... \n I'm up and running");
      
          socket->writeDatagram(buffer, QHostAddress("192.168.43.1"), 1234 );
      }
      
      void udp::readUDP()
      {
          if(socket->hasPendingDatagrams())
              qDebug()<<"data received";
          QByteArray Buffer;
          Buffer.resize(socket->pendingDatagramSize());
          QHostAddress sender;
          quint16 port;
          socket->readDatagram(Buffer.data(), Buffer.size(), &sender, &port);
          qDebug()<<"message is"<<Buffer;
      }
      
      //adduser.cpp (where the object is created and send is called)
      
      #include "adduser.h"
      #include "ui_adduser.h"
      #include "QMessageBox"
      #include "QDebug"
      #include "udp.h"
      
      addUser::addUser(QWidget *parent) :
          QDialog(parent),
          ui(new Ui::addUser)
      {
          ui->setupUi(this);
      }
      
      addUser::~addUser()
      {
          delete ui;
      }
      
      void addUser::on_pushButton_2_clicked() //submit button press
      {
          udp myudp; //UDP object creation
          addUser User;
          QMessageBox mssgBox;
          addUserName = ui->lineEdit->text();
          addNewID = ui->lineEdit_2->text();
          addRFID = ui->lineEdit_3->text();
          IP = ui->lineEdit_4->text();
          port = ui->lineEdit_5->text();
      
          portNum = port.toInt();
      
          int rfidLength = addRFID.length();
          int lengthOfName = addUserName.length();
          strLength = QString::number(lengthOfName); // number to string
      
          if((rfidLength == 10) && (lengthOfName <11))
          {
      
      
              mssgBox.setWindowTitle("Confirmation");
              mssgBox.setText("Confirm the new user details" );
              mssgBox.setInformativeText("\n username: "+addUserName+"\n user ID: "+addNewID+"\n RFID: "+addRFID);
              mssgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Save);
              int returnValue = mssgBox.exec();
      
      //        QDeadlineTimer deadline(5000);
      
      
      
              switch(returnValue)
              {
      
                  case QMessageBox::Save :
                      {
                      //packetize and send the details over UDP
                      packetToDevice = "FE0A"+addNewID+strLength+addUserName+addRFID+"0B";
                      qDebug()<<"packet generated";
      
                      myudp.udpSend(); //(packetToDevice, IP, portNum); //UDP SEND Fn CALL
                      
      
                      qDebug()<<"user pressed save";
                      User.show(); // back to the window
                      break;
                      }
                  case QMessageBox::Cancel :
                      //continue to the user addition
                      qDebug()<<"user pressed cancel";
                      User.show(); //back to window
                      break;
                  default:
                      qDebug()<<"entered default state";
              }
      
      
              qDebug()<<addUserName;
              qDebug()<<packetToDevice;
          }
      
          else
          {
              QMessageBox falseMssg;
              falseMssg.setWindowTitle("Error");
              falseMssg.setText("Invalid RFID/Name");
              falseMssg.setInformativeText("RFID must be 10 characters and Name should contain up to 10 characters");
              falseMssg.setStandardButtons(QMessageBox::Close);
              falseMssg.exec();
      
              addUser User;
              User.show();
      
          }
      
      }
      

      I've tried to create an object in the main.cpp and then create another one in the adduser.cpp to call the sending function, but the issue still persists.

      The same udp source and header when used in another project with just a main.cpp file and object creation in main.cpp works fine

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @febinaf said in QUdpSocket not emiting readyRead in a multi files project:

      I've tried to create an object in the main.cpp and then create another one in the adduser.cpp to call the sending function, but the issue still persists.

      I'm not sure what you are doing here. In addUser::on_pushButton_2_clicked() you create a local udp myudp; //UDP object creation and send on that, but that goes out of scope at the end of that function. How do you expect to receive the datagrams then?

      If you create a udp instance somewhere you should make it available to wherever it is to be used. You might do that by passing it as a parameter to addUser class, or as a singleton or whatever, but I don't think you want to be creating more than one instance as it encapsulates your UDP socket.

      Also, separately but similarly, at least in the error case I notice

              addUser User;
              User.show();
      

      That creates a new addUser instance inside addUser::on_pushButton_2_clicked() which is already an adduser instance. Then it shows it, which requires it to persist, but it immediately goes out of scope and gets destroyed.

      F 1 Reply Last reply
      3
      • F febinaf

        The program is to take user input and send it via udp, and the receiver sends acknowledgement. Tried to read the acknowledgement udp using the readyRead signal. But, the signal is not emitted on arrival of package. Checked the status of bind() and it returns true, and the arrival of packet is confirmed through wireshark.
        I'm not really sure if the connect() for readyRead is always in scope (as its written within the constructor for udp class).

        //udp.h
        #ifndef UDP_H
        #define UDP_H
        
        #include <QObject>
        #include <QUdpSocket>
        class udp : public QObject
        {
            Q_OBJECT
        public:
            explicit udp(QObject *parent = nullptr);
            void udpSend();
        
        signals:
        
        public slots:
            void readUDP();
        private:
            QUdpSocket *socket;
        
        };
        
        #endif // UDP_H
        
        
        
        //udp.cpp
        #include "udp.h"
        #include "QDebug"
        #include "QUdpSocket"
        udp::udp(QObject *parent)
            : QObject{parent}
        {
            socket =  new QUdpSocket(this);
            socket->bind(QHostAddress("192.168.43.209"), 1234);
        
            if(socket->state() == QUdpSocket::BoundState)
            {
                qDebug() << "server bound";
            }
            else
            qDebug()<<"not bound";
            bool stat = connect(socket, SIGNAL(readyRead()), this, SLOT(readUDP()));
            qDebug()<<"connect status"<<stat;
        }
        
        
        
        void udp::udpSend()
        {
            QByteArray buffer;
            buffer.append("Hello hey ..... \n I'm up and running");
        
            socket->writeDatagram(buffer, QHostAddress("192.168.43.1"), 1234 );
        }
        
        void udp::readUDP()
        {
            if(socket->hasPendingDatagrams())
                qDebug()<<"data received";
            QByteArray Buffer;
            Buffer.resize(socket->pendingDatagramSize());
            QHostAddress sender;
            quint16 port;
            socket->readDatagram(Buffer.data(), Buffer.size(), &sender, &port);
            qDebug()<<"message is"<<Buffer;
        }
        
        //adduser.cpp (where the object is created and send is called)
        
        #include "adduser.h"
        #include "ui_adduser.h"
        #include "QMessageBox"
        #include "QDebug"
        #include "udp.h"
        
        addUser::addUser(QWidget *parent) :
            QDialog(parent),
            ui(new Ui::addUser)
        {
            ui->setupUi(this);
        }
        
        addUser::~addUser()
        {
            delete ui;
        }
        
        void addUser::on_pushButton_2_clicked() //submit button press
        {
            udp myudp; //UDP object creation
            addUser User;
            QMessageBox mssgBox;
            addUserName = ui->lineEdit->text();
            addNewID = ui->lineEdit_2->text();
            addRFID = ui->lineEdit_3->text();
            IP = ui->lineEdit_4->text();
            port = ui->lineEdit_5->text();
        
            portNum = port.toInt();
        
            int rfidLength = addRFID.length();
            int lengthOfName = addUserName.length();
            strLength = QString::number(lengthOfName); // number to string
        
            if((rfidLength == 10) && (lengthOfName <11))
            {
        
        
                mssgBox.setWindowTitle("Confirmation");
                mssgBox.setText("Confirm the new user details" );
                mssgBox.setInformativeText("\n username: "+addUserName+"\n user ID: "+addNewID+"\n RFID: "+addRFID);
                mssgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Save);
                int returnValue = mssgBox.exec();
        
        //        QDeadlineTimer deadline(5000);
        
        
        
                switch(returnValue)
                {
        
                    case QMessageBox::Save :
                        {
                        //packetize and send the details over UDP
                        packetToDevice = "FE0A"+addNewID+strLength+addUserName+addRFID+"0B";
                        qDebug()<<"packet generated";
        
                        myudp.udpSend(); //(packetToDevice, IP, portNum); //UDP SEND Fn CALL
                        
        
                        qDebug()<<"user pressed save";
                        User.show(); // back to the window
                        break;
                        }
                    case QMessageBox::Cancel :
                        //continue to the user addition
                        qDebug()<<"user pressed cancel";
                        User.show(); //back to window
                        break;
                    default:
                        qDebug()<<"entered default state";
                }
        
        
                qDebug()<<addUserName;
                qDebug()<<packetToDevice;
            }
        
            else
            {
                QMessageBox falseMssg;
                falseMssg.setWindowTitle("Error");
                falseMssg.setText("Invalid RFID/Name");
                falseMssg.setInformativeText("RFID must be 10 characters and Name should contain up to 10 characters");
                falseMssg.setStandardButtons(QMessageBox::Close);
                falseMssg.exec();
        
                addUser User;
                User.show();
        
            }
        
        }
        

        I've tried to create an object in the main.cpp and then create another one in the adduser.cpp to call the sending function, but the issue still persists.

        The same udp source and header when used in another project with just a main.cpp file and object creation in main.cpp works fine

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @febinaf To add to @JonB : please read https://en.cppreference.com/w/cpp/language/scope

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • JonBJ JonB

          @febinaf said in QUdpSocket not emiting readyRead in a multi files project:

          I've tried to create an object in the main.cpp and then create another one in the adduser.cpp to call the sending function, but the issue still persists.

          I'm not sure what you are doing here. In addUser::on_pushButton_2_clicked() you create a local udp myudp; //UDP object creation and send on that, but that goes out of scope at the end of that function. How do you expect to receive the datagrams then?

          If you create a udp instance somewhere you should make it available to wherever it is to be used. You might do that by passing it as a parameter to addUser class, or as a singleton or whatever, but I don't think you want to be creating more than one instance as it encapsulates your UDP socket.

          Also, separately but similarly, at least in the error case I notice

                  addUser User;
                  User.show();
          

          That creates a new addUser instance inside addUser::on_pushButton_2_clicked() which is already an adduser instance. Then it shows it, which requires it to persist, but it immediately goes out of scope and gets destroyed.

          F Offline
          F Offline
          febinaf
          wrote on last edited by
          #4

          @JonB said in QUdpSocket not emiting readyRead in a multi files project:
          ooh ok, but I'm clueless on how to create a udp object in main (so that the readyRead is connected throughout the runtime), and use the same object in adduser file for sending over udp. I'll go through the suggested solutions.

          passing it as a parameter to addUser class

          could you please explain a bit more on passing the object to the class, will that help me to keep the object in scope throughout the adduser file ?

          addUser User;
                  User.show();
          

          I've used the above snippet, so that the window appears again after the QMessageBox is closed. Without that, the window used to close immediately after the message box is closed. I'm trying to take the user input again after the 'Save' or 'Cancel' button is clicked. Any info regarding the proper implementation of this idea would be really helpful.

          really appreciate the time :)

          jsulmJ 1 Reply Last reply
          0
          • F febinaf

            @JonB said in QUdpSocket not emiting readyRead in a multi files project:
            ooh ok, but I'm clueless on how to create a udp object in main (so that the readyRead is connected throughout the runtime), and use the same object in adduser file for sending over udp. I'll go through the suggested solutions.

            passing it as a parameter to addUser class

            could you please explain a bit more on passing the object to the class, will that help me to keep the object in scope throughout the adduser file ?

            addUser User;
                    User.show();
            

            I've used the above snippet, so that the window appears again after the QMessageBox is closed. Without that, the window used to close immediately after the message box is closed. I'm trying to take the user input again after the 'Save' or 'Cancel' button is clicked. Any info regarding the proper implementation of this idea would be really helpful.

            really appreciate the time :)

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @febinaf said in QUdpSocket not emiting readyRead in a multi files project:

            how to create a udp object in main

            Why in main?!
            Simply make it class member...

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            F 3 Replies Last reply
            1
            • jsulmJ jsulm

              @febinaf said in QUdpSocket not emiting readyRead in a multi files project:

              how to create a udp object in main

              Why in main?!
              Simply make it class member...

              F Offline
              F Offline
              febinaf
              wrote on last edited by
              #6

              @jsulm said in QUdpSocket not emiting readyRead in a multi files project:

              Simply make it class member...

              ok, so i need to create an object for udp and make it the class member of addUser class ... got it thanks

              1 Reply Last reply
              0
              • jsulmJ jsulm

                @febinaf said in QUdpSocket not emiting readyRead in a multi files project:

                how to create a udp object in main

                Why in main?!
                Simply make it class member...

                F Offline
                F Offline
                febinaf
                wrote on last edited by
                #7

                @jsulm Hey I've added the object for udp class in the addUser class. I've also forward declared the udp class in addUser header file. But I'm getting the error that " field 'myudp' has incomplete type 'myUDP'.
                Here is the changes that I've made.

                //addUser.h
                #ifndef ADDUSER_H
                #define ADDUSER_H
                #include <QMainWindow>
                #include <QDialog>
                #include <QString>
                
                namespace Ui {
                class addUser;
                }
                class myUDP; //fwd declared
                
                class addUser : public QDialog
                {
                    Q_OBJECT
                
                public:
                    explicit addUser(QWidget *parent = nullptr);
                    ~addUser();
                    QString packetToDevice, IP, port;
                    int portNum;
                    myUDP myudp; //object added as member
                private slots:
                    void on_pushButton_2_clicked();
                
                private:
                    Ui::addUser *ui;
                    QString addUserName, addNewID, addRFID, confirmBuffer, strLength;
                
                };
                #endif // ADDUSER_H
                

                and did #include "myudp.h" in adduser.cpp .

                Also, tried to create

                myUDP  *myudp; //object pointer
                

                and added, below line in addUser constructor

                myudp = new myUDP(this);
                

                and doing so removed the errors but, the socket -> bind() in myUDP constructor returns false during operation, and the readyRead is not emitted. Could you help me with what I'm missing here.

                Christian EhrlicherC 1 Reply Last reply
                0
                • F febinaf

                  @jsulm Hey I've added the object for udp class in the addUser class. I've also forward declared the udp class in addUser header file. But I'm getting the error that " field 'myudp' has incomplete type 'myUDP'.
                  Here is the changes that I've made.

                  //addUser.h
                  #ifndef ADDUSER_H
                  #define ADDUSER_H
                  #include <QMainWindow>
                  #include <QDialog>
                  #include <QString>
                  
                  namespace Ui {
                  class addUser;
                  }
                  class myUDP; //fwd declared
                  
                  class addUser : public QDialog
                  {
                      Q_OBJECT
                  
                  public:
                      explicit addUser(QWidget *parent = nullptr);
                      ~addUser();
                      QString packetToDevice, IP, port;
                      int portNum;
                      myUDP myudp; //object added as member
                  private slots:
                      void on_pushButton_2_clicked();
                  
                  private:
                      Ui::addUser *ui;
                      QString addUserName, addNewID, addRFID, confirmBuffer, strLength;
                  
                  };
                  #endif // ADDUSER_H
                  

                  and did #include "myudp.h" in adduser.cpp .

                  Also, tried to create

                  myUDP  *myudp; //object pointer
                  

                  and added, below line in addUser constructor

                  myudp = new myUDP(this);
                  

                  and doing so removed the errors but, the socket -> bind() in myUDP constructor returns false during operation, and the readyRead is not emitted. Could you help me with what I'm missing here.

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  And where did you define + implement myUDP class?
                  Forward declaring an object is not possible. You can use a forward-declared class only as pointer.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  F 1 Reply Last reply
                  1
                  • Christian EhrlicherC Christian Ehrlicher

                    And where did you define + implement myUDP class?
                    Forward declaring an object is not possible. You can use a forward-declared class only as pointer.

                    F Offline
                    F Offline
                    febinaf
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher I've defined the myUDP class in myudp.cpp and used in in the addUser.cpp file.

                    I did all these steps to use the myudp object as a member of the addUser class. If this is not the right thing to do, could you guide me through the steps.

                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • F febinaf

                      @Christian-Ehrlicher I've defined the myUDP class in myudp.cpp and used in in the addUser.cpp file.

                      I did all these steps to use the myudp object as a member of the addUser class. If this is not the right thing to do, could you guide me through the steps.

                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @febinaf said in QUdpSocket not emiting readyRead in a multi files project:

                      If this is not the right thing to do, could you guide me through the steps.

                      Forward declaring an object is not possible. You can use a forward-declared class only as a pointer.

                      I already told you whats wrong

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      F 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @febinaf said in QUdpSocket not emiting readyRead in a multi files project:

                        how to create a udp object in main

                        Why in main?!
                        Simply make it class member...

                        F Offline
                        F Offline
                        febinaf
                        wrote on last edited by
                        #11
                        This post is deleted!
                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          @febinaf said in QUdpSocket not emiting readyRead in a multi files project:

                          If this is not the right thing to do, could you guide me through the steps.

                          Forward declaring an object is not possible. You can use a forward-declared class only as a pointer.

                          I already told you whats wrong

                          F Offline
                          F Offline
                          febinaf
                          wrote on last edited by
                          #12

                          @Christian-Ehrlicher
                          I'm really getting into object oriented programming and pardon me if I'm asking a lot of silly questions.

                          I'm trying to achieve what @jsulm mentioned as "Simply make it class member..." . I've created an object in the addUser.h private sections and got errors, that's the reason for trying out forward declaration.
                          What I'm trying to obtain is, to make the signal readyRead() to be connected throughout the runtime, and the connect () is in the constructor of myUDP class.

                          Christian EhrlicherC 1 Reply Last reply
                          0
                          • F febinaf

                            @Christian-Ehrlicher
                            I'm really getting into object oriented programming and pardon me if I'm asking a lot of silly questions.

                            I'm trying to achieve what @jsulm mentioned as "Simply make it class member..." . I've created an object in the addUser.h private sections and got errors, that's the reason for trying out forward declaration.
                            What I'm trying to obtain is, to make the signal readyRead() to be connected throughout the runtime, and the connect () is in the constructor of myUDP class.

                            Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @febinaf said in QUdpSocket not emiting readyRead in a multi files project:

                            I've created an object in the addUser.h private sections and got errors

                            Then please post the error messages. What you did wrong with the forward declared class was already explained...

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            F 1 Reply Last reply
                            0
                            • Christian EhrlicherC Christian Ehrlicher

                              @febinaf said in QUdpSocket not emiting readyRead in a multi files project:

                              I've created an object in the addUser.h private sections and got errors

                              Then please post the error messages. What you did wrong with the forward declared class was already explained...

                              F Offline
                              F Offline
                              febinaf
                              wrote on last edited by
                              #14

                              @Christian-Ehrlicher The error was that "the class name does not name a type" .. i think it was because of not including the required header files. Got that fixed now.

                              Right now I've created an object in private section of addUser.h and then I'm calling a method from that object in between the addUser GUI. I'm using a debug message to get status of socket ->bind(), and it returns true as the window opens the addUser gui, and the same returns false when the sendUDP method is called inside the same window.
                              Is this the reason for readyRead not being emitted.

                              Christian EhrlicherC 1 Reply Last reply
                              1
                              • F febinaf

                                @Christian-Ehrlicher The error was that "the class name does not name a type" .. i think it was because of not including the required header files. Got that fixed now.

                                Right now I've created an object in private section of addUser.h and then I'm calling a method from that object in between the addUser GUI. I'm using a debug message to get status of socket ->bind(), and it returns true as the window opens the addUser gui, and the same returns false when the sendUDP method is called inside the same window.
                                Is this the reason for readyRead not being emitted.

                                Christian EhrlicherC Offline
                                Christian EhrlicherC Offline
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                Please post a minimal, compilable exmaple to your problem - you did so many changes, posted only parts of your code so that I don't know what you are actually really compiling...

                                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                Visit the Qt Academy at https://academy.qt.io/catalog

                                F 1 Reply Last reply
                                0
                                • Christian EhrlicherC Christian Ehrlicher

                                  Please post a minimal, compilable exmaple to your problem - you did so many changes, posted only parts of your code so that I don't know what you are actually really compiling...

                                  F Offline
                                  F Offline
                                  febinaf
                                  wrote on last edited by
                                  #16

                                  @Christian-Ehrlicher Ok
                                  an on_pushButton_clicked from my mainwindow invokes an addUser object and shows the window.

                                  //addUser.h
                                  #ifndef ADDUSER_H
                                  #define ADDUSER_H
                                  #include <QMainWindow>
                                  #include <QDialog>
                                  #include <QString>
                                  #include <myudp.h>
                                  
                                  namespace Ui {
                                  class addUser;
                                  }
                                  class addUser : public QDialog
                                  {
                                      Q_OBJECT
                                  public:
                                      explicit addUser(QWidget *parent = nullptr);
                                      ~addUser();
                                      QString packetToDevice, IP, port;
                                      int portNum;
                                  
                                  private slots:
                                      void on_pushButton_2_clicked();
                                  
                                  private:
                                      Ui::addUser *ui;
                                      QString addUserName, addNewID, addRFID, confirmBuffer, strLength;
                                      myUDP myudp; //object added as member
                                  };
                                  
                                  #endif // ADDUSER_H
                                  
                                  //addUser.cpp
                                  #include "adduser.h"
                                  #include "ui_adduser.h"
                                  #include "QMessageBox"
                                  #include "QDebug"
                                  #include "myudp.h" //udp header
                                  
                                  //#include "QDeadlineTimer"
                                  
                                  addUser::addUser(QWidget *parent) :
                                      QDialog(parent),
                                      ui(new Ui::addUser)
                                  {
                                      ui->setupUi(this);
                                  }
                                  
                                  addUser::~addUser()
                                  {
                                      delete ui;
                                  }
                                  
                                  void addUser::on_pushButton_2_clicked() //submit button press
                                  {
                                      addUser User;
                                      QMessageBox mssgBox;
                                      addUserName = ui->lineEdit->text();
                                      addNewID = ui->lineEdit_2->text();
                                      addRFID = ui->lineEdit_3->text();
                                      IP = ui->lineEdit_4->text();
                                      port = ui->lineEdit_5->text();
                                      portNum = port.toInt();
                                  
                                      int rfidLength = addRFID.length();
                                      int lengthOfName = addUserName.length();
                                      strLength = QString::number(lengthOfName); // number to string
                                  
                                      if((rfidLength == 10) && (lengthOfName <11))
                                      {   mssgBox.setWindowTitle("Confirmation");
                                          mssgBox.setText("Confirm the new user details" );
                                       
                                          mssgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Save);
                                          int returnValue = mssgBox.exec();
                                  
                                  
                                          switch(returnValue)
                                          {
                                  
                                              case QMessageBox::Save :
                                                  {
                                                  //packetize and send the details over UDP
                                               packetToDevice = "FA"+addNewID+strLength+addUserName+addRFID;
                                                 myudp.udpSend(packetToDevice, IP, portNum); //UDP SEND Fn CALL
                                   User.show(); // back to the window
                                                  break;
                                                  }
                                              case QMessageBox::Cancel :
                                                  User.show(); //back to window
                                                  break;
                                              default:
                                                  qDebug()<<"entered default state";
                                          }
                                  }
                                  
                                      else
                                      {
                                          QMessageBox falseMssg;
                                          falseMssg.setWindowTitle("Error");
                                          falseMssg.setText("Invalid RFID/Name");
                                         falseMssg.setStandardButtons(QMessageBox::Close);
                                          falseMssg.exec();
                                          User.show();
                                  
                                      }
                                  
                                  }
                                  

                                  the above one is the file where myudp object is to be used to send and readyRead should be emitted whenever datagram arrives.

                                  then the udp header and source is as follows:

                                  //myUdp.h
                                  #ifndef MYUDP_H
                                  #define MYUDP_H
                                  #include <QObject>
                                  #include <QUdpSocket>
                                  
                                  class myUDP : public QObject
                                  {
                                      Q_OBJECT
                                  public:
                                      explicit myUDP(QObject *parent = nullptr);
                                      void udpSend(QString, QString, int);
                                  signals:
                                  
                                  public slots:
                                      void receiveUDP();
                                  
                                  private:
                                      QUdpSocket *socket2;
                                  };
                                  
                                  #endif // MYUDP_H
                                  
                                  //myudp.cpp
                                  #include "myudp.h"
                                  #include "QUdpSocket"
                                  #include "QDebug"
                                  #include "adduser.h" //
                                  
                                  myUDP::myUDP(QObject *parent)
                                      : QObject{parent}
                                  {
                                      socket2 = new QUdpSocket(this);
                                      bool result = socket2->bind(QHostAddress::Any, 1234);
                                      qDebug()<<"bind status"<<result;
                                      connect(socket2, SIGNAL(readyRead()), this, SLOT(receiveUDP()));
                                     
                                  void myUDP::udpSend(QString a, QString ip, int port)
                                  {       const char *str;
                                          QByteArray dataToSend;
                                          dataToSend = a.toLatin1(); //Qstring to const char *
                                          str = dataToSend.data();
                                          int retVal = socket2 ->writeDatagram(str, QHostAddress(ip), port);
                                          qDebug()<< retVal; // length of the transmitted string  if successfully transmitted or -1
                                  }
                                  
                                  void myUDP::receiveUDP() //called when readyRead SIGNAL is emitted.
                                  {
                                      while (socket2->hasPendingDatagrams())
                                      {
                                          QByteArray datagram;
                                          datagram.resize(socket2->pendingDatagramSize());
                                          QHostAddress sender;
                                          quint16 senderPort;
                                  
                                          socket2->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort );
                                          qDebug()<<datagram;
                                          qDebug()<<"IP "<<sender.toString();
                                   }
                                  }
                                  
                                  Christian EhrlicherC J.HilkJ 2 Replies Last reply
                                  0
                                  • F febinaf

                                    @Christian-Ehrlicher Ok
                                    an on_pushButton_clicked from my mainwindow invokes an addUser object and shows the window.

                                    //addUser.h
                                    #ifndef ADDUSER_H
                                    #define ADDUSER_H
                                    #include <QMainWindow>
                                    #include <QDialog>
                                    #include <QString>
                                    #include <myudp.h>
                                    
                                    namespace Ui {
                                    class addUser;
                                    }
                                    class addUser : public QDialog
                                    {
                                        Q_OBJECT
                                    public:
                                        explicit addUser(QWidget *parent = nullptr);
                                        ~addUser();
                                        QString packetToDevice, IP, port;
                                        int portNum;
                                    
                                    private slots:
                                        void on_pushButton_2_clicked();
                                    
                                    private:
                                        Ui::addUser *ui;
                                        QString addUserName, addNewID, addRFID, confirmBuffer, strLength;
                                        myUDP myudp; //object added as member
                                    };
                                    
                                    #endif // ADDUSER_H
                                    
                                    //addUser.cpp
                                    #include "adduser.h"
                                    #include "ui_adduser.h"
                                    #include "QMessageBox"
                                    #include "QDebug"
                                    #include "myudp.h" //udp header
                                    
                                    //#include "QDeadlineTimer"
                                    
                                    addUser::addUser(QWidget *parent) :
                                        QDialog(parent),
                                        ui(new Ui::addUser)
                                    {
                                        ui->setupUi(this);
                                    }
                                    
                                    addUser::~addUser()
                                    {
                                        delete ui;
                                    }
                                    
                                    void addUser::on_pushButton_2_clicked() //submit button press
                                    {
                                        addUser User;
                                        QMessageBox mssgBox;
                                        addUserName = ui->lineEdit->text();
                                        addNewID = ui->lineEdit_2->text();
                                        addRFID = ui->lineEdit_3->text();
                                        IP = ui->lineEdit_4->text();
                                        port = ui->lineEdit_5->text();
                                        portNum = port.toInt();
                                    
                                        int rfidLength = addRFID.length();
                                        int lengthOfName = addUserName.length();
                                        strLength = QString::number(lengthOfName); // number to string
                                    
                                        if((rfidLength == 10) && (lengthOfName <11))
                                        {   mssgBox.setWindowTitle("Confirmation");
                                            mssgBox.setText("Confirm the new user details" );
                                         
                                            mssgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Save);
                                            int returnValue = mssgBox.exec();
                                    
                                    
                                            switch(returnValue)
                                            {
                                    
                                                case QMessageBox::Save :
                                                    {
                                                    //packetize and send the details over UDP
                                                 packetToDevice = "FA"+addNewID+strLength+addUserName+addRFID;
                                                   myudp.udpSend(packetToDevice, IP, portNum); //UDP SEND Fn CALL
                                     User.show(); // back to the window
                                                    break;
                                                    }
                                                case QMessageBox::Cancel :
                                                    User.show(); //back to window
                                                    break;
                                                default:
                                                    qDebug()<<"entered default state";
                                            }
                                    }
                                    
                                        else
                                        {
                                            QMessageBox falseMssg;
                                            falseMssg.setWindowTitle("Error");
                                            falseMssg.setText("Invalid RFID/Name");
                                           falseMssg.setStandardButtons(QMessageBox::Close);
                                            falseMssg.exec();
                                            User.show();
                                    
                                        }
                                    
                                    }
                                    

                                    the above one is the file where myudp object is to be used to send and readyRead should be emitted whenever datagram arrives.

                                    then the udp header and source is as follows:

                                    //myUdp.h
                                    #ifndef MYUDP_H
                                    #define MYUDP_H
                                    #include <QObject>
                                    #include <QUdpSocket>
                                    
                                    class myUDP : public QObject
                                    {
                                        Q_OBJECT
                                    public:
                                        explicit myUDP(QObject *parent = nullptr);
                                        void udpSend(QString, QString, int);
                                    signals:
                                    
                                    public slots:
                                        void receiveUDP();
                                    
                                    private:
                                        QUdpSocket *socket2;
                                    };
                                    
                                    #endif // MYUDP_H
                                    
                                    //myudp.cpp
                                    #include "myudp.h"
                                    #include "QUdpSocket"
                                    #include "QDebug"
                                    #include "adduser.h" //
                                    
                                    myUDP::myUDP(QObject *parent)
                                        : QObject{parent}
                                    {
                                        socket2 = new QUdpSocket(this);
                                        bool result = socket2->bind(QHostAddress::Any, 1234);
                                        qDebug()<<"bind status"<<result;
                                        connect(socket2, SIGNAL(readyRead()), this, SLOT(receiveUDP()));
                                       
                                    void myUDP::udpSend(QString a, QString ip, int port)
                                    {       const char *str;
                                            QByteArray dataToSend;
                                            dataToSend = a.toLatin1(); //Qstring to const char *
                                            str = dataToSend.data();
                                            int retVal = socket2 ->writeDatagram(str, QHostAddress(ip), port);
                                            qDebug()<< retVal; // length of the transmitted string  if successfully transmitted or -1
                                    }
                                    
                                    void myUDP::receiveUDP() //called when readyRead SIGNAL is emitted.
                                    {
                                        while (socket2->hasPendingDatagrams())
                                        {
                                            QByteArray datagram;
                                            datagram.resize(socket2->pendingDatagramSize());
                                            QHostAddress sender;
                                            quint16 senderPort;
                                    
                                            socket2->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort );
                                            qDebug()<<datagram;
                                            qDebug()<<"IP "<<sender.toString();
                                     }
                                    }
                                    
                                    Christian EhrlicherC Offline
                                    Christian EhrlicherC Offline
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    I don't understand what you're doing - why do you try to send something to yourself? I doubt you receive data on the same socket you're currently writing it.

                                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                    Visit the Qt Academy at https://academy.qt.io/catalog

                                    F 1 Reply Last reply
                                    0
                                    • F febinaf

                                      @Christian-Ehrlicher Ok
                                      an on_pushButton_clicked from my mainwindow invokes an addUser object and shows the window.

                                      //addUser.h
                                      #ifndef ADDUSER_H
                                      #define ADDUSER_H
                                      #include <QMainWindow>
                                      #include <QDialog>
                                      #include <QString>
                                      #include <myudp.h>
                                      
                                      namespace Ui {
                                      class addUser;
                                      }
                                      class addUser : public QDialog
                                      {
                                          Q_OBJECT
                                      public:
                                          explicit addUser(QWidget *parent = nullptr);
                                          ~addUser();
                                          QString packetToDevice, IP, port;
                                          int portNum;
                                      
                                      private slots:
                                          void on_pushButton_2_clicked();
                                      
                                      private:
                                          Ui::addUser *ui;
                                          QString addUserName, addNewID, addRFID, confirmBuffer, strLength;
                                          myUDP myudp; //object added as member
                                      };
                                      
                                      #endif // ADDUSER_H
                                      
                                      //addUser.cpp
                                      #include "adduser.h"
                                      #include "ui_adduser.h"
                                      #include "QMessageBox"
                                      #include "QDebug"
                                      #include "myudp.h" //udp header
                                      
                                      //#include "QDeadlineTimer"
                                      
                                      addUser::addUser(QWidget *parent) :
                                          QDialog(parent),
                                          ui(new Ui::addUser)
                                      {
                                          ui->setupUi(this);
                                      }
                                      
                                      addUser::~addUser()
                                      {
                                          delete ui;
                                      }
                                      
                                      void addUser::on_pushButton_2_clicked() //submit button press
                                      {
                                          addUser User;
                                          QMessageBox mssgBox;
                                          addUserName = ui->lineEdit->text();
                                          addNewID = ui->lineEdit_2->text();
                                          addRFID = ui->lineEdit_3->text();
                                          IP = ui->lineEdit_4->text();
                                          port = ui->lineEdit_5->text();
                                          portNum = port.toInt();
                                      
                                          int rfidLength = addRFID.length();
                                          int lengthOfName = addUserName.length();
                                          strLength = QString::number(lengthOfName); // number to string
                                      
                                          if((rfidLength == 10) && (lengthOfName <11))
                                          {   mssgBox.setWindowTitle("Confirmation");
                                              mssgBox.setText("Confirm the new user details" );
                                           
                                              mssgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Save);
                                              int returnValue = mssgBox.exec();
                                      
                                      
                                              switch(returnValue)
                                              {
                                      
                                                  case QMessageBox::Save :
                                                      {
                                                      //packetize and send the details over UDP
                                                   packetToDevice = "FA"+addNewID+strLength+addUserName+addRFID;
                                                     myudp.udpSend(packetToDevice, IP, portNum); //UDP SEND Fn CALL
                                       User.show(); // back to the window
                                                      break;
                                                      }
                                                  case QMessageBox::Cancel :
                                                      User.show(); //back to window
                                                      break;
                                                  default:
                                                      qDebug()<<"entered default state";
                                              }
                                      }
                                      
                                          else
                                          {
                                              QMessageBox falseMssg;
                                              falseMssg.setWindowTitle("Error");
                                              falseMssg.setText("Invalid RFID/Name");
                                             falseMssg.setStandardButtons(QMessageBox::Close);
                                              falseMssg.exec();
                                              User.show();
                                      
                                          }
                                      
                                      }
                                      

                                      the above one is the file where myudp object is to be used to send and readyRead should be emitted whenever datagram arrives.

                                      then the udp header and source is as follows:

                                      //myUdp.h
                                      #ifndef MYUDP_H
                                      #define MYUDP_H
                                      #include <QObject>
                                      #include <QUdpSocket>
                                      
                                      class myUDP : public QObject
                                      {
                                          Q_OBJECT
                                      public:
                                          explicit myUDP(QObject *parent = nullptr);
                                          void udpSend(QString, QString, int);
                                      signals:
                                      
                                      public slots:
                                          void receiveUDP();
                                      
                                      private:
                                          QUdpSocket *socket2;
                                      };
                                      
                                      #endif // MYUDP_H
                                      
                                      //myudp.cpp
                                      #include "myudp.h"
                                      #include "QUdpSocket"
                                      #include "QDebug"
                                      #include "adduser.h" //
                                      
                                      myUDP::myUDP(QObject *parent)
                                          : QObject{parent}
                                      {
                                          socket2 = new QUdpSocket(this);
                                          bool result = socket2->bind(QHostAddress::Any, 1234);
                                          qDebug()<<"bind status"<<result;
                                          connect(socket2, SIGNAL(readyRead()), this, SLOT(receiveUDP()));
                                         
                                      void myUDP::udpSend(QString a, QString ip, int port)
                                      {       const char *str;
                                              QByteArray dataToSend;
                                              dataToSend = a.toLatin1(); //Qstring to const char *
                                              str = dataToSend.data();
                                              int retVal = socket2 ->writeDatagram(str, QHostAddress(ip), port);
                                              qDebug()<< retVal; // length of the transmitted string  if successfully transmitted or -1
                                      }
                                      
                                      void myUDP::receiveUDP() //called when readyRead SIGNAL is emitted.
                                      {
                                          while (socket2->hasPendingDatagrams())
                                          {
                                              QByteArray datagram;
                                              datagram.resize(socket2->pendingDatagramSize());
                                              QHostAddress sender;
                                              quint16 senderPort;
                                      
                                              socket2->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort );
                                              qDebug()<<datagram;
                                              qDebug()<<"IP "<<sender.toString();
                                       }
                                      }
                                      
                                      J.HilkJ Offline
                                      J.HilkJ Offline
                                      J.Hilk
                                      Moderators
                                      wrote on last edited by
                                      #18

                                      @febinaf I'm reasonably sure, that writeDatagram

                                      void myUDP::udpSend(QString a, QString ip, int port)
                                      {       const char *str;
                                              QByteArray dataToSend;
                                              dataToSend = a.toLatin1(); //Qstring to const char *
                                              str = dataToSend.data();
                                              int retVal = socket2 ->writeDatagram(str, QHostAddress(ip), port);
                                              qDebug()<< retVal; // length of the transmitted string  if successfully transmitted or -1
                                      }
                                      

                                      is an asynchronous process. So there might be some lifetime issues...


                                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                      Q: What's that?
                                      A: It's blue light.
                                      Q: What does it do?
                                      A: It turns blue.

                                      F 1 Reply Last reply
                                      0
                                      • Christian EhrlicherC Christian Ehrlicher

                                        I don't understand what you're doing - why do you try to send something to yourself? I doubt you receive data on the same socket you're currently writing it.

                                        F Offline
                                        F Offline
                                        febinaf
                                        wrote on last edited by
                                        #19

                                        @Christian-Ehrlicher I'm writing data to IP and port entered using user input. And I've used the same socket to send and receive, as its being shown in online tutorials ..... https://www.youtube.com/watch?v=4qx4FaglSig

                                        I've checked the above code in another project with just main function and udp class, and both reading and writing works fine using a single socket.

                                        1 Reply Last reply
                                        0
                                        • J.HilkJ J.Hilk

                                          @febinaf I'm reasonably sure, that writeDatagram

                                          void myUDP::udpSend(QString a, QString ip, int port)
                                          {       const char *str;
                                                  QByteArray dataToSend;
                                                  dataToSend = a.toLatin1(); //Qstring to const char *
                                                  str = dataToSend.data();
                                                  int retVal = socket2 ->writeDatagram(str, QHostAddress(ip), port);
                                                  qDebug()<< retVal; // length of the transmitted string  if successfully transmitted or -1
                                          }
                                          

                                          is an asynchronous process. So there might be some lifetime issues...

                                          F Offline
                                          F Offline
                                          febinaf
                                          wrote on last edited by
                                          #20

                                          @J-Hilk ooh ... so the writeDatagram method may be killing the object after the methods execution?

                                          Let me try with other write methods available in the QUdpSocket class.

                                          Christian EhrlicherC 1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved