×
Namespaces

Variants
Actions

Qt Object Serialization

From Nokia Developer Wiki
Jump to: navigation, search

This article shows how to serialize Qt data types.

Article Metadata
Tested with
Devices(s): Emulator / desktop / device
Compatibility
Platform(s): Qt 4.7 and later
Symbian
Article
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);
file.open(QIODevice::WriteOnly);
QDataStream out(&file); // write the data
out << dict;
file.close();
 
//setting new a value
dict["book.owner"] = "Sunil Kumar";
 
//update the dictionary
file.open(QIODevice::ReadOnly);
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 04:18.
144 page views in the last 30 days.