Qt World Summit: Submit your Presentation

UDP reception

  • Hi.i am using QT widget application to receive udp packets from another PC. my souce is a labview VI. i am able to see the received data in debug window.but cannot see in in user interface. i tried text edit and list widget.but not working. this is my first QT pgm. plz help.here is my code. i first tried a local connection. then the data was dis[layed in list widget. i modified the same code for reception.


    MainWindow::MainWind ow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)

    msocket = new QUdpSocket(this);

    msocket-> bind(QHostAddress(""),55500);

    void MainWindow:: readyRead()
        QByteArray Buffer;
        QHostAddress sender;
        quint16 senderPort;
        msocket->readDatagram(Buffer.data(),Buffer.size(),& sender,& senderPort);
       qDebug() << " message from:" << sender.toString();
        qDebug() << " message port:" << senderPort;
        qDebug() << " message:"<<Buffer;
        mystr = QString::fromStdString((const char*)Buffer);
       // ui->listWidget->addItem(QString(mystr));

  • Lifetime Qt Champion

    @reshu said in UDP reception:


    You are aware that setText overrides what was set before?
    You need to append https://doc.qt.io/qt-5/qtextedit.html#append
    Also what exact data do you get over UDP? Is it text or binary?

  • Lifetime Qt Champion

    @reshu said in UDP reception:

    mystr = QString::fromStdString((const char*)Buffer);

    Also, I'm not sure what you're trying to achieve with this line?
    QByteArray is not const char*
    Why don't you simply use https://doc.qt.io/qt-5/qbytearray.html#toStdString ?
    And there is actually no need to conver to std string if you want to set text in QLineEdit as it expects a QString.

  • @jsulm
    I tried this too.
    in both cases i am getting display in local mode in both listwidget and textedit. when i give the udp data from another PC , data is not displayed whereas the data is there in debug window.

  • Lifetime Qt Champion

    @reshu said in UDP reception:



    You did not answer this question: "Also what exact data do you get over UDP? Is it text or binary?"!
    What data do you send? Is it actually printable?
    Your qDebug output does not look like the data you're sending is printable...

  • @jsulm
    this is my data which i am sending from another PC ,a labview VI. actually this data simulates the data from some sensors, counters etc updated in different fields. real time. Here i am using constant data in all fields.
    ui->listWidget->addItem(sender.toString()); this is working
    ui->txtedit->setText(sender.toString()); this is working

    QByteArray x;
    ui->listWidget->addItem(QByteArray(x)); this is also working

    ui->txtedit->append(QString::fromLatin1(Buffer)); is not working

  • @reshu
    As @jsulm asked you, your buffer data is binary bytes and not "actually printable", isn't it? So how would you expect it to be displayable in a QTextEdit? Everything is working, you need to do something about deciding how you want this binary data to be visibly displayed. For example, if you're expecting it to come out like the bottom pane you show, or like qDebug() chooses to display it, you're going to have write the code to do that, it doesn't happen by magic.

  • @JonB
    Sorry.i am new to this. i thought i will be able to see the byte array in textedit as in labview. all the fields of the UDP data corresponds to some sensor data. i just wanted to see if all the data fields are updating correctly first. later i will use the data as i wish to do some other action. is there any other option by which i can see my data and verify it?? the data which i am getting in the debug window is also not exactly the same as the one i am not sending. some data is missing. sorry for wasting your time. let me try first.

  • @reshu You could use Buffer.toHex(' ')

  • @Bonnie
    .i tried it already and i am getting the data which i am sending displayed correctly in the debug window.
    qDebug() << "total msg:"<< Buffer.toHex();
    i tried like this . i mean without (' ') single quotes. will it make any change.

    message from: ""
    message port: 55000
    message: @ // this corresponds to a single byte (ascii value of 4th byte) //qDebug() << " message:"<< (Buffer.data()[3]);
    total msg: "00015a400000000055aa000001000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"

    i want to convert each byte seperately to hex so that i can initiate some other action based on these byte values. can you give the solution for converting each byte of QBytearray to hex.

  • @reshu with a (' ') as parameter, it should be like "00 01 5a 40".
    So if you want the hex text seperated byte by byte maybe you can try

    Buffer.toHex(' ').split(' ');

    This will return a QList<QByteArray>, from which each one should be a hex of a byte.
    And if you want a specific byte to hex, for example, the 4th (index=3), you can try

    QByteArray(1, Buffer[3]).toHex();



  • @Bonnie
    thank you so much. it worked perfectly. i tried all the three. split and append worked.
    QByteArray(1, Buffer[3]).toHex(); i didn't get properly.
    here is my output
    message from: ""
    message port: 55000
    message: @ // 3dr byte read directly from buffer
    total msg: "00015a400000000055aa000001000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
    total msg_dec: @
    test: "00" /// output of QByteArray(1, Buffer[3]).toHex();
    test1: "40" /// output of QByteArray().append(Buffer[3]).toHex();

    test3: ("00", "01", "5a", "40", "00", "00", "00", "00", "55", "aa", "00", "00", "01", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "01", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00") //// output of Buffer.toHex(' ').split(' ');

    thanks once again

  • Lifetime Qt Champion

    And what should be wrong here? 0x40 = '@' ...

  • @Christian-Ehrlicher
    I never said that it is wrong.
    please refer to the previous msg
    message: @ // this corresponds to a single byte (ascii value of 4th byte) //qDebug() << " message:"<< (Buffer.data()[3]);
    i have to chk each byte of the data for some control action for which i need either decimal or hex value for easiness. its difficult to compare ascii values .that's it.

  • @reshu The second one works on my machine, not sure why not working on yours.
    But since you can use the third one, that doesn't matter.
    If your problem is solved, please mark this topic as "solved".

  • Lifetime Qt Champion

    @reshu said in UDP reception:

    its difficult to compare ascii values

    Why? It's very basic C stuff ...

  • @Bonnie yeah its solved.thanks for the support.

Log in to reply