QDataStream(3qt)

πŸ“¦ version 3.3.8b, in fedora 43, fedora 44

πŸ’– NAME πŸ’–

QDataStream β€” serialization of binary data to a QIODevice. Yep bestie, this is the QDataStream(3qt) manual page and we are gonna get so comfy with it. πŸ¦‹


✨ SYNOPSIS ✨

Ok so real talk first, the important lil disclaimer: all the functions in this class are reentrant when Qt is built with thread support. πŸ‘‘

And the include, do not forget the include:

#include <qdatastream.h>

πŸ‘‘ Public Members πŸ‘‘

QDataStream ()
QDataStream ( QIODevice * d )
QDataStream ( QByteArray a, int mode )
virtual ~QDataStream ()
QIODevice * device () const
void setDevice ( QIODevice * d )
void unsetDevice ()
bool atEnd () const
bool eof () const  (obsolete)
enum ByteOrder { BigEndian, LittleEndian }
int byteOrder () const
void setByteOrder ( int bo )
bool isPrintableData () const
void setPrintableData ( bool enable )
int version () const
void setVersion ( int v )
QDataStream & operator>> ( Q_INT8 & i )
QDataStream & operator>> ( Q_UINT8 & i )
QDataStream & operator>> ( Q_INT16 & i )
QDataStream & operator>> ( Q_UINT16 & i )
QDataStream & operator>> ( Q_INT32 & i )
QDataStream & operator>> ( Q_UINT32 & i )
QDataStream & operator>> ( Q_INT64 & i )
QDataStream & operator>> ( Q_UINT64 & i )
QDataStream & operator>> ( Q_LONG & i )
QDataStream & operator>> ( Q_ULONG & i )
QDataStream & operator>> ( float & f )
QDataStream & operator>> ( double & f )
QDataStream & operator>> ( char *& s )
QDataStream & operator<< ( Q_INT8 i )
QDataStream & operator<< ( Q_UINT8 i )
QDataStream & operator<< ( Q_INT16 i )
QDataStream & operator<< ( Q_UINT16 i )
QDataStream & operator<< ( Q_INT32 i )
QDataStream & operator<< ( Q_UINT32 i )
QDataStream & operator<< ( Q_INT64 i )
QDataStream & operator<< ( Q_UINT64 i )
QDataStream & operator<< ( Q_LONG i )
QDataStream & operator<< ( Q_ULONG i )
QDataStream & operator<< ( float f )
QDataStream & operator<< ( double f )
QDataStream & operator<< ( const char * s )
QDataStream & readBytes ( char *& s, uint & l )
QDataStream & readRawBytes ( char * s, uint len )
QDataStream & writeBytes ( const char * s, uint len )
QDataStream & writeRawBytes ( const char * s, uint len )

🌸 DESCRIPTION 🌸

So the QDataStream class provides serialization of binary data to a QIODevice. That's the whole vibe. πŸ’…

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. Fr fr. Like, a data stream that is written by a PC under Windows can be read by a Sun SPARC running Solaris. Portability ate and left no crumbs. 🌍✨

You can also use a data stream to read/write raw unencoded binary data. But if you want a "parsing" input stream, go look at QTextStream instead, that's a different girl. πŸ“–

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. It's giving "decompose the problem" energy. 🧩

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 IO device. πŸ‘―

Example (write binary data to a stream):

QFile file( "file.dat" );
file.open( IO_WriteOnly );
QDataStream stream( &file ); // we will serialize the data into the file
stream << "the answer is";   // serialize a string
stream << (Q_INT32)42;       // serialize an integer

Example (read binary data from a stream):

QFile file( "file.dat" );
file.open( IO_ReadOnly );
QDataStream stream( &file );  // read the data serialized from the file
QString str;
Q_INT32 a;
stream >> 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. The complete list of all Qt types supporting data streaming lives in the Format of the QDataStream operators. Go peek, iykyk. πŸ‘€

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. No surprises, only slay. πŸŽ€

To take one example: a char* string is written as a 32-bit integer equal to the length of the string including the NUL byte ('\0'), followed by all the characters of the string including the NUL 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 NUL are read. Bestie that's literally just length-prefix + payload, iconic. πŸ’«

The initial IODevice 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 IODevice set) atEnd() will return TRUE. πŸ›‘

If you want the data to be compatible with an earlier version of Qt, use setVersion(). πŸ•°οΈ

If you want the data to be human-readable, e.g. for debugging, you can set the data stream into printable data mode with setPrintableData(). The data is then written slower, in a bloated but human readable format. Tradeoffs, bestie, tradeoffs. πŸ™ˆ

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. Main character planning energy. πŸ‘‘

QFile file( "file.xxx" );
file.open( IO_WriteOnly );
QDataStream stream( &file );
// Write a header with a "magic number" and a version
stream << (Q_UINT32)0xA0B0C0D0;
stream << (Q_INT32)123;
// Write the data
stream << [lots of interesting data]

Then read it in with:

QFile file( "file.xxx" );
file.open( IO_ReadOnly );
QDataStream stream( &file );
// Read and check the header
Q_UINT32 magic;
stream >> magic;
if ( magic != 0xA0B0C0D0 )
    return XXX_BAD_FILE_FORMAT;
// Read the version
Q_INT32 version;
stream >> version;
if ( version < 100 )
    return XXX_BAD_FILE_TOO_OLD;
if ( version > 123 )
    return XXX_BAD_FILE_TOO_NEW;
if ( version <= 110 )
    stream.setVersion(1);
// Read the data
stream >> [lots of interesting data];
if ( version > 120 )
    stream >> [data new in XXX version 1.2];
stream >> [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. Don't be chaotic, okay? 🫢


🧚 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 readRawBytes(). Similarly data can be written to the stream using writeRawBytes(). Notice that any encoding/decoding of the data must be done by you. You're on your own for that part, bestie, the class is not doing it for you. πŸ’€

A similar pair of functions is readBytes() and writeBytes(). These differ from their raw counterparts as follows: readBytes() reads a Q_UINT32 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 Q_UINT32 containing the length of the data, followed by the data. Notice that any encoding/decoding of the data (apart from the length Q_UINT32) must be done by you. 🫑

See also QTextStream, QVariant, and Input/Output and Networking. 🌈


🌟 Member Type Documentation 🌟

QDataStream::ByteOrder

The byte order used for reading/writing the data.

  • QDataStream::BigEndian β€” the default πŸ‘‘
  • QDataStream::LittleEndian

πŸ’œ MEMBER FUNCTION DOCUMENTATION πŸ’œ

QDataStream::QDataStream ()

Constructs a data stream that has no IO device. Just a lil stream, vibing, attached to nothing. πŸ“

See also setDevice().

QDataStream::QDataStream ( QIODevice * d )

Constructs a data stream that uses the IO device d.

⚠️ Warning: If you use QSocket or QSocketDevice as the IO 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. Not me getting caught lacking by a short read 😭

See also setDevice() and device().

QDataStream::QDataStream ( QByteArray a, int mode )

Constructs a data stream that operates on a byte array, a, through an internal QBuffer device. The mode is a QIODevice::mode(), usually either IO_ReadOnly or IO_WriteOnly.

Example:

static char bindata[] = { 231, 1, 44, ... };
QByteArray a;
a.setRawData( bindata, sizeof(bindata) );   // a points to bindata
QDataStream stream( a, IO_ReadOnly );       // open on a's data
stream >> [something];                      // read raw bindata
a.resetRawData( bindata, sizeof(bindata) ); // finished
🚨 The QByteArray::setRawData() function is not for the inexperienced. Respect the raw data, bestie. πŸ’…

QDataStream::~QDataStream () [virtual]

Destroys the data stream. ✨

The destructor will not affect the current IO device, unless it is an internal IO device processing a QByteArray passed in the constructor, in which case the internal IO device is destroyed. So like, it only takes the internal one down with it. πŸ’€

bool QDataStream::atEnd () const

Returns TRUE if the IO device has reached the end position (end of the stream or file) or if there is no IO device set; otherwise returns FALSE, i.e. if the current position of the IO device is before the end position. πŸ›‘

See also QIODevice::atEnd().

int QDataStream::byteOrder () const

Returns the current byte order setting β€” either BigEndian or LittleEndian. πŸ¦‹

See also setByteOrder().

QIODevice * QDataStream::device () const

Returns the IO device currently set. πŸŽ€

See also setDevice() and unsetDevice().

bool QDataStream::eof () const

πŸ’€ This function is obsolete. It is provided to keep old source working. We strongly advise against using it in new code. Put it down, bestie, it's not worth it.

Returns TRUE if the IO device has reached the end position (end of stream or file) or if there is no IO device set.

Returns FALSE if the current position of the read/write head of the IO device is somewhere before the end position.

See also QIODevice::atEnd().

bool QDataStream::isPrintableData () const

Returns TRUE if the printable data flag has been set; otherwise returns FALSE. πŸ–¨οΈβœ¨

See also setPrintableData().

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

Writes a signed byte, i, to the stream and returns a reference to the stream. πŸ’…

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

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

okay so this is an overloaded member function, provided for convenience β€” it behaves essentially like the one right above it πŸ’«

It writes a signed 16-bit integer, i, to the stream and hands you back a reference to the stream. cute, quick, done.

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

another overloaded member function, provided for convenience, behaving essentially like the one above it ✨

Writes an unsigned 16-bit integer, i, to the stream. Returns a reference to the stream. no thoughts, just bytes πŸŽ€

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

yep, overloaded member function, provided for convenience, behaves essentially like the above function πŸ’…

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

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

overloaded member function, provided for convenience β€” behaves essentially like the above function 🫢

This one's got its own lil wording: it writes an unsigned integer, i, to the stream as a 32-bit unsigned integer (Q_UINT32). Returns a reference to the stream.

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

overloaded member function, provided for convenience, behaves essentially like the above function 🌸

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

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

overloaded member function, provided for convenience, behaves essentially like the above function ⭐

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

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

overloaded member function, provided for convenience, behaves essentially like the above function πŸ’œ

Writes a signed integer i, of the system's word length, to the stream and returns a reference to the stream. (system's word length, bestie β€” it's giving "it depends on your machine" 😭)

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

overloaded member function, provided for convenience, behaves essentially like the above function 🌈

Writes an unsigned integer i, of the system's word length, to the stream and returns a reference to the stream.

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

overloaded member function, provided for convenience, behaves essentially like the above function πŸ¦‹

Writes a 32-bit floating point number, f, to the stream using the standard IEEE754 format. Returns a reference to the stream. IEEE754 is the main character here, no cap.

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

overloaded member function, provided for convenience, behaves essentially like the above function πŸ’–

Writes a 64-bit floating point number, f, to the stream using the standard IEEE754 format. Returns a reference to the stream.

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

overloaded member function, provided for convenience, behaves essentially like the above function πŸŽ€

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

The string is serialized using writeBytes(). that's the whole tea πŸ“


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

Reads a signed byte from the stream into i, and returns a reference to the stream. (no "overloaded" disclaimer on this one, it's the original bestie πŸ‘‘)

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

overloaded member function, provided for convenience, behaves essentially like the above function ✨

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

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

overloaded member function, provided for convenience, behaves essentially like the above function πŸ’«

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

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

overloaded member function, provided for convenience, behaves essentially like the above function πŸ’…

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

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

overloaded member function, provided for convenience, behaves essentially like the above function 🫢

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

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

overloaded member function, provided for convenience, behaves essentially like the above function 🌸

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

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

overloaded member function, provided for convenience, behaves essentially like the above function ⭐

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

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

overloaded member function, provided for convenience, behaves essentially like the above function πŸ’œ

Reads an unsigned 64-bit integer from the stream, into i, and returns a reference to the stream. (yes the commas moved, I'm not fixing it, that's literally what it says 😭)

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

overloaded member function, provided for convenience, behaves essentially like the above function 🌈

Reads a signed integer of the system's word length from the stream into i, and returns a reference to the stream.

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

overloaded member function, provided for convenience, behaves essentially like the above function πŸ¦‹

Reads an unsigned integer of the system's word length from the stream, into i, and returns a reference to the stream.

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

overloaded member function, provided for convenience, behaves essentially like the above function πŸ’–

Reads a 32-bit floating point number from the stream into f, using the standard IEEE754 format. Returns a reference to the stream.

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

overloaded member function, provided for convenience, behaves essentially like the above function πŸŽ€

Reads a 64-bit floating point number from the stream into f, using the standard IEEE754 format. Returns a reference to the stream.

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

overloaded member function, provided for convenience, behaves essentially like the above function πŸ“

Reads the '\0'-terminated string s 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[]. bestie please. do not leak. the memory is YOUR responsibility now, it's giving main character energy but the tragic kind πŸ’€

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. If the length is zero or s cannot be allocated, s is set to 0.

The l parameter will be set to the length of the buffer.

The serialization format is a Q_UINT32 length specifier first, then l bytes of data. Note that the data is not encoded.

See also readRawBytes() and writeBytes().

QDataStream & QDataStream::readRawBytes ( char * s, uint len )

Reads len bytes from the stream into s and returns a reference to the stream.

The buffer s must be preallocated. (you bring the memory, it just fills it 🫑) The data is not encoded.

See also readBytes(), QIODevice::readBlock(), and writeRawBytes().

void QDataStream::setByteOrder ( int 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().

void QDataStream::setDevice ( QIODevice * d )

void QDataStream::setDevice(QIODevice *d )

Sets the IO device to d.

See also device() and unsetDevice().

void QDataStream::setPrintableData ( bool enable )

If enable is TRUE, data will be output in a human readable format. If enable is FALSE, data will be output in a binary format.

If enable is TRUE, the write functions will generate output that consists of printable characters (7 bit ASCII). This output will typically be a lot larger than the default binary output, and consequently slower to write.

We recommend only enabling printable data for debugging purposes. periodt.

void QDataStream::setVersion ( int v )

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

You don't need to set a version if you are using the current version of Qt.

In order 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 of QDataStream.

There's also a lil formatting artifact hanging out in here, verbatim, and I'm keeping it because rules are rules πŸ™ˆ:

<center>.nf
</center>

See also version().

void QDataStream::unsetDevice ()

Unsets the IO device. This is the same as calling setDevice( 0 ).

See also device() and setDevice().

int QDataStream::version () const

Returns the version number of the data serialization format. In Qt 3.1, this number is 5.

See also setVersion().

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 Q_UINT32, followed by len bytes from s. Note that the data is not encoded.

See also writeRawBytes() and readBytes().

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

Writes len bytes from s to the stream and returns a reference to the stream. The data is not encoded.

See also writeBytes(), QIODevice::writeBlock(), and readRawBytes().


🌟 SEE ALSO 🌟


Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the license file included in the distribution for a complete license statement.


✍️ AUTHOR ✍️

Generated automatically from the source code. (no human author to thank, it just appeared ✨)


πŸ› BUGS πŸ›

If you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. Good bug reports help us to help you. Thank you. (genuinely, thank you, they mean it 🫢)

The definitive Qt documentation is provided in HTML format; it is located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. This man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech.

If you find errors in this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qdatastream.3qt) and the Qt version (3.3.8).


okay bestie that's the whole page πŸ’… this was Trolltech AS, dated 2 February 2007, and the page itself is QDataStream(3qt) β€” version 3.3.8, if you're keeping receipts. go forth and serialize responsibly πŸŽ€βœ¨

πŸ“¦ where this page comes from