The objects of the custom classes in MFC and Java can be persisted, and the Qt Persistence object serialization is certainly essential.
But this problem really bothered me for a long time ...
MFC by overriding virtual functions serialize (), Java is the class that belongs to must implement Java.io.Serializable or Externalizable interface complete,
How does QT fix this thing, Qdatastream class: Let us think of MFC in the Cachive class,
The Java.io.objectoutputstream/objectinputstream class in Java provides the serialization and deserialization capabilities of an object, respectively.
What's worse now is the realization of ">>" and "<<".
Haha, C + + friend friends in the useful. Assuming your class is qsampledata, you can implement the following two functions.
#ifndef Qt_no_datastream
Friend qdatastream& operator>> (qdatastream&, qsampledata&);
Friend qdatastream& operator<< (qdatastream&, qsampledata&);
#endif
The thing that can not be ignored here is that qsampledata derives directly or indirectly from Qobject, there is a constructor without parameters, which is a cliché.
An example, which is also necessary.
More lazy, this time the comments will be free ...
Class Qsampledata:public Qobject
{
Public
Qchunneldata ();
Virtual ~qchunneldata ();
qchunneldata& operator= (const qchunneldata &other);
#ifndef Qt_no_datastream
Friend qdatastream& operator>> (qdatastream&, qchunneldata&);
Friend qdatastream& operator<< (qdatastream&, qchunneldata&);
#endif
Data members that are defined
int m_ntype;
QString M_strname;
};
Csampledata::csampledata ()
{
M_ntype = 0;
M_strname = "";
}
Csampledata::~csampledata ()
{
}
Csampledata::operator = (const csampledata& Other)
{
M_ntype = Other.m_ntype;
M_strname = Other.m_strname;
return *this;
}
#ifndef Qt_no_datastream
qdatastream& operator>> (qdatastream& in, csampledata& data)
{
In >> data.m_ntype >> data.m_strname;
return in;
}
qdatastream& operator<< (qdatastream& out, csampledata& data)
{
Out << data.m_ntype << data.m_strname;
return out;
}
#endif
This is Qfile, Qbytearray ... You can associate an object of the Qdatastream class with read-write operations.
Qsampledata data;
Data.m_ntpye = 12;
Data.m_strname = "Vic." MINg ";
QFile file ("file.dat");
File.Open (qiodevice::writeonly);
Qdatastream out (&file);
Out << data;
File.close ();
File.Open (qiodevice::readonly);
Qdatastream in (&file);
in >> data;
File.close ();
http://cool.worm.blog.163.com/blog/static/64339006200832642918250/
QT Persistence objects are serialized (while comparing MFC and Java methods)