Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-53796

QDataStream::readBytes and ::writeBytes overloads with QByteArray

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Unresolved
    • Not Evaluated
    • None
    • 5.4.2
    • Core: I/O
    • None

    Description

      QDataStream::readBytes is cumbersome to use and not very c++ : you need to pass a reference to a free pointer and a reference to a size, the function will set this pointer to a buffer allocated with [] and set the size accordingly:

      uint size = 0;
      char * buf = 0;
      datastream.readBytes(buf, size);
      
      //Do something with the data...
      
      delete[] buf;
      
      

      This is a pure C approach and prone to memory leaks.

      Why not just returning a QByteArray?

      Same for QDataStream::writeBytes, but this is less a problem as one can write:

      QDataStream::writeBytes(ba.data(), ba.size());

      We just need :

      QByteArray ba;
      datastream.writeBytes(ba);
      
      QByteArray ba = datastream.readBytes();
      
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            laknoll Lars Knoll
            galinette Etienne Sandré-Chardonnal
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes