定义:
对象转换为二进制——>序列化
二进制转换为对象——>反序列化
序列化:
Qt中实现对象序列化的类是QDataStream,写二进制到流中
QFile file("file.dat"); //创建一个文档
file.open(QIODevice::WriteOnly);//打开并只写
QDataStream out(&file); //序列化文档
out << QString("the answer is");//将字符串写入文档中,实际在该文档中为二进制
out << (qint32)42;
反序列化:
从流中读入二进制
QFile file("file.dat");
file.open(QIODevice::ReadOnly);
QDataStream in(&file); // read the data serialized from the file
QString str;
qint32 a;
in >> str >> a;
- 提醒:当Qt的Qdatastream 满足不了定义的数据对象的序列化和反序列化时
需要对已有的数据对象进行<<和>>的重载函数实现。