CopperSpice API  1.9.1
QDataStream Class Reference

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

Public Types

enum  ByteOrder
 
enum  FloatingPointPrecision
 
enum  Status
 
enum  Version
 

Public Methods

 QDataStream ()
 
 QDataStream (const QByteArray &buffer)
 
 QDataStream (QByteArray *buffer, QIODevice::OpenMode mode)
 
 QDataStream (QIODevice *device)
 
virtual ~QDataStream ()
 
bool atEnd () const
 
ByteOrder byteOrder () const
 
QIODevicedevice () const
 
FloatingPointPrecision floatingPointPrecision () const
 
QDataStream & operator<< (bool i)
 
QDataStream & operator<< (const char *str)
 
QDataStream & operator<< (double f)
 
QDataStream & operator<< (float f)
 
QDataStream & operator<< (long i)
 
QDataStream & operator<< (qint16 i)
 
QDataStream & operator<< (qint32 i)
 
QDataStream & operator<< (qint64 i)
 
QDataStream & operator<< (qint8 i)
 
QDataStream & operator<< (quint16 i)
 
QDataStream & operator<< (quint32 i)
 
QDataStream & operator<< (quint64 i)
 
QDataStream & operator<< (quint8 i)
 
QDataStream & operator<< (unsigned long i)
 
QDataStream & operator>> (bool &i)
 
QDataStream & operator>> (char *&str)
 
QDataStream & operator>> (double &f)
 
QDataStream & operator>> (float &f)
 
QDataStream & operator>> (long &i)
 
QDataStream & operator>> (qint16 &i)
 
QDataStream & operator>> (qint32 &i)
 
QDataStream & operator>> (qint64 &i)
 
QDataStream & operator>> (qint8 &i)
 
QDataStream & operator>> (quint16 &i)
 
QDataStream & operator>> (quint32 &i)
 
QDataStream & operator>> (quint64 &i)
 
QDataStream & operator>> (quint8 &i)
 
QDataStream & operator>> (unsigned long &i)
 
QDataStream & readBytes (char *&buffer, uint &len)
 
int readRawData (char *buffer, int len)
 
void resetStatus ()
 
void setByteOrder (ByteOrder order)
 
void setDevice (QIODevice *device)
 
void setFloatingPointPrecision (FloatingPointPrecision precision)
 
void setStatus (Status status)
 
void setVersion (int version)
 
int skipRawData (int len)
 
Status status () const
 
int version () const
 
QDataStream & writeBytes (const char *buffer, uint len)
 
int writeRawData (const char *buffer, int len)
 

Related Functions

These are not member functions

QDataStream & operator<< (QDataStream &stream, const QFlatMap< Key, Val, C > &flatmap)
 
QDataStream & operator<< (QDataStream &stream, const QHash< Key, Val, Hash, KeyEqual > &hash)
 
QDataStream & operator<< (QDataStream &stream, const QLinkedList< T > &list)
 
QDataStream & operator<< (QDataStream &stream, const QList< T > &list)
 
QDataStream & operator<< (QDataStream &stream, const QMap< Key, Val, C > &map)
 
QDataStream & operator<< (QDataStream &stream, const QMultiHash< Key, Val, Hash, KeyEqual > &hash)
 
QDataStream & operator<< (QDataStream &stream, const QMultiMap< Key, Val, C > &map)
 
QDataStream & operator<< (QDataStream &stream, const QSet< T > &set)
 
QDataStream & operator<< (QDataStream &stream, const QVector< T > &vector)
 
QDataStream & operator>> (QDataStream &stream, QFlatMap< Key, Val, C > &flatmap)
 
QDataStream & operator>> (QDataStream &stream, QHash< Key, Val, Hash, KeyEqual > &hash)
 
QDataStream & operator>> (QDataStream &stream, QLinkedList< T > &list)
 
QDataStream & operator>> (QDataStream &stream, QList< T > &list)
 
QDataStream & operator>> (QDataStream &stream, QMap< Key, Val, C > &map)
 
QDataStream & operator>> (QDataStream &stream, QMultiHash< Key, Val, Hash, KeyEqual > &hash)
 
QDataStream & operator>> (QDataStream &stream, QMultiMap< Key, Val, C > &map)
 
QDataStream & operator>> (QDataStream &stream, QSet< T > &set)
 
QDataStream & operator>> (QDataStream &stream, QVector< T > &vector)
 

Detailed Description

The QDataStream class provides serialization of binary data to 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.

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. You can use a data stream to read/write raw unencoded binary data. If you want a text based input stream refer to 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.

The following is an example to 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 << QString("the answer is"); // serialize a string
out << (qint32)42; // serialize an integer

The following is an example to 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;
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 types include QBrush, QColor, QDateTime, QFont, QPixmap, QString, QVariant and many others. For the complete list of supported streaming data types refer to Serializing Data Types.

For integers it is best to always cast to a CopperSpice integer type for writing, and to read back into the same CopperSpice 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

When inputting or outputting complex types, it is very important to make sure 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::CS_1_7);

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.

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::CS_1_7);
// Write the data
out << lots_of_interesting_data;

Then read it in with the following code.

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
qint32version;
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::CS_1_6);
} else {
in.setVersion(QDataStream::CS_1_7);
}
// 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.

Raw binary data

Data may be read from a stream into a preallocated char * using readRawData(). In addition, data can be written to a stream using writeRawData(). Any encoding/decoding of the data must be done in your program.

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. Any encoding/decoding of the data (apart from the length quint32) must be done in your code.

Containers

The CopperSpice container classes can be written to or read from a QDataStream. These classes include QList, QLinkedList, QVector, QSet, QHash, QMultiMap, QMap, and QMultiMap. Most of the stream operators are functions.

Other Classes

The following is an example of how a non container class supports writing to or reading from a QDataStream. QXxx can be a variety of different classes. Review the QDataStream documentation for a full list.

The following operator functions support reading and writing a QImage.

QDataStream &operator<< (QDataStream & stream, const QImage &image);
See also
QTextStream, QVariant

Member Enumeration Documentation

The byte order used for reading/writing the data.

ConstantValueDescription
QDataStream::BigEndianQSysInfo::BigEndianMost significant byte first (the default)
QDataStream::LittleEndianQSysInfo::LittleEndianLeast significant byte first

The precision of floating point numbers used for reading/writing the data.

Warning
The floating point precision must be set to the same value on the object that writes and the object that reads the data stream.
ConstantValueDescription
QDataStream::SinglePrecision0All floating point numbers in the data stream have 32-bit precision.
QDataStream::DoublePrecision1All floating point numbers in the data stream have 64-bit precision.
See also
setFloatingPointPrecision(), floatingPointPrecision()

This enum describes the current status of the data stream.

ConstantValueDescription
QDataStream::Ok0The data stream is operating normally.
QDataStream::ReadPastEnd1The data stream has read past the end of the data in the underlying device.
QDataStream::ReadCorruptData2The data stream has read corrupt data.
QDataStream::WriteFailed3The data stream can not write to the underlying device.

This enum provides symbolic synonyms for the data serialization format version numbers.

ConstantValueDescription
QDataStream::CS_1_0 128 Version 1.0
QDataStream::CS_1_1 128 Version 1.0
QDataStream::CS_1_2 128 Version 1.0
QDataStream::CS_1_3 128 Version 1.0
QDataStream::CS_1_4 128 Version 1.0
QDataStream::CS_1_5 128 Version 1.0
QDataStream::CS_1_6 128 Version 1.0
QDataStream::CS_1_7 128 Version 1.0
See also
setVersion(), version()

Constructor & Destructor Documentation

QDataStream::QDataStream ( )

Constructs a data stream that has no I/O device.

See also
setDevice()
QDataStream::QDataStream ( QIODevice device)
explicit

Constructs a data stream that uses the given I/O device.

Warning
If you use QSocket or QSocketDevice as the I/O device d for reading data, you must make sure that enough data is available on the socket for the operation to successfully proceed; QDataStream does not have any means to handle or recover from short-reads.
See also
setDevice(), device()
QDataStream::QDataStream ( QByteArray buffer,
QIODevice::OpenMode  mode 
)

Constructs a data stream that operates on a byte array buffer. The mode describes how the device is to be used. Alternatively, you can use QDataStream(const QByteArray &) if you just want to read from a byte array.

QDataStream::QDataStream ( const QByteArray buffer)

Constructs a read only data stream that operates on byte array buffer. Use QDataStream(QByteArray*, int) if you want to write to a byte array.

QDataStream::~QDataStream ( )
virtual

Destroys the data stream.

The destructor will not affect the current I/O device, unless it is an internal I/O device (e.g. a QBuffer) processing a QByteArray passed in the constructor, in which case the internal I/O device is destroyed.

Method Documentation

bool QDataStream::atEnd ( ) const

Returns true if the I/O device has reached the end position (end of the stream or file) or if there is no I/O device set, otherwise returns false.

See also
QIODevice::atEnd()
QDataStream::ByteOrder QDataStream::byteOrder ( ) const
inline

Returns the current byte order setting, either BigEndian or LittleEndian.

See also
setByteOrder()
QIODevice * QDataStream::device ( ) const
inline

Returns the I/O device currently set or a nullptr if no device is currently set.

See also
setDevice()
FloatingPointPrecision QDataStream::floatingPointPrecision ( ) const

Returns the floating point precision of the data stream.

See also
FloatingPointPrecision, setFloatingPointPrecision()
QDataStream & QDataStream::operator<< ( bool  i)

Writes a boolean i to the stream and returns a reference to the stream.

QDataStream & QDataStream::operator<< ( const char *  str)

Writes a null terminated string str to the stream and returns a reference to the stream. The string is serialized using writeBytes().

QDataStream & QDataStream::operator<< ( double  f)

Writes a floating point number f to the stream using the standard IEEE 754 format. Returns a reference to the stream.

See also
setFloatingPointPrecision()
QDataStream & QDataStream::operator<< ( float  f)

Writes a floating point number f to the stream using the standard IEEE 754 format. Returns a reference to the stream.

See also
setFloatingPointPrecision()
QDataStream & QDataStream::operator<< ( long  i)

Writes a long i to the stream and returns a reference to the stream.

QDataStream & QDataStream::operator<< ( qint16  i)

Writes a signed 16-bit integer i to the stream and returns a reference to the stream.

QDataStream & QDataStream::operator<< ( qint32  i)

Writes a signed 32-bit integer i to the stream and returns a reference to the stream.

QDataStream & QDataStream::operator<< ( qint64  i)

Writes a signed 64-bit integer i to the stream and returns a reference to the stream.

QDataStream & QDataStream::operator<< ( qint8  i)

Writes a signed 8-bit integer i to the stream and returns a reference to the stream.

QDataStream & QDataStream::operator<< ( quint16  i)
inline

Writes an unsigned 16-bit integer i to the stream and returns a reference to the stream.

QDataStream & QDataStream::operator<< ( quint32  i)
inline

Writes an unsigned 32-bit integer i to the stream and returns a reference to the stream.

QDataStream & QDataStream::operator<< ( quint64  i)
inline

Writes an unsigned 64-bit integer i to the stream and returns a reference to the stream.

QDataStream & QDataStream::operator<< ( quint8  i)
inline

Writes an unsigned 8-bit integer i to the stream and returns a reference to the stream.

QDataStream & QDataStream::operator<< ( unsigned long  i)

Writes an unsigned long i to the stream and returns a reference to the stream.

QDataStream & QDataStream::operator>> ( bool &  i)

Reads a boolean from the stream into i and returns a reference to the stream.

QDataStream & QDataStream::operator>> ( char *&  str)

Reads a null terminated string str from the stream and returns a reference to the stream. Space for the string is allocated using new, the caller must destroy it with delete[].

QDataStream & QDataStream::operator>> ( double &  f)

Reads a floating point number from the stream into f using the standard IEEE 754 format. Returns a reference to the stream.

See also
setFloatingPointPrecision()
QDataStream & QDataStream::operator>> ( float &  f)

Reads a floating point number from the stream into f using the standard IEEE 754 format. Returns a reference to the stream.

See also
setFloatingPointPrecision()
QDataStream & QDataStream::operator>> ( long &  i)

Reads a long from the stream into i and returns a reference to the stream.

QDataStream & QDataStream::operator>> ( qint16 i)

Reads a signed 16-bit integer from the stream into i and returns a reference to the stream.

QDataStream & QDataStream::operator>> ( qint32 i)

Reads a signed 32-bit integer from the stream into i and returns a reference to the stream.

QDataStream & QDataStream::operator>> ( qint64 i)

Reads a signed 64-bit integer from the stream into i and returns a reference to the stream.

QDataStream & QDataStream::operator>> ( qint8 i)

Reads a signed 8-bit integer from the stream into i and returns a reference to the stream.

QDataStream & QDataStream::operator>> ( quint16 i)
inline

Reads an unsigned 16-bit integer from the stream into i and returns a reference to the stream.

QDataStream & QDataStream::operator>> ( quint32 i)
inline

Reads an unsigned 32-bit integer from the stream into i and returns a reference to the stream.

QDataStream & QDataStream::operator>> ( quint64 i)
inline

Reads an unsigned 64-bit integer from the stream into i and returns a reference to the stream.

QDataStream & QDataStream::operator>> ( quint8 i)
inline

Reads an unsigned 8-bit integer from the stream into i and returns a reference to the stream.

QDataStream & QDataStream::operator>> ( unsigned long &  i)

Reads a unsigned long from the stream into i and returns a reference to the stream.

QDataStream & QDataStream::readBytes ( char *&  buffer,
uint len 
)

Reads the buffer from the stream and returns a reference to the stream. The buffer s is allocated using new. Destroy it with the delete[] operator.

The len parameter is set to the length of the buffer. If the string read is empty, len is set to 0 and buffer is set to a null pointer. The serialization format is a quint32 length specifier first, then len bytes of data.

See also
readRawData(), writeBytes()
int QDataStream::readRawData ( char *  buffer,
int  len 
)

Reads at most len bytes from the stream into s and returns the number of bytes read. If an error occurs, this method returns -1. The buffer s must be preallocated. The data is not encoded.

See also
readBytes(), QIODevice::read(), writeRawData()
void QDataStream::resetStatus ( )

Resets the status of the data stream.

See also
Status, status(), setStatus()
void QDataStream::setByteOrder ( ByteOrder  order)

Sets the serialization byte order to order. The order parameter can be QDataStream::BigEndian or QDataStream::LittleEndian.

The default setting is big endian. We recommend leaving this setting unless you have special requirements.

See also
byteOrder()
void QDataStream::setDevice ( QIODevice device)

Sets the I/O device to device which can be nullptr to unset the current I/O device.

See also
device()
void QDataStream::setFloatingPointPrecision ( FloatingPointPrecision  precision)

Sets the floating point precision of the data stream to precision.

If the floating point precision is DoublePrecision all floating point numbers will be written and read with 64-bit precision. If the floating point precision is SinglePrecision all floating point numbers will be written and read with 32-bit precision.

The default is DoublePrecision.

Warning
This property must be set to the same value on the object that writes and the object that reads the data stream.
See also
floatingPointPrecision()
void QDataStream::setStatus ( Status  status)

Sets the status of the data stream to the status given. Subsequent calls to setStatus() are ignored until resetStatus() is called.

See also
Status, status(), resetStatus()
void QDataStream::setVersion ( int  version)
inline

Sets the version number of the data serialization format to version.

To accommodate new functionality the datastream serialization format may change. If you want to read data which was created by an earlier version of CopperSpice or write data which can be read by a program compiled with an earlier version, use this function to modify the serialization format used by QDataStream.

The Version enum provides symbolic constants for the different versions.

QDataStream out(file);
out.setVersion(QDataStream::CS_1_7);
See also
version(), Version
int QDataStream::skipRawData ( int  len)

Skips len bytes from the device. Returns the number of bytes actually skipped, or -1 on error. This is equivalent to calling readRawData() on a buffer of length len and ignoring the buffer.

See also
QIODevice::seek()
Status QDataStream::status ( ) const

Returns the status of the data stream.

See also
Status, setStatus(), resetStatus()
int QDataStream::version ( ) const
inline

Returns the version number of the data serialization format.

See also
setVersion(), Version
QDataStream & QDataStream::writeBytes ( const char *  buffer,
uint  len 
)

Writes the length specifier len and buffer to the stream and returns a reference to the stream. The len is serialized as a quint32, followed by len bytes from buffer. The data is not encoded.

See also
writeRawData(), readBytes()
int QDataStream::writeRawData ( const char *  buffer,
int  len 
)

Writes len bytes from buffer to the stream. Returns the number of bytes actually written, or -1 on error. The data is not encoded.

See also
writeBytes(), QIODevice::write(), readRawData()

Friends And Related Function Documentation

QDataStream & operator<< ( QDataStream &  stream,
const QFlatMap< Key, Val, C > &  flatmap 
)
related

Writes the given flatmap to the stream. Returns a reference to the stream. This function requires the type T to support operator<<().

Refer to Serializing Data Types for additional information.

QDataStream & operator<< ( QDataStream &  stream,
const QHash< Key, Val, Hash, KeyEqual > &  hash 
)
related

Writes the given hash to the stream. Returns a reference to the stream. Requires the type T to support operator<<().

Refer to Serializing Data Types for additional information.

QDataStream & operator<< ( QDataStream &  stream,
const QLinkedList< T > &  list 
)
related

Writes the given list to the stream. Returns a reference to the stream. This function requires the type T to support operator<<().

Refer to Serializing Data Types for additional information.

QDataStream & operator<< ( QDataStream &  stream,
const QList< T > &  list 
)
related

Writes the given list to the stream. Returns a reference to the stream. This function requires the type T to support operator<<().

Refer to Serializing Data Types for additional information.

QDataStream & operator<< ( QDataStream &  stream,
const QMap< Key, Val, C > &  map 
)
related

Writes the given map to the stream. Returns a reference to the stream. This function requires the types Key and Val to support operator<<().

Refer to Serializing Data Types for additional information.

QDataStream & operator<< ( QDataStream &  stream,
const QMultiHash< Key, Val, Hash, KeyEqual > &  hash 
)
related

Writes the given hash to the stream. Returns a reference to the stream. This function requires the type T to support operator<<().

Refer to Serializing Data Types for additional information.

QDataStream & operator<< ( QDataStream &  stream,
const QMultiMap< Key, Val, C > &  map 
)
related

Writes the given map to the stream. Returns a reference to the stream. This function requires the key and value types to implement operator<<().

Refer to Serializing Data Types for additional information.

QDataStream & operator<< ( QDataStream &  stream,
const QSet< T > &  set 
)
related

Writes the given set to the stream. Returns a reference to the stream. Returns a reference to the stream. This function requires the type T to support operator<<().

Refer to Serializing Data Types for additional information.

QDataStream & operator<< ( QDataStream &  stream,
const QVector< T > &  vector 
)
related

Writes the given vector to the stream. Returns a reference to the stream. This function requires the type T to support operator<<().

Refer to Serializing Data Types for additional information.

QDataStream & operator>> ( QDataStream &  stream,
QFlatMap< Key, Val, C > &  flatmap 
)
related

Reads from the stream into the given flatmap. Returns a reference to the stream. This function requires the type T to support operator>>().

Refer to Serializing Data Types for additional information.

QDataStream & operator>> ( QDataStream &  stream,
QHash< Key, Val, Hash, KeyEqual > &  hash 
)
related

Reads from the stream into the given hash. Returns a reference to the stream. Requires the type T to support operator>>().

Refer to Serializing Data Types for additional information.

QDataStream & operator>> ( QDataStream &  stream,
QLinkedList< T > &  list 
)
related

Reads from the stream into the given list. Returns a reference to the stream. This function requires the type T to support operator>>().

Refer to Serializing Data Types for additional information.

QDataStream & operator>> ( QDataStream &  stream,
QList< T > &  list 
)
related

Reads from the stream into the given list. Returns a reference to the stream. This function requires the type T to support operator>>().

Refer to Serializing Data Types for additional information.

QDataStream & operator>> ( QDataStream &  stream,
QMap< Key, Val, C > &  map 
)
related

Reads from the stream into the given map. Returns a reference to the stream. This function requires the types Key and Val to support operator>>().

Refer to Serializing Data Types for additional information.

QDataStream & operator>> ( QDataStream &  stream,
QMultiHash< Key, Val, Hash, KeyEqual > &  hash 
)
related

Reads from the stream into the given hash. Returns a reference to the stream. This function requires the type T to support operator>>().

Refer to Serializing Data Types for additional information.

QDataStream & operator>> ( QDataStream &  stream,
QMultiMap< Key, Val, C > &  map 
)
related

Reads from the stream into the given map. Returns a reference to the stream. This function requires the key and value types to implement operator>>().

Refer to Serializing Data Types for additional information.

QDataStream & operator>> ( QDataStream &  stream,
QSet< T > &  set 
)
related

Reads from the stream into the given set. Returns a reference to the stream. This function requires the type T to support operator>>().

Refer to Serializing Data Types for additional information.

QDataStream & operator>> ( QDataStream &  stream,
QVector< T > &  vector 
)
related

Reads from the stream into the given vector. Returns a reference to the stream. This function requires the type T to support operator>>().

Refer to Serializing Data Types for additional information.