The Actual Data not received in receiver slot while using QByteArray

  • Hi Friends
    I have a problem with using QByteArray , The receiver slot can not receive actual data

    Here is the code snippet

    Sending Slot is :

    void My_SIGNAL_GET_Form1::Test_Array_Initialization_1()
       QByteArray m_data_1("\x0c\x06\x04\x04\x02\x00",6);
       emit Send_Data_Array_to_form(m_data_1);     //Emiting data to 

    I formed connect signal like this in my My_SIGNAL_GET_Form1.cpp


    The receiver slot in main window

    void MainWindow::Test_Array_Initialization(QByteArray m_data)
        qDebug()<<"*****SIGNAL EMITED From My_signal_get_form1**********"<<endl;
      qDebug()<<"data received"<<m_data<<endl;

    I formed connect signal like this mainwindow.cpp



  • Moderators

    I strongly recommend using the new signal-slot syntax, it will warn you about connection problems at compile time.

    In the picture you attach, we can clearly see "data received" line, so it seems to be working. What exactly do you expect to happen, what is wrong?

  • hi@sierdzio If i send char in QByte array thats fine to receive. but I want to emit integer or hexadecimal value in emit signal. I really don know how to do this? help me out to resolve this issue.

    this data received properly in receiver slot

    QByteArray m_data_1("\A\F\B\E\C\D",6);

    But I want to send Hexdecimal data like

    QByteArray m_data_1("\x0c\x06\x04\x04\x02\x00",6);
    QByteArray m_data_1("\0\4\7\9\5\3",6);

    if i send hexadecimal value or integer value in emit signal the receiver slot i cant see the actual data and it shows a junk data.

    data received "
    Sending_DAta: "

  • Moderators

    Most probably what you see is the way that your console interprets the data (it displays UTF-8, most probably, so your bytes are reinterpreted as UTF-8 text). The underlying QByteArray is fine and sends exactly what you want.

    In your qDebug() statements, add .toHex() to the byte array, like so:

     qDebug()<<"data received"<<m_data.toHex();

    Also, don't use endl, it is not needed in qDebug. And another hint: you don't need to provide data size to QByteArray, it will automatically calculate it. So, there is no need to specify the second parameter in QByteArray constructor.

  • Moderators

    @vivekyuvan thats qDebug() "fault" not that of QByteArray
    try toHex()

    qDebug()<<"data received"<<m_data.toHex()<<endl;

    edit: @sierdzio was a tiny bit faster x)

  • @sierdzio The problem solved thanks for the reply.

  • @J.Hilk The problem is solved Thanks for the reply

Log in to reply