C++/QT Serialization Operations

Source: Internet
Author: User

The meaning of a serialization operation is, by definition, saving data to a storage device in a certain order, or reading a sequence of data from a storage device, and the data type is consistent (that is, the data you read is the type I know). The process knows, how to do it?

I am a simple program implemented with the help of the QT framework. (The framework, of course, implements the complex part.) If in your encapsulated data, meaning non-QT standard data types, then it is necessary to implement the "<<" and ">>" operators to implement a custom type of serialization operations).

The specific implementation process is as follows:

Order.h

#include <QDataStream>

#include <QDateTime>

#include <QFile>


typedef struct __MY_STRCT

{

QString _name;

unsigned int _id;

}SMY_STRCT;


Qdatastream & operator << (qdatastream& out, const SMY_STRCT &data);

Qdatastream &operator >> (qdatastream& in, smy_strct &data);


Class Orderclstest

{

Public

Orderclstest ();

void Store ();

void load ();


Private

Qdatetime _lasttime;

};


Order.cpp

#include "Orderclstest.h"

#include <QDebug>


Orderclstest::orderclstest ()

{

Store ();

}


void Orderclstest::load ()

{

QFile file ("./data.dat");

if (!file.open (qiodevice::readonly)) return;


SMY_STRCT _my_strct;

Qdatastream in (&file);

In >> _lasttime >> _my_strct;

File.close ();

}


void Orderclstest::store ()

{

QFile file ("./data.dat");

if (!file.open (qiodevice::writeonly)) {return;}


_lasttime = Qdatetime::currentdatetime ();

Qdatastream out (&file);

SMY_STRCT _my_strct;

_my_strct._id = 123;

_my_strct._name = "Hello";


Out << _lasttime << _my_strct;


File.close ();

}


qdatastream& operator << (qdatastream &out, const SMY_STRCT &data)

{

Out << data._id << data._name;

return out;

}


qdatastream& operator >> (Qdatastream &in, smy_strct &data)

{

In >> data._id >> data._name;

return in;

}

int main (int argc, char *argv[])

{

Orderclstest test;

Test.load ();

}


Isn't it simple? Working on the Framework Development Guide These are all right, if you want to achieve the true meaning of the serialization operation, you can refer to the MFC persistence mechanism, that is, my previous blog. You'll see a lot.


This article is from the "GDT Commentary" blog, make sure to keep this source http://844133395.blog.51cto.com/3483632/1723255

C++/QT serialization Operation

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.