Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.
Qt Object Serialization
This article shows how to serialize Qt data types.
Qt provides two classes for serialization,
- QDataStream class to serialize binary data and
- QTextStream to parse input stream (reading and writing text)
usually the application requirement is that we need serialization of binary data for example, create a dictionary for a configuration file.
In the following example we will play around with QHash
#define PATH "c:/data/file.dat"
dict["book.owner"] = "skumar";
dict["book.version"] = "0.1.0";
QDataStream out(&file); // write the data
out << dict;
//setting new a value
dict["book.owner"] = "Sunil Kumar";
//update the dictionary
QDataStream in(&file); // read the data serialized from the file
in >> dict;
qDebug() << "value: " << dict.value("book.owner");
Other list of data types which can be serialized Serializing Qt Data Types . But if we want Serialization of our own QObject derived classes then we need to do it our self but provided two function one for serialize and another for de-serialize.