Results 1 to 7 of 7

Thread: QByteArray overrides

  1. #1
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QByteArray overrides

    I'm creating an array to send across the network, so decided on QByteArray, which seems ideal for the task, except I want to insert some 32-bit values in the array, and it doesn't seem to support this other than by creating another array with the value and then appending that array to the first one, which seems a little daft.

    I really don't want to have to shift, mask, and insert each byte seperately, but is there another option? I could be doing this 20 times or more per packet, but thinking there must be a better way than creating a method that takes a QByteArray and inserts each byte at the end.

  2. #2
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    81
    Thanks
    6
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QByteArray overrides

    You can use the union keyword to make to make the conversion from qint32 to char *, and the just use the funcion
    QByteArray::append ( const char * str, int len ) to append the value32bits to a QByteArray


    Qt Code:
    1. union {
    2. qint32 value32bits;
    3. char char32bits[4];
    4. } castValue;
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QCoreApplication a(argc, argv);
    9.  
    10. QByteArray array;
    11. qint32 myValue = 87000; // some 32 bits value
    12. castValue.value32bits = myValue;
    13. array.append(castValue.char32bits, 4);
    14. qDebug() << array;
    15.  
    16. return a.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 

    is a low level, but using union you can make the bit convertion.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QByteArray overrides

    If you use this approach though you might need to very careful about byte order.

  4. #4
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    81
    Thanks
    6
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QByteArray overrides

    Quote Originally Posted by fatjuicymole View Post
    I'm creating an array to send across the network, so decided on QByteArray, which seems ideal for the task, except I want to insert some 32-bit values in the array, and it doesn't seem to support this other than by creating another array with the value and then appending that array to the first one, which seems a little daft.
    if the QByteArray data are send across the network, using a QDataStream resolve the issues about the byte order f the machine.

    The QByteArray is filled whit data in the native byte order, and then is send if a portable way using QDataStream.


    or use a code like ...
    Qt Code:
    1. if ( QSysInfo::ByteOrder == QSysInfo::LittleEndian) {
    2. ...
    3. }
    4. else{ //QSysInfo::BigEndian
    5. ...
    6. }
    To copy to clipboard, switch view to plain text mode 

    and using the function provide in QtEndian header.

    sorry about the incomplete code . i don´t remember how work whit the endianess of the machine. one litle disadvantages of programming of QT
    Last edited by ecanela; 11th January 2010 at 05:57. Reason: updated contents

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QByteArray overrides

    Thanks, you've given me a brilliant idea. I noticed that all of these 32 bit values appear at the start of the packet, and that the byte ordering is the same as Intel, so I can just create an array of quint32's, calculate the size and either cast or use a union to copy X amount of bytes from that array to the packet. Perfect.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QByteArray overrides

    You don't have to cast nor to use unions.
    The scheme is:
    Qt Code:
    1. ba.resize(1000);
    2. int x, y, z;
    3. char *data = ba.data();
    4. memcpy(data, &x, sizeof(x));
    5. memcpy(data+sizeof(x), &y, sizeof(y));
    6. memcpy(data+sizeof(x)+sizeof(y), &z, sizeof(z));
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QByteArray overrides

    Thanks wysota, I didn't know whether or not accessing the internal data of a QByteArray was frowned upon or not, or could cause potential problems. I was also going to use append as it guarantees the space is available, and resizes if not, but I guess I can calculate the amount of space required by the quint32's at least (at the moment, I just over-estimate and 'reserve' the amount rather than a resize).

Similar Threads

  1. Regarding qbytearray
    By mohanakrishnan in forum Qt Programming
    Replies: 7
    Last Post: 19th November 2009, 13:38
  2. Replies: 9
    Last Post: 25th July 2009, 13:27
  3. QGtkStyle - Ignores QPalette color overrides
    By chezifresh in forum Qt Programming
    Replies: 1
    Last Post: 9th July 2009, 01:49
  4. Qwt overrides Widget background
    By garfield in forum Qwt
    Replies: 1
    Last Post: 18th December 2007, 08:16
  5. QByteArray in Qt3
    By joseph in forum Qt Programming
    Replies: 1
    Last Post: 6th September 2007, 06:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.