QString str = "Hello world!"; QByteArray utf8Data = str.toUtf8();
QByteArray buffer; QFile file("file.bin"); if (file.open(QIODevice::ReadOnly)) { buffer = file.readAll(); file.close(); }In this example, the QFile class is used to open a binary file called "file.bin" in read-only mode. The readAll() function is used to read all the data from the file and store it in a QByteArray object called buffer. Finally, the file is closed using the close() function. The QByteArray class is part of the Qt core library, so it is included in the main Qt package.