Logo Search packages:      
Sourcecode: qt4-x11 version File versions  Download package

QDataStream Class Reference

List of all members.


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}{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.

Definition at line 71 of file qdatastream.h.


Public Types

enum  ByteOrder { BigEndian = QSysInfo::BigEndian, LittleEndian = QSysInfo::LittleEndian }
enum  Status { Ok, ReadPastEnd, ReadCorruptData }
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
}

Public Member Functions

bool atEnd () const
ByteOrder byteOrder () const
QIODevicedevice () const
QDataStreamoperator<< (const char *str)
QDataStreamoperator<< (double f)
QDataStreamoperator<< (float f)
QDataStreamoperator<< (bool i)
QDataStreamoperator<< (quint64 i)
QDataStreamoperator<< (qint64 i)
QDataStreamoperator<< (quint32 i)
QDataStreamoperator<< (qint32 i)
QDataStreamoperator<< (quint16 i)
QDataStreamoperator<< (qint16 i)
QDataStreamoperator<< (quint8 i)
QDataStreamoperator<< (qint8 i)
QDataStreamoperator>> (char *&str)
QDataStreamoperator>> (double &f)
QDataStreamoperator>> (float &f)
QDataStreamoperator>> (bool &i)
QDataStreamoperator>> (quint64 &i)
QDataStreamoperator>> (qint64 &i)
QDataStreamoperator>> (quint32 &i)
QDataStreamoperator>> (qint32 &i)
QDataStreamoperator>> (quint16 &i)
QDataStreamoperator>> (qint16 &i)
QDataStreamoperator>> (quint8 &i)
QDataStreamoperator>> (qint8 &i)
 QDataStream (const QByteArray &)
 QDataStream (QByteArray *, QIODevice::OpenMode flags)
 QDataStream (QIODevice *)
 QDataStream ()
QDataStreamreadBytes (char *&, uint &len)
int readRawData (char *, int len)
void resetStatus ()
void setByteOrder (ByteOrder)
void setDevice (QIODevice *)
void setStatus (Status status)
void setVersion (int)
int skipRawData (int len)
Status status () const
void unsetDevice ()
int version () const
QDataStreamwriteBytes (const char *, uint len)
int writeRawData (const char *, int len)
virtual ~QDataStream ()

Private Attributes

ByteOrder byteorder
QDataStreamPrivate * d
QIODevicedev
bool noswap
bool owndev
Status q_status
int ver

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

Generated by  Doxygen 1.6.0   Back to index