Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

UDP client and real-time graphics



  • 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:

    Thank you for your help :-) !



  • 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



  • 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.


  • Lifetime Qt Champion

    @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)));
    


  • Alleluya !

    It's work ! But like this :

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

    Thank you very much !


Log in to reply