QByteArray Initialization



  • Hi Friends

    I am facing an issue in QByteArray when QByteArray Initialization.

    I can initialize a QByteArray like:

    QByteArray m_data;
    m_data[0] = 0x0c;
    m_data[1] = 0x06;
    m_data[2] = 0x04;
    m_data[3] = 0x04;
    m_data[4] = 0x02;
    m_data[5] = 0x00;
    

    But I would like something more compact, like:

    QByteArray m_data{0x0c, 0x06, 0x04, 0x04, 0x02, 0x00};
    

    Unfortunately, this form isn't allowed:

    error: could not convert '{12, 6, 4, 4, 2, 0}' from '<brace-enclosed initializer list>' to 'QByteArray'
    QByteArray m_data{0x0c, 0x06, 0x04, 0x04, 0x02, 0x00};

    I want to pass m_data(array Data ) in emit signal. Any code example and snippet will be helpful.

    Thanks In Advance

    Best Regards
    Vivek Yuvan



  • HI @vivekyuvan

    What if you initialize it like this:

    QByteArray m_data("\x0c\x06\x04\x04\x02\x00",6);
    

    ?



  • HI @mostefa I got an an error like this below
    error: could not convert '{12, 6, 4, 4, 2, 0}' from '<brace-enclosed initializer list>' to 'QByteArray'
    QByteArray m_data{0x0c, 0x06, 0x04, 0x04, 0x02, 0x00};



  • @vivekyuvan said in QByteArray Initialization:

    HI @mostefa I got an an error like this below
    error: could not convert '{12, 6, 4, 4, 2, 0}' from '<brace-enclosed initializer list>' to 'QByteArray'
    QByteArray m_data{0x0c, 0x06, 0x04, 0x04, 0x02, 0x00};

    Can you share the code of what you are doing exactly?



  • Hi @mostefa
    Its a test code I want to initalize array in normal C Standard type

    If I initialize qbyte array in Private Slot

    Ex

    QByteArray m_data={0x0c, 0x06, 0x04, 0x04, 0x02, 0x00};
    

    I got an error See the screen shot ![alt text](0_1505900733317_ErrorScreenShot.png image url)



  • @vivekyuvan said in QByteArray Initialization:

    Hi @mostefa
    Its a test code I want to initalize array in normal C Standard type

    If I initialize qbyte array in Private Slot

    Ex

    QByteArray m_data={0x0c, 0x06, 0x04, 0x04, 0x02, 0x00};
    

    I got an error See the screen shot ![alt text](0_1505900733317_ErrorScreenShot.png image url)

    What if you change this line :

    QByteArray m_data={0x0c, 0x06, 0x04, 0x04, 0x02, 0x00};
    

    To this following line :

    QByteArray m_data = QByteArray("\x0c\x06\x04\x04\x02\x00",6);
    

    QByteArray Constructor



  • Hi@mostefa

    QByteArray m_data = QByteArray("\x0c\x06\x04\x04\x02\x00",6);
    

    The Error message gone and I got the output thanks. But I want to send the array to emit signal? is that possible ? I am newbie to QT soft/Development guide me to resolve this problem



  • @vivekyuvan said in QByteArray Initialization:

    Hi@mostefa The Error message gone and I got the output thanks. But I want to send the array to emit signal? is that possible ? I am newbie to QT soft/Development guide me to resolve this problem

    You can actually send array in signal,

    let's say you have the following signal :

    void sendByteArray(QByteArray);// on your .h file
    

    inside your testArrayInitialization you can do something like this :

    QByteArray m_data = QByteArray("\x0c\x06\x04\x04\x02\x00",6);
    emit sendByteArray(m_data);
    

    If this is not what you want , can you explain what you want to do?



  • @mostefa thanks The problem is solved



  • You are welcome =) , you can change the state of your post to solved



  • What about:

    QByteArray text = QByteArray::fromHex("517420697320677265617421");
    

  • Moderators

    @titan83 This is just a string containing ASCII characters.
    It should be like @mostefa shown:

    QByteArray m_data = QByteArray("\x0c\x06\x04\x04\x02\x00",6);
    


  • Hi friend, use this:

    #include <QCoreApplication>
    
    int main(int argc, char *argv[])
    {
       QCoreApplication a(argc, argv);
    
       QByteArray data;
    
       data.append((char)0x0c);
       data.append((char)0x06);
       data.append((char)0x04);
       data.append((char)0x04);
       data.append((char)0x02);
       data.append((char)0x00);
       data.append((char)0xFF);
    
       printf("%d\n", data.size());
       printf("%s\n", data.toHex().toStdString().c_str());
    
       return a.exec();   
    }
    

  • Moderators

    @joaopagotto This is actually the same he already did. He asked for something like this or similar:

    QByteArray m_data{0x0c, 0x06, 0x04, 0x04, 0x02, 0x00};
    

    Also there is no need to use printf in C++. Instead of

    printf("%d\n", data.size());
    

    you can just write

    qDebug() << data.size();
    

    or C++ without Qt

    std::cout << data.size();
    


  • This post is deleted!

  • Qt Champions 2017

    @jsulm said in QByteArray Initialization:

    @titan83 This is just a string containing ASCII characters.

    Of course @titan83 has a point here. His string is made of ASCII characters, but he uses QByteArray::fromHex(). This may not be the fastest operation, but allows to read the data from UI or an ASCII file, for example.



  • You can basically use a C++11 initializer list the following way (I've added a char higher than 0x7f and initializtion by variables):

    char        v1 = 0x06, v2 = 0x04;
    QByteArray  ba(std::begin<char>({0x0c, v1, v2, v2, 0x02, 0x00, '\xf3'}), 7);
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.