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. UDP client and real-time graphics
QtWS25 Last Chance

UDP client and real-time graphics

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 927 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.
  • D Offline
    D Offline
    dfvgergver
    wrote on last edited by dfvgergver
    #1

    Hello,

    I am developing a UDP "client" that receives UDP frames every second. From this data (temperature data for this case), the client displays a graph with all the temperature data received since the customer's launch.

    I can receive UD data without any problems thank to this line :

    connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
    

    But, I can't transmit the data to the "Chart::handleTimeout(QByteArray)" function from the "MyUDP::readyRead()" function.

    I tried with this line :

    connect(this,SIGNAL(dataReady(QByteArray)),Chart::handleTimeout(),SLOT(Chart::handleTimeout(QByteArray)));
    

    But, it didn't work.

    And last but not least, I'm a newbie and I have based myself on the following examples:

    • https://gist.github.com/lamprosg/4593723

    • https://doc.qt.io/qt-5/qtcharts-dynamicspline-example.html

    Thank you for your help :-) !

    VRoninV 1 Reply Last reply
    0
    • D dfvgergver

      Hello,

      I am developing a UDP "client" that receives UDP frames every second. From this data (temperature data for this case), the client displays a graph with all the temperature data received since the customer's launch.

      I can receive UD data without any problems thank to this line :

      connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
      

      But, I can't transmit the data to the "Chart::handleTimeout(QByteArray)" function from the "MyUDP::readyRead()" function.

      I tried with this line :

      connect(this,SIGNAL(dataReady(QByteArray)),Chart::handleTimeout(),SLOT(Chart::handleTimeout(QByteArray)));
      

      But, it didn't work.

      And last but not least, I'm a newbie and I have based myself on the following examples:

      • https://gist.github.com/lamprosg/4593723

      • https://doc.qt.io/qt-5/qtcharts-dynamicspline-example.html

      Thank you for your help :-) !

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      The syntax is connect(sender,signal,receiver,slot) so:

      • the first argument must be a pointer to the object emitting the signal
      • the second argument must be either a function pointer to the specific signal or the name of the signal in the SIGNAL macro.
      • the third argument must be a pointer to the object the slot should execute on
      • the second argument must be either a function pointer to the specific slot or the name of the slot in the SLOT macro.

      In your case the 3rd argument is wrong, it should be an object and you have to remove Chart:: from the forth argument

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      3
      • D Offline
        D Offline
        dfvgergver
        wrote on last edited by
        #3

        Ok,

        Here, my "myudp.cpp" file :

        #include "myudp.h"
        #include "chart.h"
        
        MyUDP::MyUDP(QObject *parent) : 
            QObject(parent)
        {
          socket = new QUdpSocket(this);
          
          //We need to bind the UDP socket to an address and a port
          socket->bind(QHostAddress::LocalHost,29200);
        
          connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
          connect(this,SIGNAL(dataReady(QByteArray)),chart,SLOT(handleTimeout(QByteArray)));
        }
        
        
        void MyUDP::SayHello()      //Just spit out some data
        {
          QByteArray Data;
          Data.append("Hello from UDP land");
          
          socket->writeDatagram(Data,QHostAddress::LocalHost,1234);
          
          //If you want to broadcast something you send it to your broadcast address
          //ex. 192.2.1.255
        }
        
        
        void MyUDP::readyRead()     //Read something
        {
            QByteArray Buffer;
        
            //Buffer.append("15 24 15 41 ");
            //socket->writeDatagram(Buffer,QHostAddress::LocalHost,29200);
        
            Buffer.clear();
            Buffer.resize(socket->pendingDatagramSize());
            QHostAddress sender;
            quint16 senderPort;
            socket->readDatagram(Buffer.data(),Buffer.size(),&sender,&senderPort);
        
            qInfo() << "bufferData=" << Buffer.data();
        
            emit dataReady(Buffer.data());
        }
        
        

        And here, my "main.cpp" file :

        #include "chart.h"
        #include "myudp.h"
        #include <QtCharts/QChartView>
        #include <QtWidgets/QApplication>
        #include <QtWidgets/QMainWindow>
        
        QT_CHARTS_USE_NAMESPACE
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
        
            MyUDP server;
            //server.readyRead();
        
            QMainWindow window;
            Chart *chart = new Chart;
        
            chart->setTitle("Mesures de températures");
            chart->legend()->hide();
            chart->setAnimationOptions(QChart::AllAnimations);
            QChartView chartView(chart);
            chartView.setRenderHint(QPainter::Antialiasing);
            window.setCentralWidget(&chartView);
            window.resize(400, 300);
            window.show();
        
            return a.exec();
        }
        
        

        The line :

        connect(this,SIGNAL(dataReady(QByteArray)),chart,SLOT(handleTimeout(QByteArray)));
        

        in the "myudp.cpp" file contain an error. The third argument isn't recognize. Because "chart" is declare in "main.cpp" file.

        jsulmJ 1 Reply Last reply
        0
        • D dfvgergver

          Ok,

          Here, my "myudp.cpp" file :

          #include "myudp.h"
          #include "chart.h"
          
          MyUDP::MyUDP(QObject *parent) : 
              QObject(parent)
          {
            socket = new QUdpSocket(this);
            
            //We need to bind the UDP socket to an address and a port
            socket->bind(QHostAddress::LocalHost,29200);
          
            connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
            connect(this,SIGNAL(dataReady(QByteArray)),chart,SLOT(handleTimeout(QByteArray)));
          }
          
          
          void MyUDP::SayHello()      //Just spit out some data
          {
            QByteArray Data;
            Data.append("Hello from UDP land");
            
            socket->writeDatagram(Data,QHostAddress::LocalHost,1234);
            
            //If you want to broadcast something you send it to your broadcast address
            //ex. 192.2.1.255
          }
          
          
          void MyUDP::readyRead()     //Read something
          {
              QByteArray Buffer;
          
              //Buffer.append("15 24 15 41 ");
              //socket->writeDatagram(Buffer,QHostAddress::LocalHost,29200);
          
              Buffer.clear();
              Buffer.resize(socket->pendingDatagramSize());
              QHostAddress sender;
              quint16 senderPort;
              socket->readDatagram(Buffer.data(),Buffer.size(),&sender,&senderPort);
          
              qInfo() << "bufferData=" << Buffer.data();
          
              emit dataReady(Buffer.data());
          }
          
          

          And here, my "main.cpp" file :

          #include "chart.h"
          #include "myudp.h"
          #include <QtCharts/QChartView>
          #include <QtWidgets/QApplication>
          #include <QtWidgets/QMainWindow>
          
          QT_CHARTS_USE_NAMESPACE
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
          
              MyUDP server;
              //server.readyRead();
          
              QMainWindow window;
              Chart *chart = new Chart;
          
              chart->setTitle("Mesures de températures");
              chart->legend()->hide();
              chart->setAnimationOptions(QChart::AllAnimations);
              QChartView chartView(chart);
              chartView.setRenderHint(QPainter::Antialiasing);
              window.setCentralWidget(&chartView);
              window.resize(400, 300);
              window.show();
          
              return a.exec();
          }
          
          

          The line :

          connect(this,SIGNAL(dataReady(QByteArray)),chart,SLOT(handleTimeout(QByteArray)));
          

          in the "myudp.cpp" file contain an error. The third argument isn't recognize. Because "chart" is declare in "main.cpp" file.

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

          @dfvgergver It would be better to do the connect in main.cpp. This way your MyUDP class does not have to know anything about any charts:

          connect(&server,SIGNAL(dataReady(QByteArray)),chart,SLOT(handleTimeout(QByteArray)));
          

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

          1 Reply Last reply
          4
          • D Offline
            D Offline
            dfvgergver
            wrote on last edited by
            #5

            Alleluya !

            It's work ! But like this :

            QObject::connect(&server,SIGNAL(dataReady(QByteArray)),chart,SLOT(handleTimeout(QByteArray)));
            
            

            Thank you very much !

            1 Reply Last reply
            1

            • Login

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