QT Persistence objects are serialized (while comparing MFC and Java methods)

Source: Internet
Author: User
Tags object serialization

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.