PDA

View Full Version : Serializing a Qobject



erfan
12th December 2012, 07:34
Hi,
i want to serialize my costumes classes ,
but QDataStream doesn't have any method for that
and it's operation "<<" doesn't have any QObject or anything else.
how can i do this?
please help me!!

droneone
12th December 2012, 18:00
You must create your own streaming operators for the class - otherwise there's no ability for another body of code to automatically determine how to construct/de-construct your class.
E.g.:



Q_DECLARE_METATYPE(MyClass)

inline QDataStream& operator<<(QdataStream& out, const MyClass& in) {

out << quint32(in.foo());
out << quint32(in.bar());

return out;
}

inline QDataStream& operator>>(QdataStream& in, const MyClass& out) {

quint32 foo;
quint32 bar;
in >> foo;
in >> bar;

out.setFoo(foo);
out.setBar(bar);

return in;
}


See also: http://stackoverflow.com/questions/2570679/serialization-with-qt