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. serialization
Qt 6.11 is out! See what's new in the release blog

serialization

Scheduled Pinned Locked Moved General and Desktop
5 Posts 5 Posters 2.6k Views 1 Watching
  • 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.
  • I Offline
    I Offline
    Ivan Filko
    wrote on last edited by
    #1

    hello everyone, I have no experience in serialization. I have a problem like:
    This is my
    struct plane
    {
    int id;
    int size_az;
    int size_dal;

     int amplityda;
     double speed;
     QList <int> k_x, k_y;
     QList <int> k_az, k_dal;
    

    };

    and
    QList <plane> all_plane;

    I need to serializ and deserializ all_plane, use the file.
    Please help me.
    I need an example step by step.

    [[merged all serialization threads, Tobias]]

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bjanuario
      wrote on last edited by
      #2

      Hi, hope this can help you ...

      @#include <QtCore>
      #include <QApplication>
      #include <QHash>
      #include <QFile>
      #include <QDataStream>

      #define PATH "./file.dat"

      int main()
      {

      //create a dictionary
      QHash<QString,QString> dict;
      dict["project.owner"] = “owner”;
      dict["project.version"] = “0.10.0″;
      
      QFile file&#40;PATH&#41;;
      file.open(QIODevice::WriteOnly);
      QDataStream out(&file);   // write the data
      out << dict;
      file.close();
      
      //setting new a value
      dict["project.owner"] = “new”;
      
      //update the dictionary
      file.open(QIODevice::ReadOnly);
      QDataStream in(&file);   // read the data serialized from the file
      in >> dict;
      
      qDebug() << “value: ” << dict.value(”project.owner”);
      return 0;
      

      }@

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Please use coding tags with your code, without it's difficult to read it.

        You can achieve this by implementing QTextStream or QDataStream for both your struct plane and your QList<plane>

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • 8 Offline
          8 Offline
          8majkel8
          wrote on last edited by
          #4

          QDataStream has automatic serizalization of base types.

          Create custom write and read operators.

          @
          QDataStream & operator << (QDataStream & out, const plane & obj)
          {
          out << obj.id << obj.size_az << ...

          out << obj.k_x.size();
          foreach (int v in k_x) {
             out << obj.k_x.v;
          }
          

          ...

          return out;
          

          }
          @

          @
          QDataStream & operator>>(QDataStream & in, plane & obj)
          {
          out >> obj.id;
          out >> obj.size_az
          out >> ...

          int cnt; out >> cnt;
          for (; cnt > 0; cnt--) {
             int v; out >> v;
             obj.k_x.push_back(v);
          }
          
          ...
          
          return in;
          

          }
          @

          Example usage.

          @QFile file("file.dat");
          file.open(QIODevice::ReadWrite);
          QDataStream out(&file);

          plane thePlane;
          out << thePlane; //to write
          out >> thePlane; //to read
          @

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qxoz
            wrote on last edited by
            #5

            Добрый день Ivan Filko.
            На форуме лучше не дублировать посты, к какому бы разделу они не относились видны для всех одинаково.

            1 Reply Last reply
            0

            • Login

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