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

From Wiki
Jump to: navigation, search

This article shows how to serialize Qt data types.

Article Metadata
Tested with
Devices(s): Emulator / desktop / device
Platform(s): Qt 4.7 and later
Keywords: Qt Application
Created: skumar_rao (25 Nov 2010)
Last edited: hamishwillee (11 Oct 2012)

Qt provides two classes for serialization,

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"
int main()
QHash<QString,QString> dict;
dict["book.owner"] = "skumar";
dict["book.version"] = "0.1.0";
QFile file(PATH);;
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");
return 0;

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.

This page was last modified on 11 October 2012, at 01:18.
101 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.