Search code examples
qtqlistqbytearray

What is the difference between QByteArray and QList<unsigned char> or QVector<unsigned char>?


QByteArray holds individual bytes, and if we make a QList of unsigned chars that will also hold the individual bytes.

What is the reason QByteArray exists when there can made a QList<unsigned char>?
What is the difference between QByteArray and QList<unsigned char> or QVector<unsigned char>?

What points am I missing?


Solution

  • QByteArray is a usefull wrapper around char*. Suitable for streaming with QDataStream, string operations and other data management. From the docs you also can find that:

    Behind the scenes, it always ensures that the data is followed by a '\0' terminator, and uses implicit sharing (copy-on-write) to reduce memory usage and avoid needless copying of data.

    QList, at first is not linear(subsequent) in memory (you should use QVector), and have no such usefull API