Qt 4.8
Public Types | Public Functions | Properties | List of all members
QDataStream Class Reference

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

#include <qdatastream.h>

Inheritance diagram for QDataStream:
QPacket QPacketAutoSend

Public Types

enum  ByteOrder { BigEndian = QSysInfo::BigEndian, LittleEndian = QSysInfo::LittleEndian }
 The byte order used for reading/writing the data. More...
 
enum  FloatingPointPrecision { SinglePrecision, DoublePrecision }
 The precision of floating point numbers used for reading/writing the data. More...
 
enum  Status { Ok, ReadPastEnd, ReadCorruptData, WriteFailed }
 This enum describes the current status of the data stream. More...
 
enum  Version {
  Qt_1_0 = 1, Qt_2_0 = 2, Qt_2_1 = 3, Qt_3_0 = 4,
  Qt_3_1 = 5, Qt_3_3 = 6, Qt_4_0 = 7, Qt_4_1 = Qt_4_0,
  Qt_4_2 = 8, Qt_4_3 = 9, Qt_4_4 = 10, Qt_4_5 = 11,
  Qt_4_6 = 12, Qt_4_7 = Qt_4_6, Qt_4_8 = Qt_4_7
}
 This enum provides symbolic synonyms for the data serialization format version numbers. More...
 

Public Functions

bool 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. More...
 
ByteOrder byteOrder () const
 Returns the current byte order setting – either BigEndian or LittleEndian. More...
 
QIODevicedevice () const
 Returns the I/O device currently set, or 0 if no device is currently set. More...
 
FloatingPointPrecision floatingPointPrecision () const
 Returns the floating point precision of the data stream. More...
 
QDataStreamoperator<< (qint8 i)
 Writes a signed byte, i, to the stream and returns a reference to the stream. More...
 
QDataStreamoperator<< (quint8 i)
 Writes an unsigned byte, i, to the stream and returns a reference to the stream. More...
 
QDataStreamoperator<< (qint16 i)
 Writes a signed 16-bit integer, i, to the stream and returns a reference to the stream. More...
 
QDataStreamoperator<< (quint16 i)
 Writes an unsigned 16-bit integer, i, to the stream and returns a reference to the stream. More...
 
QDataStreamoperator<< (qint32 i)
 Writes a signed 32-bit integer, i, to the stream and returns a reference to the stream. More...
 
QDataStreamoperator<< (quint32 i)
 Writes an unsigned integer, i, to the stream as a 32-bit unsigned integer (quint32). More...
 
QDataStreamoperator<< (qint64 i)
 Writes a signed 64-bit integer, i, to the stream and returns a reference to the stream. More...
 
QDataStreamoperator<< (quint64 i)
 Writes an unsigned 64-bit integer, i, to the stream and returns a reference to the stream. More...
 
QDataStreamoperator<< (bool i)
 Writes a boolean value, i, to the stream. More...
 
QDataStreamoperator<< (float f)
 Writes a floating point number, f, to the stream using the standard IEEE 754 format. More...
 
QDataStreamoperator<< (double f)
 Writes a floating point number, f, to the stream using the standard IEEE 754 format. More...
 
QDataStreamoperator<< (const char *str)
 Writes the '\0'-terminated string s to the stream and returns a reference to the stream. More...
 
QDataStreamoperator>> (qint8 &i)
 Reads a signed byte from the stream into i, and returns a reference to the stream. More...
 
QDataStreamoperator>> (quint8 &i)
 Reads an unsigned byte from the stream into i, and returns a reference to the stream. More...
 
QDataStreamoperator>> (qint16 &i)
 Reads a signed 16-bit integer from the stream into i, and returns a reference to the stream. More...
 
QDataStreamoperator>> (quint16 &i)
 Reads an unsigned 16-bit integer from the stream into i, and returns a reference to the stream. More...
 
QDataStreamoperator>> (qint32 &i)
 Reads a signed 32-bit integer from the stream into i, and returns a reference to the stream. More...
 
QDataStreamoperator>> (quint32 &i)
 Reads an unsigned 32-bit integer from the stream into i, and returns a reference to the stream. More...
 
QDataStreamoperator>> (qint64 &i)
 Reads a signed 64-bit integer from the stream into i, and returns a reference to the stream. More...
 
QDataStreamoperator>> (quint64 &i)
 Reads an unsigned 64-bit integer from the stream, into i, and returns a reference to the stream. More...
 
QDataStreamoperator>> (bool &i)
 Reads a boolean value from the stream into i. More...
 
QDataStreamoperator>> (float &f)
 Reads a floating point number from the stream into f, using the standard IEEE 754 format. More...
 
QDataStreamoperator>> (double &f)
 Reads a floating point number from the stream into f, using the standard IEEE 754 format. More...
 
QDataStreamoperator>> (char *&str)
 Reads the '\0'-terminated string s from the stream and returns a reference to the stream. More...
 
 QDataStream ()
 Constructs a data stream that has no I/O device. More...
 
 QDataStream (QIODevice *)
 Constructs a data stream that uses the I/O device d. More...
 
 QDataStream (QByteArray *, QIODevice::OpenMode flags)
 Constructs a data stream that operates on a byte array, a. More...
 
 QDataStream (const QByteArray &)
 Constructs a read-only data stream that operates on byte array a. More...
 
QDataStreamreadBytes (char *&, uint &len)
 Reads the buffer s from the stream and returns a reference to the stream. More...
 
int readRawData (char *, int len)
 Reads at most len bytes from the stream into s and returns the number of bytes read. More...
 
void resetStatus ()
 Resets the status of the data stream. More...
 
void setByteOrder (ByteOrder)
 Sets the serialization byte order to bo. More...
 
void setDevice (QIODevice *)
 void QDataStream::setDevice(QIODevice *d) More...
 
void setFloatingPointPrecision (FloatingPointPrecision precision)
 Sets the floating point precision of the data stream to precision. More...
 
void setStatus (Status status)
 Sets the status of the data stream to the status given. More...
 
void setVersion (int)
 Sets the version number of the data serialization format to v. More...
 
int skipRawData (int len)
 Skips len bytes from the device. More...
 
Status status () const
 Returns the status of the data stream. More...
 
void unsetDevice ()
 Unsets the I/O device. More...
 
int version () const
 Returns the version number of the data serialization format. More...
 
QDataStreamwriteBytes (const char *, uint len)
 Writes the length specifier len and the buffer s to the stream and returns a reference to the stream. More...
 
int writeRawData (const char *, int len)
 Writes len bytes from s to the stream. More...
 
virtual ~QDataStream ()
 Destroys the data stream. More...
 

Properties

ByteOrder byteorder
 
QScopedPointer< QDataStreamPrivated
 
QIODevicedev
 
bool noswap
 
bool owndev
 
Status q_status
 
int ver
 

Detailed Description

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

Note
This class or function is reentrant.

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");
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

Example (read binary data from a stream):

QFile file("file.dat");
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 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 Serializing Qt Data Types.

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:

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");
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");
QDataStream in(&file);
// Read and check the header
in >> magic;
if (magic != 0xA0B0C0D0)
return XXX_BAD_FILE_FORMAT;
// Read the 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.

Reading and writing Qt collection classes

The Qt container classes can also be serialized to a QDataStream. These include QList, QLinkedList, QVector, QSet, QHash, and QMap. The stream operators are declared as non-members of the classes.

.

Reading and writing other Qt classes.

In addition to the overloaded stream operators documented here, any Qt classes that you might want to serialize to a QDataStream will have appropriate stream operators declared as non-member of the class:

For example, here are the stream operators declared as non-members of the QImage class:

To see if your favorite Qt class has similar stream operators defined, check the Related Non-Members section of the class's documentation page.

See also
QTextStream QVariant

Definition at line 71 of file qdatastream.h.

Enumerations

◆ ByteOrder

The byte order used for reading/writing the data.

  • BigEndian Most significant byte first (the default)
  • LittleEndian Least significant byte first
Enumerator
BigEndian 
LittleEndian 

Definition at line 96 of file qdatastream.h.

96  {
97  BigEndian = QSysInfo::BigEndian,
98  LittleEndian = QSysInfo::LittleEndian
99  };

◆ FloatingPointPrecision

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

This will only have an effect if the version of the data stream is Qt_4_6 or higher.

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.
  • SinglePrecision All floating point numbers in the data stream have 32-bit precision.
  • DoublePrecision All floating point numbers in the data stream have 64-bit precision.
See also
setFloatingPointPrecision(), floatingPointPrecision()
Enumerator
SinglePrecision 
DoublePrecision 

Definition at line 108 of file qdatastream.h.

◆ Status

This enum describes the current status of the data stream.

  • Ok The data stream is operating normally.
  • ReadPastEnd The data stream has read past the end of the data in the underlying device.
  • ReadCorruptData The data stream has read corrupt data.
  • WriteFailed The data stream cannot write to the underlying device.
Enumerator
Ok 
ReadPastEnd 
ReadCorruptData 
WriteFailed 

Definition at line 101 of file qdatastream.h.

◆ Version

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

  • Qt_1_0 Version 1 (Qt 1.x)
  • Qt_2_0 Version 2 (Qt 2.0)
  • Qt_2_1 Version 3 (Qt 2.1, 2.2, 2.3)
  • Qt_3_0 Version 4 (Qt 3.0)
  • Qt_3_1 Version 5 (Qt 3.1, 3.2)
  • Qt_3_3 Version 6 (Qt 3.3)
  • Qt_4_0 Version 7 (Qt 4.0, Qt 4.1)
  • Qt_4_1 Version 7 (Qt 4.0, Qt 4.1)
  • Qt_4_2 Version 8 (Qt 4.2)
  • Qt_4_3 Version 9 (Qt 4.3)
  • Qt_4_4 Version 10 (Qt 4.4)
  • Qt_4_5 Version 11 (Qt 4.5)
  • Qt_4_6 Version 12 (Qt 4.6, Qt 4.7, Qt 4.8)
  • Qt_4_7 Same as Qt_4_6.
  • Qt_4_8 Same as Qt_4_6.
See also
setVersion(), version()
Enumerator
Qt_1_0 
Qt_2_0 
Qt_2_1 
Qt_3_0 
Qt_3_1 
Qt_3_3 
Qt_4_0 
Qt_4_1 
Qt_4_2 
Qt_4_3 
Qt_4_4 
Qt_4_5 
Qt_4_6 
Qt_4_7 
Qt_4_8 

Definition at line 74 of file qdatastream.h.

74  {
75  Qt_1_0 = 1,
76  Qt_2_0 = 2,
77  Qt_2_1 = 3,
78  Qt_3_0 = 4,
79  Qt_3_1 = 5,
80  Qt_3_3 = 6,
81  Qt_4_0 = 7,
82  Qt_4_1 = Qt_4_0,
83  Qt_4_2 = 8,
84  Qt_4_3 = 9,
85  Qt_4_4 = 10,
86  Qt_4_5 = 11,
87  Qt_4_6 = 12,
88  Qt_4_7 = Qt_4_6,
89  Qt_4_8 = Qt_4_7
90 #if QT_VERSION >= 0x040900
91 #error Add the datastream version for this Qt version
92  Qt_4_9 = Qt_4_8
93 #endif
94  };

Constructors and Destructors

◆ QDataStream() [1/4]

QDataStream::QDataStream ( )

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

See also
setDevice()

Definition at line 276 of file qdatastream.cpp.

Referenced by QDataStream().

277 {
278  dev = 0;
279  owndev = false;
282  noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian;
283  q_status = Ok;
284 }
QIODevice * dev
Definition: qdatastream.h:193
Status q_status
Definition: qdatastream.h:198
ByteOrder byteorder
Definition: qdatastream.h:196

◆ QDataStream() [2/4]

QDataStream::QDataStream ( QIODevice d)
explicit

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

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()

Definition at line 298 of file qdatastream.cpp.

299 {
300  dev = d; // set device
301  owndev = false;
302  byteorder = BigEndian; // default byte order
304  noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian;
305  q_status = Ok;
306 }
QIODevice * dev
Definition: qdatastream.h:193
Status q_status
Definition: qdatastream.h:198
ByteOrder byteorder
Definition: qdatastream.h:196
QScopedPointer< QDataStreamPrivate > d
Definition: qdatastream.h:191

◆ QDataStream() [3/4]

QDataStream::QDataStream ( QByteArray a,
QIODevice::OpenMode  mode 
)

Constructs a data stream that operates on a byte array, a.

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.

Since QByteArray is not a QIODevice subclass, internally a QBuffer is created to wrap the byte array.

Definition at line 346 of file qdatastream.cpp.

347 {
348  QBuffer *buf = new QBuffer(a);
349 #ifndef QT_NO_QOBJECT
350  buf->blockSignals(true);
351 #endif
352  buf->open(flags);
353  dev = buf;
354  owndev = true;
357  noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian;
358  q_status = Ok;
359 }
bool blockSignals(bool b)
If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke...
Definition: qobject.cpp:1406
bool open(OpenMode openMode)
Reimplemented Function
Definition: qbuffer.cpp:338
QIODevice * dev
Definition: qdatastream.h:193
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
Status q_status
Definition: qdatastream.h:198
ByteOrder byteorder
Definition: qdatastream.h:196

◆ QDataStream() [4/4]

QDataStream::QDataStream ( const QByteArray a)

Constructs a read-only data stream that operates on byte array a.

Use QDataStream(QByteArray*, int) if you want to write to a byte array.

Since QByteArray is not a QIODevice subclass, internally a QBuffer is created to wrap the byte array.

Definition at line 369 of file qdatastream.cpp.

370 {
371  QBuffer *buf = new QBuffer;
372 #ifndef QT_NO_QOBJECT
373  buf->blockSignals(true);
374 #endif
375  buf->setData(a);
377  dev = buf;
378  owndev = true;
381  noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian;
382  q_status = Ok;
383 }
bool blockSignals(bool b)
If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke...
Definition: qobject.cpp:1406
void setData(const QByteArray &data)
Sets the contents of the internal buffer to be data.
Definition: qbuffer.cpp:315
bool open(OpenMode openMode)
Reimplemented Function
Definition: qbuffer.cpp:338
QIODevice * dev
Definition: qdatastream.h:193
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
Status q_status
Definition: qdatastream.h:198
ByteOrder byteorder
Definition: qdatastream.h:196

◆ ~QDataStream()

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.

Definition at line 394 of file qdatastream.cpp.

395 {
396  if (owndev)
397  delete dev;
398 }
QIODevice * dev
Definition: qdatastream.h:193

Functions

◆ atEnd()

bool QDataStream::atEnd ( ) const

◆ byteOrder()

QDataStream::ByteOrder QDataStream::byteOrder ( ) const
inline

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

See also
setByteOrder()

Definition at line 209 of file qdatastream.h.

Referenced by QUuid::operator<<(), operator<<(), QUuid::operator>>(), and operator>>().

210 { return byteorder; }
ByteOrder byteorder
Definition: qdatastream.h:196

◆ device()

QIODevice * QDataStream::device ( ) const
inline

Returns the I/O device currently set, or 0 if no device is currently set.

See also
setDevice()

Definition at line 206 of file qdatastream.h.

Referenced by QPicture::exec(), operator<<(), operator>>(), QPicture::play(), qt_read_dibv5(), qt_write_dib(), qt_write_dibv5(), and read_dib_body().

207 { return dev; }
QIODevice * dev
Definition: qdatastream.h:193

◆ floatingPointPrecision()

QDataStream::FloatingPointPrecision QDataStream::floatingPointPrecision ( ) const

Returns the floating point precision of the data stream.

Since
4.6
See also
FloatingPointPrecision setFloatingPointPrecision()

Definition at line 468 of file qdatastream.cpp.

Referenced by operator<<(), and operator>>().

469 {
471 }
QDataStream::FloatingPointPrecision floatingPointPrecision
Definition: qdatastream_p.h:66
QScopedPointer< QDataStreamPrivate > d
Definition: qdatastream.h:191

◆ operator<<() [1/12]

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

Writes a signed byte, i, to the stream and returns a reference to the stream.

Definition at line 1071 of file qdatastream.cpp.

1072 {
1074  if (!dev->putChar(i))
1076  return *this;
1077 }
QIODevice * dev
Definition: qdatastream.h:193
#define CHECK_STREAM_WRITE_PRECOND(retVal)
Status q_status
Definition: qdatastream.h:198
if(void) toggleToolbarShown
return(isPopup||isToolTip)

◆ operator<<() [2/12]

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

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 230 of file qdatastream.h.

231 { return *this << qint8(i); }
signed char qint8
Definition: qglobal.h:933

◆ operator<<() [3/12]

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

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 1101 of file qdatastream.cpp.

1102 {
1104  if (!noswap) {
1105  i = qbswap(i);
1106  }
1107  if (dev->write((char *)&i, sizeof(qint16)) != sizeof(qint16))
1109  return *this;
1110 }
void qbswap(const T src, uchar *dest)
Definition: qendian.h:74
QIODevice * dev
Definition: qdatastream.h:193
#define CHECK_STREAM_WRITE_PRECOND(retVal)
Status q_status
Definition: qdatastream.h:198
short qint16
Definition: qglobal.h:935
if(void) toggleToolbarShown
qint64 write(const char *data, qint64 len)
Writes at most maxSize bytes of data from data to the device.
Definition: qiodevice.cpp:1342

◆ operator<<() [4/12]

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

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 233 of file qdatastream.h.

234 { return *this << qint16(i); }
short qint16
Definition: qglobal.h:935

◆ operator<<() [5/12]

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

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 1122 of file qdatastream.cpp.

1123 {
1125  if (!noswap) {
1126  i = qbswap(i);
1127  }
1128  if (dev->write((char *)&i, sizeof(qint32)) != sizeof(qint32))
1130  return *this;
1131 }
int qint32
Definition: qglobal.h:937
void qbswap(const T src, uchar *dest)
Definition: qendian.h:74
QIODevice * dev
Definition: qdatastream.h:193
#define CHECK_STREAM_WRITE_PRECOND(retVal)
Status q_status
Definition: qdatastream.h:198
if(void) toggleToolbarShown
qint64 write(const char *data, qint64 len)
Writes at most maxSize bytes of data from data to the device.
Definition: qiodevice.cpp:1342

◆ operator<<() [6/12]

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

Writes an unsigned integer, i, to the stream as a 32-bit unsigned integer (quint32).

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Returns a reference to the stream.

Definition at line 236 of file qdatastream.h.

237 { return *this << qint32(i); }
int qint32
Definition: qglobal.h:937

◆ operator<<() [7/12]

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

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 1154 of file qdatastream.cpp.

1155 {
1157  if (version() < 6) {
1158  quint32 i1 = i & 0xffffffff;
1159  quint32 i2 = i >> 32;
1160  *this << i2 << i1;
1161  } else {
1162  if (!noswap) {
1163  i = qbswap(i);
1164  }
1165  if (dev->write((char *)&i, sizeof(qint64)) != sizeof(qint64))
1167  }
1168  return *this;
1169 }
void qbswap(const T src, uchar *dest)
Definition: qendian.h:74
QIODevice * dev
Definition: qdatastream.h:193
#define CHECK_STREAM_WRITE_PRECOND(retVal)
Status q_status
Definition: qdatastream.h:198
__int64 qint64
Definition: qglobal.h:942
int version() const
Returns the version number of the data serialization format.
Definition: qdatastream.h:212
unsigned int quint32
Definition: qglobal.h:938
if(void) toggleToolbarShown
qint64 write(const char *data, qint64 len)
Writes at most maxSize bytes of data from data to the device.
Definition: qiodevice.cpp:1342

◆ operator<<() [8/12]

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

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 239 of file qdatastream.h.

240 { return *this << qint64(i); }
__int64 qint64
Definition: qglobal.h:942

◆ operator<<() [9/12]

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

Writes a boolean value, i, to the stream.

Returns a reference to the stream.

Definition at line 1187 of file qdatastream.cpp.

1188 {
1190  if (!dev->putChar(qint8(i)))
1192  return *this;
1193 }
QIODevice * dev
Definition: qdatastream.h:193
#define CHECK_STREAM_WRITE_PRECOND(retVal)
signed char qint8
Definition: qglobal.h:933
Status q_status
Definition: qdatastream.h:198
if(void) toggleToolbarShown
return(isPopup||isToolTip)

◆ operator<<() [10/12]

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

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Returns a reference to the stream.

See also
setFloatingPointPrecision()

Definition at line 1207 of file qdatastream.cpp.

1208 {
1209  if (version() >= QDataStream::Qt_4_6
1211  *this << double(f);
1212  return *this;
1213  }
1214 
1216  float g = f; // fixes float-on-stack problem
1217  if (!noswap) {
1218  union {
1219  float val1;
1220  quint32 val2;
1221  } x;
1222  x.val1 = g;
1223  x.val2 = qbswap(x.val2);
1224 
1225  if (dev->write((char *)&x.val2, sizeof(float)) != sizeof(float))
1227  return *this;
1228  }
1229 
1230  if (dev->write((char *)&g, sizeof(float)) != sizeof(float))
1232  return *this;
1233 }
void qbswap(const T src, uchar *dest)
Definition: qendian.h:74
QIODevice * dev
Definition: qdatastream.h:193
#define CHECK_STREAM_WRITE_PRECOND(retVal)
Status q_status
Definition: qdatastream.h:198
int version() const
Returns the version number of the data serialization format.
Definition: qdatastream.h:212
unsigned int quint32
Definition: qglobal.h:938
if(void) toggleToolbarShown
FloatingPointPrecision floatingPointPrecision() const
Returns the floating point precision of the data stream.
qint64 write(const char *data, qint64 len)
Writes at most maxSize bytes of data from data to the device.
Definition: qiodevice.cpp:1342

◆ operator<<() [11/12]

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

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Returns a reference to the stream.

See also
setFloatingPointPrecision()

Definition at line 1248 of file qdatastream.cpp.

1249 {
1250  if (version() >= QDataStream::Qt_4_6
1252  *this << float(f);
1253  return *this;
1254  }
1255 
1257 #ifndef Q_DOUBLE_FORMAT
1258  if (noswap) {
1259  if (dev->write((char *)&f, sizeof(double)) != sizeof(double))
1261  } else {
1262  union {
1263  double val1;
1264  quint64 val2;
1265  } x;
1266  x.val1 = f;
1267  x.val2 = qbswap(x.val2);
1268  if (dev->write((char *)&x.val2, sizeof(double)) != sizeof(double))
1270  }
1271 #else
1272  union {
1273  double val1;
1274  char val2[8];
1275  } x;
1276  x.val1 = f;
1277  char *p = x.val2;
1278  char b[8];
1279  if (noswap) {
1280  b[Q_DF(0)] = *p++;
1281  b[Q_DF(1)] = *p++;
1282  b[Q_DF(2)] = *p++;
1283  b[Q_DF(3)] = *p++;
1284  b[Q_DF(4)] = *p++;
1285  b[Q_DF(5)] = *p++;
1286  b[Q_DF(6)] = *p++;
1287  b[Q_DF(7)] = *p;
1288  } else {
1289  b[Q_DF(7)] = *p++;
1290  b[Q_DF(6)] = *p++;
1291  b[Q_DF(5)] = *p++;
1292  b[Q_DF(4)] = *p++;
1293  b[Q_DF(3)] = *p++;
1294  b[Q_DF(2)] = *p++;
1295  b[Q_DF(1)] = *p++;
1296  b[Q_DF(0)] = *p;
1297  }
1298  if (dev->write(b, 8) != 8)
1300 #endif
1301  return *this;
1302 }
void qbswap(const T src, uchar *dest)
Definition: qendian.h:74
QIODevice * dev
Definition: qdatastream.h:193
#define CHECK_STREAM_WRITE_PRECOND(retVal)
Status q_status
Definition: qdatastream.h:198
unsigned __int64 quint64
Definition: qglobal.h:943
int version() const
Returns the version number of the data serialization format.
Definition: qdatastream.h:212
FloatingPointPrecision floatingPointPrecision() const
Returns the floating point precision of the data stream.
qint64 write(const char *data, qint64 len)
Writes at most maxSize bytes of data from data to the device.
Definition: qiodevice.cpp:1342

◆ operator<<() [12/12]

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

Writes the '\0'-terminated string s to the stream and returns a reference to the stream.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

The string is serialized using writeBytes().

Definition at line 1317 of file qdatastream.cpp.

1318 {
1319  if (!s) {
1320  *this << (quint32)0;
1321  return *this;
1322  }
1323  uint len = qstrlen(s) + 1; // also write null terminator
1324  *this << (quint32)len; // write length specifier
1325  writeRawData(s, len);
1326  return *this;
1327 }
unsigned int uint
Definition: qglobal.h:996
uint qstrlen(const char *str)
Definition: qbytearray.h:79
unsigned int quint32
Definition: qglobal.h:938
int writeRawData(const char *, int len)
Writes len bytes from s to the stream.

◆ operator>>() [1/12]

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

Reads a signed byte from the stream into i, and returns a reference to the stream.

Definition at line 698 of file qdatastream.cpp.

699 {
700  i = 0;
701  CHECK_STREAM_PRECOND(*this)
702  char c;
703  if (!dev->getChar(&c))
705  else
706  i = qint8(c);
707  return *this;
708 }
unsigned char c[8]
Definition: qnumeric_p.h:62
QIODevice * dev
Definition: qdatastream.h:193
void setStatus(Status status)
Sets the status of the data stream to the status given.
#define CHECK_STREAM_PRECOND(retVal)
signed char qint8
Definition: qglobal.h:933
static unsigned int getChar(const QChar *str, int &i, const int len)
if(void) toggleToolbarShown
return(isPopup||isToolTip)

◆ operator>>() [2/12]

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

Reads an unsigned byte from the stream into i, and returns a reference to the stream.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 218 of file qdatastream.h.

219 { return *this >> reinterpret_cast<qint8&>(i); }
signed char qint8
Definition: qglobal.h:933

◆ operator>>() [3/12]

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

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 732 of file qdatastream.cpp.

733 {
734  i = 0;
735  CHECK_STREAM_PRECOND(*this)
736  if (dev->read((char *)&i, 2) != 2) {
737  i = 0;
739  } else {
740  if (!noswap) {
741  i = qbswap(i);
742  }
743  }
744  return *this;
745 }
void qbswap(const T src, uchar *dest)
Definition: qendian.h:74
QIODevice * dev
Definition: qdatastream.h:193
void setStatus(Status status)
Sets the status of the data stream to the status given.
#define CHECK_STREAM_PRECOND(retVal)
if(void) toggleToolbarShown

◆ operator>>() [4/12]

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

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 221 of file qdatastream.h.

222 { return *this >> reinterpret_cast<qint16&>(i); }
short qint16
Definition: qglobal.h:935

◆ operator>>() [5/12]

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

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 769 of file qdatastream.cpp.

770 {
771  i = 0;
772  CHECK_STREAM_PRECOND(*this)
773  if (dev->read((char *)&i, 4) != 4) {
774  i = 0;
776  } else {
777  if (!noswap) {
778  i = qbswap(i);
779  }
780  }
781  return *this;
782 }
void qbswap(const T src, uchar *dest)
Definition: qendian.h:74
QIODevice * dev
Definition: qdatastream.h:193
void setStatus(Status status)
Sets the status of the data stream to the status given.
#define CHECK_STREAM_PRECOND(retVal)
if(void) toggleToolbarShown

◆ operator>>() [6/12]

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

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 224 of file qdatastream.h.

225 { return *this >> reinterpret_cast<qint32&>(i); }
int qint32
Definition: qglobal.h:937

◆ operator>>() [7/12]

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

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 805 of file qdatastream.cpp.

806 {
807  i = qint64(0);
808  CHECK_STREAM_PRECOND(*this)
809  if (version() < 6) {
810  quint32 i1, i2;
811  *this >> i2 >> i1;
812  i = ((quint64)i1 << 32) + i2;
813  } else {
814  if (dev->read((char *)&i, 8) != 8) {
815  i = qint64(0);
817  } else {
818  if (!noswap) {
819  i = qbswap(i);
820  }
821  }
822  }
823  return *this;
824 }
void qbswap(const T src, uchar *dest)
Definition: qendian.h:74
QIODevice * dev
Definition: qdatastream.h:193
void setStatus(Status status)
Sets the status of the data stream to the status given.
qint64 read(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, and returns the number of bytes read...
Definition: qiodevice.cpp:791
#define CHECK_STREAM_PRECOND(retVal)
unsigned __int64 quint64
Definition: qglobal.h:943
__int64 qint64
Definition: qglobal.h:942
int version() const
Returns the version number of the data serialization format.
Definition: qdatastream.h:212
unsigned int quint32
Definition: qglobal.h:938
if(void) toggleToolbarShown

◆ operator>>() [8/12]

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

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 227 of file qdatastream.h.

228 { return *this >> reinterpret_cast<qint64&>(i); }
__int64 qint64
Definition: qglobal.h:942

◆ operator>>() [9/12]

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

Reads a boolean value from the stream into i.

Returns a reference to the stream.

Definition at line 830 of file qdatastream.cpp.

831 {
832  qint8 v;
833  *this >> v;
834  i = !!v;
835  return *this;
836 }
signed char qint8
Definition: qglobal.h:933

◆ operator>>() [10/12]

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

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Returns a reference to the stream.

See also
setFloatingPointPrecision()

Definition at line 851 of file qdatastream.cpp.

852 {
855  double d;
856  *this >> d;
857  f = d;
858  return *this;
859  }
860 
861  f = 0.0f;
862  CHECK_STREAM_PRECOND(*this)
863  if (dev->read((char *)&f, 4) != 4) {
864  f = 0.0f;
866  } else {
867  if (!noswap) {
868  union {
869  float val1;
870  quint32 val2;
871  } x;
872  x.val2 = qbswap(*reinterpret_cast<quint32 *>(&f));
873  f = x.val1;
874  }
875  }
876  return *this;
877 }
void qbswap(const T src, uchar *dest)
Definition: qendian.h:74
QIODevice * dev
Definition: qdatastream.h:193
void setStatus(Status status)
Sets the status of the data stream to the status given.
#define CHECK_STREAM_PRECOND(retVal)
int version() const
Returns the version number of the data serialization format.
Definition: qdatastream.h:212
unsigned int quint32
Definition: qglobal.h:938
if(void) toggleToolbarShown
FloatingPointPrecision floatingPointPrecision() const
Returns the floating point precision of the data stream.
QScopedPointer< QDataStreamPrivate > d
Definition: qdatastream.h:191

◆ operator>>() [11/12]

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

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Returns a reference to the stream.

See also
setFloatingPointPrecision()

Definition at line 896 of file qdatastream.cpp.

897 {
900  float d;
901  *this >> d;
902  f = d;
903  return *this;
904  }
905 
906  f = 0.0;
907  CHECK_STREAM_PRECOND(*this)
908 #ifndef Q_DOUBLE_FORMAT
909  if (dev->read((char *)&f, 8) != 8) {
910  f = 0.0;
912  } else {
913  if (!noswap) {
914  union {
915  double val1;
916  quint64 val2;
917  } x;
918  x.val2 = qbswap(*reinterpret_cast<quint64 *>(&f));
919  f = x.val1;
920  }
921  }
922 #else
923  //non-standard floating point format
924  union {
925  double val1;
926  char val2[8];
927  } x;
928  char *p = x.val2;
929  char b[8];
930  if (dev->read(b, 8) == 8) {
931  if (noswap) {
932  *p++ = b[Q_DF(0)];
933  *p++ = b[Q_DF(1)];
934  *p++ = b[Q_DF(2)];
935  *p++ = b[Q_DF(3)];
936  *p++ = b[Q_DF(4)];
937  *p++ = b[Q_DF(5)];
938  *p++ = b[Q_DF(6)];
939  *p = b[Q_DF(7)];
940  } else {
941  *p++ = b[Q_DF(7)];
942  *p++ = b[Q_DF(6)];
943  *p++ = b[Q_DF(5)];
944  *p++ = b[Q_DF(4)];
945  *p++ = b[Q_DF(3)];
946  *p++ = b[Q_DF(2)];
947  *p++ = b[Q_DF(1)];
948  *p = b[Q_DF(0)];
949  }
950  f = x.val1;
951  } else {
953  }
954 #endif
955  return *this;
956 }
void qbswap(const T src, uchar *dest)
Definition: qendian.h:74
QIODevice * dev
Definition: qdatastream.h:193
void setStatus(Status status)
Sets the status of the data stream to the status given.
qint64 read(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, and returns the number of bytes read...
Definition: qiodevice.cpp:791
#define CHECK_STREAM_PRECOND(retVal)
unsigned __int64 quint64
Definition: qglobal.h:943
int version() const
Returns the version number of the data serialization format.
Definition: qdatastream.h:212
FloatingPointPrecision floatingPointPrecision() const
Returns the floating point precision of the data stream.
QScopedPointer< QDataStreamPrivate > d
Definition: qdatastream.h:191

◆ operator>>() [12/12]

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

Reads the '\0'-terminated string s from the stream and returns a reference to the stream.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Space for the string is allocated using new – the caller must destroy it with delete[].

Definition at line 972 of file qdatastream.cpp.

973 {
974  uint len = 0;
975  return readBytes(s, len);
976 }
unsigned int uint
Definition: qglobal.h:996
QDataStream & readBytes(char *&, uint &len)
Reads the buffer s from the stream and returns a reference to the stream.

◆ readBytes()

QDataStream & QDataStream::readBytes ( char *&  s,
uint l 
)

Reads the buffer s from the stream and returns a reference to the stream.

The buffer s is allocated using new. Destroy it with the delete[] operator.

The l parameter is set to the length of the buffer. If the string read is empty, l is set to 0 and s is set to a null pointer.

The serialization format is a quint32 length specifier first, then l bytes of data.

See also
readRawData(), writeBytes()

Definition at line 996 of file qdatastream.cpp.

Referenced by operator>>().

997 {
998  s = 0;
999  l = 0;
1000  CHECK_STREAM_PRECOND(*this)
1001 
1002  quint32 len;
1003  *this >> len;
1004  if (len == 0)
1005  return *this;
1006 
1007  const quint32 Step = 1024 * 1024;
1008  quint32 allocated = 0;
1009  char *prevBuf = 0;
1010  char *curBuf = 0;
1011 
1012  do {
1013  int blockSize = qMin(Step, len - allocated);
1014  prevBuf = curBuf;
1015  curBuf = new char[allocated + blockSize + 1];
1016  if (prevBuf) {
1017  memcpy(curBuf, prevBuf, allocated);
1018  delete [] prevBuf;
1019  }
1020  if (dev->read(curBuf + allocated, blockSize) != blockSize) {
1021  delete [] curBuf;
1023  return *this;
1024  }
1025  allocated += blockSize;
1026  } while (allocated < len);
1027 
1028  s = curBuf;
1029  s[len] = '\0';
1030  l = (uint)len;
1031  return *this;
1032 }
const int blockSize
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
QIODevice * dev
Definition: qdatastream.h:193
void setStatus(Status status)
Sets the status of the data stream to the status given.
qint64 read(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, and returns the number of bytes read...
Definition: qiodevice.cpp:791
#define CHECK_STREAM_PRECOND(retVal)
unsigned int uint
Definition: qglobal.h:996
unsigned int quint32
Definition: qglobal.h:938
if(void) toggleToolbarShown
QFactoryLoader * l
return(isPopup||isToolTip)

◆ readRawData()

int QDataStream::readRawData ( char *  s,
int  len 
)

Reads at most len bytes from the stream into s and returns the number of bytes read.

If an error occurs, this function returns -1.

The buffer s must be preallocated. The data is not encoded.

See also
readBytes(), QIODevice::read(), writeRawData()

Definition at line 1043 of file qdatastream.cpp.

Referenced by QPicturePrivate::checkFormat(), QUuid::operator>>(), operator>>(), QByteArray::operator>>(), qExtractServerTime(), qNtlmDecodePhase2(), and QMainWindowLayoutState::restoreState().

1044 {
1046  return dev->read(s, len);
1047 }
QIODevice * dev
Definition: qdatastream.h:193
#define CHECK_STREAM_PRECOND(retVal)
return(isPopup||isToolTip)

◆ resetStatus()

void QDataStream::resetStatus ( )

Resets the status of the data stream.

See also
Status status() setStatus()

Definition at line 513 of file qdatastream.cpp.

Referenced by operator>>().

514 {
515  q_status = Ok;
516 }
Status q_status
Definition: qdatastream.h:198

◆ setByteOrder()

void QDataStream::setByteOrder ( ByteOrder  bo)

Sets the serialization byte order to bo.

The bo 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()

Definition at line 561 of file qdatastream.cpp.

Referenced by QWindowsMimeImage::convertFromMime(), QWindowsMimeImage::convertToMime(), QRegion::exec(), qEncodeNtlmv2Response(), qExtractServerTime(), qNtlmDecodePhase2(), qNtlmPhase1(), qNtlmPhase3(), QBmpHandler::read(), QBmpHandler::readHeader(), and QBmpHandler::write().

562 {
563  byteorder = bo;
564  if (QSysInfo::ByteOrder == QSysInfo::BigEndian)
565  noswap = (byteorder == BigEndian);
566  else
568 }
ByteOrder byteorder
Definition: qdatastream.h:196

◆ setDevice()

void QDataStream::setDevice ( QIODevice d)

void QDataStream::setDevice(QIODevice *d)

Sets the I/O device to d, which can be 0 to unset to current I/O device.

See also
device()

Definition at line 419 of file qdatastream.cpp.

Referenced by QPicturePrivate::checkFormat(), QPicture::play(), QPacket::QPacket(), and unsetDevice().

420 {
421  if (owndev) {
422  delete dev;
423  owndev = false;
424  }
425  dev = d;
426 }
QIODevice * dev
Definition: qdatastream.h:193
QScopedPointer< QDataStreamPrivate > d
Definition: qdatastream.h:191

◆ setFloatingPointPrecision()

void QDataStream::setFloatingPointPrecision ( QDataStream::FloatingPointPrecision  precision)

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

If the floating point precision is DoublePrecision and the version of the data stream is Qt_4_6 or higher, all floating point numbers will be written and read with 64-bit precision. If the floating point precision is SinglePrecision and the version is Qt_4_6 or higher, all floating point numbers will be written and read with 32-bit precision.

For versions prior to Qt_4_6, the precision of floating point numbers in the data stream depends on the stream operator called.

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.
Since
4.6

Definition at line 490 of file qdatastream.cpp.

Referenced by QTraceWindowSurface::~QTraceWindowSurface().

491 {
492  if (d == 0)
493  d.reset(new QDataStreamPrivate());
494  d->floatingPointPrecision = precision;
495 }
QDataStream::FloatingPointPrecision floatingPointPrecision
Definition: qdatastream_p.h:66
void reset(T *other=0)
Deletes the existing object it is pointing to if any, and sets its pointer to other.
QScopedPointer< QDataStreamPrivate > d
Definition: qdatastream.h:191

◆ setStatus()

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()

Definition at line 526 of file qdatastream.cpp.

Referenced by QMetaObjectBuilder::deserialize(), QVariant::load(), QUuid::operator<<(), operator>>(), operator>>(), QUuid::operator>>(), QHostAddress::operator>>(), QByteArray::operator>>(), readBytes(), and QDockAreaLayout::restoreState().

527 {
528  if (q_status == Ok)
529  q_status = status;
530 }
Status status() const
Returns the status of the data stream.
Status q_status
Definition: qdatastream.h:198

◆ setVersion()

void QDataStream::setVersion ( int  v)
inline

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

You don't have to set a version if you are using the current version of Qt, but for your own custom binary formats we recommend that you do; see Versioning in the Detailed Description.

To accommodate new functionality, the datastream serialization format of some Qt classes has changed in some versions of Qt. If you want to read data that was created by an earlier version of Qt, or write data that can be read by a program that was compiled with an earlier version of Qt, use this function to modify the serialization format used by QDataStream.

Qt Version QDataStream Version
Qt 4.6 12
Qt 4.5 11
Qt 4.4 10
Qt 4.3 9
Qt 4.2 8
Qt 4.0, 4.1 7
Qt 3.3 6
Qt 3.1, 3.2 5
Qt 3.0 4
Qt 2.1, 2.2, 2.3 3
Qt 2.0 2
Qt 1.x 1

The Version enum provides symbolic constants for the different versions of Qt. For example:

QDataStream out(file);
out.setVersion(QDataStream::Qt_4_0);
See also
version(), Version

Definition at line 215 of file qdatastream.h.

Referenced by QPicturePrivate::checkFormat(), QRegion::exec(), QAxServerBase::Load(), QPicture::play(), QPacket::QPacket(), QWidget::restoreGeometry(), QWidget::saveGeometry(), and QSettingsPrivate::variantToString().

216 { ver = v; }

◆ skipRawData()

int QDataStream::skipRawData ( int  len)

Skips len bytes from the device.

Since
4.1

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()

Definition at line 1381 of file qdatastream.cpp.

Referenced by qExtractServerTime().

1382 {
1384 
1385  if (dev->isSequential()) {
1386  char buf[4096];
1387  int sumRead = 0;
1388 
1389  while (len > 0) {
1390  int blockSize = qMin(len, (int)sizeof(buf));
1391  int n = dev->read(buf, blockSize);
1392  if (n == -1)
1393  return -1;
1394  if (n == 0)
1395  return sumRead;
1396 
1397  sumRead += n;
1398  len -= blockSize;
1399  }
1400  return sumRead;
1401  } else {
1402  qint64 pos = dev->pos();
1403  qint64 size = dev->size();
1404  if (pos + len > size)
1405  len = size - pos;
1406  if (!dev->seek(pos + len))
1407  return -1;
1408  return len;
1409  }
1410 }
const int blockSize
virtual qint64 size() const
For open random-access devices, this function returns the size of the device.
Definition: qiodevice.cpp:642
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
virtual qint64 pos() const
For random-access devices, this function returns the position that data is written to or read from...
Definition: qiodevice.cpp:624
QIODevice * dev
Definition: qdatastream.h:193
qint64 read(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, and returns the number of bytes read...
Definition: qiodevice.cpp:791
#define CHECK_STREAM_PRECOND(retVal)
__int64 qint64
Definition: qglobal.h:942
if(void) toggleToolbarShown
virtual bool seek(qint64 pos)
For random-access devices, this function sets the current position to pos, returning true on success...
Definition: qiodevice.cpp:659

◆ status()

QDataStream::Status QDataStream::status ( ) const

◆ unsetDevice()

void QDataStream::unsetDevice ( )

Unsets the I/O device.

Use setDevice(0) instead.

Definition at line 437 of file qdatastream.cpp.

438 {
439  setDevice(0);
440 }
void setDevice(QIODevice *)
void QDataStream::setDevice(QIODevice *d)

◆ version()

int QDataStream::version ( ) const
inline

◆ writeBytes()

QDataStream & QDataStream::writeBytes ( const char *  s,
uint  len 
)

Writes the length specifier len and the buffer s to the stream and returns a reference to the stream.

The len is serialized as a quint32, followed by len bytes from s. Note that the data is not encoded.

See also
writeRawData(), readBytes()

Definition at line 1340 of file qdatastream.cpp.

Referenced by operator<<(), QByteArray::operator<<(), and QTraceWindowSurface::~QTraceWindowSurface().

1341 {
1343  *this << (quint32)len; // write length specifier
1344  if (len)
1345  writeRawData(s, len);
1346  return *this;
1347 }
#define CHECK_STREAM_WRITE_PRECOND(retVal)
unsigned int quint32
Definition: qglobal.h:938
int writeRawData(const char *, int len)
Writes len bytes from s to the stream.

◆ writeRawData()

int QDataStream::writeRawData ( const char *  s,
int  len 
)

Writes len bytes from s 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()

Definition at line 1358 of file qdatastream.cpp.

Referenced by operator<<(), operator<<(), QUuid::operator<<(), qEncodeNtlmv2Response(), qStreamNtlmBuffer(), QTcpServerConnection::send(), QmlOstPlugin::send(), QPdfEnginePrivate::write(), writeBytes(), QPdfEnginePrivate::writeCompressed(), and QPdfEnginePrivate::xprintf().

1359 {
1361  int ret = dev->write(s, len);
1362  if (ret != len)
1364  return ret;
1365 }
QIODevice * dev
Definition: qdatastream.h:193
#define CHECK_STREAM_WRITE_PRECOND(retVal)
Status q_status
Definition: qdatastream.h:198
if(void) toggleToolbarShown
return(isPopup||isToolTip)

Properties

◆ byteorder

ByteOrder QDataStream::byteorder
private

Definition at line 196 of file qdatastream.h.

Referenced by QDataStream(), and setByteOrder().

◆ d

QScopedPointer<QDataStreamPrivate> QDataStream::d
private

◆ dev

QIODevice* QDataStream::dev
private

◆ noswap

bool QDataStream::noswap
private

Definition at line 195 of file qdatastream.h.

Referenced by operator<<(), operator>>(), QDataStream(), and setByteOrder().

◆ owndev

bool QDataStream::owndev
private

Definition at line 194 of file qdatastream.h.

Referenced by QDataStream(), setDevice(), and ~QDataStream().

◆ q_status

Status QDataStream::q_status
private

Definition at line 198 of file qdatastream.h.

Referenced by operator<<(), QDataStream(), resetStatus(), setStatus(), status(), and writeRawData().

◆ ver

int QDataStream::ver
private

Definition at line 197 of file qdatastream.h.

Referenced by QDataStream().


The documentation for this class was generated from the following files: