serialization
-
hello everyone, I have no experience in serialization. I have a problem like:
This is my
struct plane
{
int id;
int size_az;
int size_dal;int amplityda; double speed; QList <int> k_x, k_y; QList <int> k_az, k_dal;
};
and
QList <plane> all_plane;I need to serializ and deserializ all_plane, use the file.
Please help me.
I need an example step by step.[[merged all serialization threads, Tobias]]
-
Hi, hope this can help you ...
@#include <QtCore>
#include <QApplication>
#include <QHash>
#include <QFile>
#include <QDataStream>#define PATH "./file.dat"
int main()
{//create a dictionary QHash<QString,QString> dict; dict["project.owner"] = “owner”; dict["project.version"] = “0.10.0″; QFile file(PATH); file.open(QIODevice::WriteOnly); QDataStream out(&file); // write the data out << dict; file.close(); //setting new a value dict["project.owner"] = “new”; //update the dictionary file.open(QIODevice::ReadOnly); QDataStream in(&file); // read the data serialized from the file in >> dict; qDebug() << “value: ” << dict.value(”project.owner”); return 0;
}@
-
Hi,
Please use coding tags with your code, without it's difficult to read it.
You can achieve this by implementing QTextStream or QDataStream for both your struct plane and your QList<plane>
-
QDataStream has automatic serizalization of base types.
Create custom write and read operators.
@
QDataStream & operator << (QDataStream & out, const plane & obj)
{
out << obj.id << obj.size_az << ...out << obj.k_x.size(); foreach (int v in k_x) { out << obj.k_x.v; }
...
return out;
}
@@
QDataStream & operator>>(QDataStream & in, plane & obj)
{
out >> obj.id;
out >> obj.size_az
out >> ...int cnt; out >> cnt; for (; cnt > 0; cnt--) { int v; out >> v; obj.k_x.push_back(v); } ... return in;
}
@Example usage.
@QFile file("file.dat");
file.open(QIODevice::ReadWrite);
QDataStream out(&file);plane thePlane;
out << thePlane; //to write
out >> thePlane; //to read
@