Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
src_corelib_io_qdatastream.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
5{
6
8QFile file("file.dat");
10QDataStream out(&file); // we will serialize the data into the file
11out << QString("the answer is"); // serialize a string
12out << (qint32)42; // serialize an integer
14
15
17QFile file("file.dat");
19QDataStream in(&file); // read the data serialized from the file
21qint32 a;
22in >> str >> a; // extract "the answer is" and 42
24
25
27stream.setVersion(QDataStream::Qt_4_0);
29
30
32QFile file("file.xxx");
35
36// Write a header with a "magic number" and a version
37out << (quint32)0xA0B0C0D0;
38out << (qint32)123;
39
40out.setVersion(QDataStream::Qt_4_0);
41
42// Write the data
43out << lots_of_interesting_data;
45
46
48QFile file("file.xxx");
51
52// Read and check the header
54in >> magic;
55if (magic != 0xA0B0C0D0)
56 return XXX_BAD_FILE_FORMAT;
57
58// Read the version
59qint32 version;
60in >> version;
61if (version < 100)
62 return XXX_BAD_FILE_TOO_OLD;
63if (version > 123)
64 return XXX_BAD_FILE_TOO_NEW;
65
66if (version <= 110)
67 in.setVersion(QDataStream::Qt_3_2);
68else
69 in.setVersion(QDataStream::Qt_4_0);
70
71// Read the data
72in >> lots_of_interesting_data;
73if (version >= 120)
74 in >> data_new_in_XXX_version_1_2;
75in >> other_interesting_data;
77
78
81out.setVersion(QDataStream::Qt_4_0);
83
85in.startTransaction();
87qint32 a;
88in >> str >> a; // try to read packet atomically
89
90if (!in.commitTransaction())
91 return; // wait for more data
93
94}
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
Definition qfile.h:93
QFILE_MAYBE_NODISCARD bool open(OpenMode flags) override
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition qfile.cpp:904
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString str
[2]
EGLStreamKHR stream
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint in
static const uchar magic[MagicLength]
unsigned int quint32
Definition qtypes.h:50
int qint32
Definition qtypes.h:49
void wrapInFunction()
QFile file
[0]
QTextStream out(stdout)
[7]