Home · Overviews · Examples 

QDataStream Class Reference
[com.trolltech.qt.core module]

The QDataStream class provides serialization of binary data to a QIODevice. More...


Detailed Description

The QDataStream class provides serialization of binary data to a QIODevice.

A data stream is a binary stream of encoded information which is 100% independent of the host computer's operating system, CPU or byte order. For example, a data stream that is written by a PC under Windows can be read by a Sun SPARC running Solaris.

You can also use a data stream to read/write raw unencoded binary data. If you want a "parsing" input stream, see QTextStream.

The QDataStream class implements the serialization of C++'s basic data types, like char, short, int, char *, etc. Serialization of more complex data is accomplished by breaking up the data into primitive units.

A data stream cooperates closely with a QIODevice. A QIODevice represents an input/output medium one can read data from and write data to. The QFile class is an example of an I/O device.

Example (write binary data to a stream):

    QFile file("file.dat");
    file.open(QIODevice::WriteOnly);
    QDataStream out(&file);   // we will serialize the data into the file
    out << "the answer is";   // serialize a string
    out << (qint32)42;        // serialize an integer

Example (read binary data from a stream):

    QFile file("file.dat");
    file.open(QIODevice::ReadOnly);
    QDataStream in(&file);    // read the data serialized from the file
    QString str;
    qint32 a;
    in >> str >> a;           // extract "the answer is" and 42

Each item written to the stream is written in a predefined binary format that varies depending on the item's type. Supported Qt types include QBrush, QColor, QDateTime, QFont, QPixmap, QString, QVariant and many others. For the complete list of all Qt types supporting data streaming see the Format of the QDataStream operators.

For integers it is best to always cast to a Qt integer type for writing, and to read back into the same Qt integer type. This ensures that you get integers of the size you want and insulates you from compiler and platform differences.

To take one example, a char * string is written as a 32-bit integer equal to the length of the string including the '\0' byte, followed by all the characters of the string including the '\0' byte. When reading a char * string, 4 bytes are read to create the 32-bit length value, then that many characters for the char * string including the '\0' terminator are read.

The initial I/O device is usually set in the constructor, but can be changed with setDevice. If you've reached the end of the data (or if there is no I/O device set) atEnd will return true.

Versioning

QDataStream's binary format has evolved since Qt 1.0, and is likely to continue evolving to reflect changes done in Qt. When inputting or outputting complex types, it's very important to make sure that the same version of the stream (version) is used for reading and writing. If you need both forward and backward compatibility, you can hardcode the version number in the application:

    stream.setVersion(QDataStream::Qt_4_0);

If you are producing a new binary data format, such as a file format for documents created by your application, you could use a QDataStream to write the data in a portable format. Typically, you would write a brief header containing a magic string and a version number to give yourself room for future expansion. For example:

    QFile file("file.xxx");
    file.open(QIODevice::WriteOnly);
    QDataStream out(&file);

    // Write a header with a "magic number" and a version
    out << (quint32)0xA0B0C0D0;
    out << (qint32)123;

    out.setVersion(QDataStream::Qt_4_0);

    // Write the data
    out << lots_of_interesting_data;

Then read it in with:

    QFile file("file.xxx");
    file.open(QIODevice::ReadOnly);
    QDataStream in(&file);

    // Read and check the header
    quint32 magic;
    in >> magic;
    if (magic != 0xA0B0C0D0)
        return XXX_BAD_FILE_FORMAT;

    // Read the version
    qint32 version;
    in >> version;
    if (version < 100)
        return XXX_BAD_FILE_TOO_OLD;
    if (version > 123)
        return XXX_BAD_FILE_TOO_NEW;

    if (version <= 110)
        in.setVersion(QDataStream::Qt_3_2);
    else
        in.setVersion(QDataStream::Qt_4_0);

    // Read the data
    in >> lots_of_interesting_data;
    if (version >= 120)
        in >> data_new_in_XXX_version_1_2;
    in >> other_interesting_data;

You can select which byte order to use when serializing data. The default setting is big endian (MSB first). Changing it to little endian breaks the portability (unless the reader also changes to little endian). We recommend keeping this setting unless you have special requirements.

Reading and writing raw binary data

You may wish to read/write your own raw binary data to/from the data stream directly. Data may be read from the stream into a preallocated char * using readRawData(). Similarly data can be written to the stream using writeRawData(). Note that any encoding/decoding of the data must be done by you.

A similar pair of functions is readBytes() and writeBytes(). These differ from their raw counterparts as follows: readBytes() reads a quint32 which is taken to be the length of the data to be read, then that number of bytes is read into the preallocated char *; writeBytes() writes a quint32 containing the length of the data, followed by the data. Note that any encoding/decoding of the data (apart from the length quint32) must be done by you.

See also QTextStream and QVariant.


Copyright © 2008 Trolltech Trademarks
Qt Jambi 4.3.4_01