Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDataStream serialization/deserializatin of QVector Struct example :
Forum Update on Monday, May 27th 2025

QDataStream serialization/deserializatin of QVector Struct example :

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 653 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RahibeMeryem
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • O Offline
      O Offline
      ofmrew
      wrote on last edited by
      #2

      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;
      }
      
      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved