QDataStream serialization/deserializatin of QVector Struct example :
Unsolved
General and Desktop
-
Hi,
I need an example for the below struct vector serialzation/deserialzation from / to File:
struct q_person_struct { QVector<float> q_f_desc; QString name; QImage image; }; QVector<q_person_struct> person;
Then there will be almost 10.000 person in the person QVector.
I tried from tutorials but some point I lost the direction.
I will be appreciated if you provide help, and could be helpfull for the other newbies.
Best
thanks
Maria
-
Try this:
class MyClass { public: explicit MyClass(); MyClass(QRectF r, QLineF l); QRectF br; QLineF ln; signals: public slots: }; QDataStream &operator <<(QDataStream &out, const MyClass &mc); QDataStream &operator >>(QDataStream &in, MyClass &mc); MyClass::MyClass() {} MyClass::MyClass(QRectF r, QLineF l) : br{r}, ln{l} { } QDataStream &operator <<(QDataStream &out, const MyClass &mc){ out << mc.br << mc.ln; return out; } QDataStream &operator >>(QDataStream &in, MyClass &mc){ in >> mc.br >> mc.ln; return in; } class MyCanvas : public QWidget { Q_OBJECT public: explicit MyCanvas(QWidget *parent = nullptr); void create(); void store(); void load(); QVector<MyClass> cell; QVector<MyClass> cell2; QDataStream stream; signals: public slots: }; void MyCanvas::create() { MyClass line1(QRectF(50.0, 50.0, 200.0, 200.0), QLineF(50.0, 50.0, 250.0, 250.0)); cell.append(line1); MyClass line2(QRectF(250.0, 50.0, 200.0, 200.0), QLineF(250.0, 250.0, 500.0, 50.0)); cell.append(line2); } void MyCanvas::store() { QFile file("test1.dat"); file.open(QIODevice::WriteOnly); QDataStream out(&file); out << cell; } void MyCanvas::load() { QFile file("test1.dat"); file.open(QIODevice::ReadOnly); QDataStream in(&file); in >> cell2; }