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. How to write and read Qbject in file
Forum Updated to NodeBB v4.3 + New Features

How to write and read Qbject in file

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 622 Views 2 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi
    https://doc.qt.io/qt-5/qdatastream.html
    is your friend.

    It can already stream most Qt types.

    Is it your own custom class you want to save and load ?

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

      Hi,

      QTextStream does not take only text as input. If you want to serialize objects you have to provide the appropriate methods and in this case these are the QTextStream operators.

      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
      1
      • U Offline
        U Offline
        UG SEP
        wrote on last edited by
        #4

        @mrjj I have tried doing this also but It shows the same error and @mrjj yes it's my custom class
        @SGaist thanks for guiding me

        mrjjM 1 Reply Last reply
        0
        • U UG SEP

          @mrjj I have tried doing this also but It shows the same error and @mrjj yes it's my custom class
          @SGaist thanks for guiding me

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #5

          @UG-SEP

          Hi
          very fast sample

          
          class MyClass
          {
              int number;
              QString name;
          
              friend QDataStream &operator<<(QDataStream &out, const MyClass &rhs);
              friend QDataStream &operator>>(QDataStream &in, MyClass &rhs);
          };
          
          
          QDataStream &operator<<(QDataStream &out, const MyClass &rhs)
          {
              out << rhs.number << rhs.name;
              return out;
          }
          
          QDataStream &operator>>(QDataStream &in, MyClass &rhs)
          {
              in  >> rhs.number >> rhs.name;
              return in;
          }
          
          void save() {
              QFile file("file.xxx");
              file.open(QIODevice::WriteOnly);
              QDataStream out(&file);
              MyClass test;
              out << test;
          }
          
          void load() {
            QFile file("file.xxx");
             file.open(QIODevice::ReadOnly); // check foir open errors!
             QDataStream in(&file);
          
              MyClass test;
              in >> test;
          }
          

          Do note for a real app, you should include error handling

          1 Reply Last reply
          1
          • U Offline
            U Offline
            UG SEP
            wrote on last edited by
            #6

            Thanks a lot @mrjj I understand whatever you did in the code if I guess it's function overloading

            mrjjM 1 Reply Last reply
            0
            • U UG SEP

              Thanks a lot @mrjj I understand whatever you did in the code if I guess it's function overloading

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @UG-SEP
              Hi
              yeah its operator overloading.
              the >> and << are operators and we "teach" them how to stream your classes (its members.)

              The neat part is that most of the Qt types already have such "overloads" and
              hence you can save QString and QPixmap and all such classes by simply doing
              << thevar;

              so in most cases you only need to make sure to stream in and out in the same order as else
              it will explode :)

              U 1 Reply Last reply
              2
              • mrjjM mrjj

                @UG-SEP
                Hi
                yeah its operator overloading.
                the >> and << are operators and we "teach" them how to stream your classes (its members.)

                The neat part is that most of the Qt types already have such "overloads" and
                hence you can save QString and QPixmap and all such classes by simply doing
                << thevar;

                so in most cases you only need to make sure to stream in and out in the same order as else
                it will explode :)

                U Offline
                U Offline
                UG SEP
                wrote on last edited by
                #8

                @mrjj I did understand why It's showing multiple definition errors because we overload that operator as a friend function
                My class Sample:

                class Player : public QDialog
                {
                    QString name,password;
                    int gp,gw,gl;
                    float wp;
                public:
                    Player(QString name,QString password)
                    {
                        this->name=name;
                        this->password=password;
                        gp=gw=gl=wp=0;
                    }
                    Player(){};
                    int operator==(Player &other)
                    {
                        if(name==other.name && password==other.password)
                            return 1;
                        else
                            return 0;
                    }
                    friend QDataStream &operator<<(QDataStream &out, const Player &new_player);
                        friend QDataStream &operator>>(QDataStream &in,Player &existing_player);
                };
                QDataStream &operator<<(QDataStream &out, const Player &new_player)
                {
                    out << new_player.name << new_player.password;
                    return out;
                }
                
                QDataStream &operator>>(QDataStream &in, Player &existing_player)
                {
                    in  >> existing_player.name >> existing_player.password;
                    return in;
                }
                
                

                Here is the file writing code

                QFile file(QDir::currentPath()+"Player.txt");
                QDataStream out(&file);
                QDataStream out_dup(&file);
                if(file.open(QIODevice::ReadWrite))
                {
                    if(!exist(new_player,out_dup))
                out << new_player;
                }
                
                
                mrjjM 1 Reply Last reply
                0
                • U UG SEP

                  @mrjj I did understand why It's showing multiple definition errors because we overload that operator as a friend function
                  My class Sample:

                  class Player : public QDialog
                  {
                      QString name,password;
                      int gp,gw,gl;
                      float wp;
                  public:
                      Player(QString name,QString password)
                      {
                          this->name=name;
                          this->password=password;
                          gp=gw=gl=wp=0;
                      }
                      Player(){};
                      int operator==(Player &other)
                      {
                          if(name==other.name && password==other.password)
                              return 1;
                          else
                              return 0;
                      }
                      friend QDataStream &operator<<(QDataStream &out, const Player &new_player);
                          friend QDataStream &operator>>(QDataStream &in,Player &existing_player);
                  };
                  QDataStream &operator<<(QDataStream &out, const Player &new_player)
                  {
                      out << new_player.name << new_player.password;
                      return out;
                  }
                  
                  QDataStream &operator>>(QDataStream &in, Player &existing_player)
                  {
                      in  >> existing_player.name >> existing_player.password;
                      return in;
                  }
                  
                  

                  Here is the file writing code

                  QFile file(QDir::currentPath()+"Player.txt");
                  QDataStream out(&file);
                  QDataStream out_dup(&file);
                  if(file.open(QIODevice::ReadWrite))
                  {
                      if(!exist(new_player,out_dup))
                  out << new_player;
                  }
                  
                  
                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #9

                  Hi

                  The body of the
                  QDataStream &operator<<(QDataStream &out, const Player &new_player)
                  { ...

                  goes into the .cpp file. Not the .h as then it will be defined multiple times.

                  so simply moved the functions into the cpp

                  U 1 Reply Last reply
                  1
                  • mrjjM mrjj

                    Hi

                    The body of the
                    QDataStream &operator<<(QDataStream &out, const Player &new_player)
                    { ...

                    goes into the .cpp file. Not the .h as then it will be defined multiple times.

                    so simply moved the functions into the cpp

                    U Offline
                    U Offline
                    UG SEP
                    wrote on last edited by
                    #10

                    @mrjj Yah I understand
                    Everything work fine and I have completed the complex part of the project
                    Thanks a lot, @mrjj the label LifeTime QT Champion describes you the best

                    mrjjM 1 Reply Last reply
                    1
                    • U UG SEP

                      @mrjj Yah I understand
                      Everything work fine and I have completed the complex part of the project
                      Thanks a lot, @mrjj the label LifeTime QT Champion describes you the best

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @UG-SEP
                      Super.
                      Just for a heads up.
                      If you plan to change the data file format over time. its often a good idea to add a version number
                      https://doc.qt.io/qt-5/qdatastream.html#versioning

                      That was you can know if you app is given an older file as else it will just try to read the file in and odd stuff will happen if you change the order of members or added new one.

                      1 Reply Last reply
                      2

                      • Login

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