QByteArray and QString

QString always contains UTF-16 encoded strings, but what if you have data in an unknown (yet) encoding? Also, what if the data is not even text? In these cases, Qt uses the QByteArray class. When you read data directly from a file or receive it from a network socket, Qt will return the data as a QByteArray, indicating that this is an arbitrary array of bytes without any information about the encoding:

QFile file("/path/to/file");
file.open(QFile::ReadOnly);
QByteArray array = file.readAll();

The closest equivalent of QByteArray in the standard library would be std::vector<char>. As the name implies, this is just an array of bytes with some helpful methods. In the preceding example, if you know that the file you read ...

Get Game Programming using Qt 5 Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.