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. QVector serialization / deserialization

QVector serialization / deserialization

Scheduled Pinned Locked Moved Solved General and Desktop
27 Posts 5 Posters 7.3k 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 mrjj
    #6

    Hi
    Lucky its not super complex.

    in .h file
    struct enrolment {
      QString person_name;
      QPixmap person_foto;
      QString category;
    };
    
    QDataStream& operator <<(QDataStream& out, const enrolment Entry);
    QDataStream& operator >>(QDataStream& in, enrolment Entry);
    

    and in .cpp

    // save
    QDataStream& operator <<(QDataStream& out, const enrolment Entry) {
       out << Entry.person_name;
      out << Entry.person_foto;
      out << Entry.category;
      return out;
    }
    
    // load. MUST BE SAME ORDER as when saved
    QDataStream& operator >>(QDataStream& in, enrolment &Entry) { 
      in >> Entry.person_name;
      in >> Entry.person_foto;
      in >> Entry.category;
      return in;
    }
    
    1 Reply Last reply
    3
    • R Offline
      R Offline
      RahibeMeryem
      wrote on last edited by mrjj
      #7

      it gave:

      error: overloaded 'operator<<' must be a binary operator (has 3 parameters)
          QDataStream& operator <<(QDataStream& out, const enrolment Entry);
      

      for

      QDataStream& operator <<(QDataStream& out, const enrolment Entry);
      QDataStream& operator >>(QDataStream& in, enrolment &Entry);
      
      mrjjM 1 Reply Last reply
      0
      • R RahibeMeryem

        it gave:

        error: overloaded 'operator<<' must be a binary operator (has 3 parameters)
            QDataStream& operator <<(QDataStream& out, const enrolment Entry);
        

        for

        QDataStream& operator <<(QDataStream& out, const enrolment Entry);
        QDataStream& operator >>(QDataStream& in, enrolment &Entry);
        
        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #8

        @RahibeMeryem
        Can you show the actual code ?
        My compiler had zero issues.

        That errors come if you put them inside class. like with friend.
        did you add just like in sample ?
        under class?

        1 Reply Last reply
        0
        • R Offline
          R Offline
          RahibeMeryem
          wrote on last edited by mrjj
          #9

          I put the private section of the header below :

          in widget.h file
          struct enrolment {
            QString person_name;
            QPixmap person_foto;
            QString category;
          };
          
          QDataStream& operator <<(QDataStream& out, const enrolment Entry);
          QDataStream& operator >>(QDataStream& in, enrolment &Entry);
          
          

          in the widget.cpp :

          void Widget::on_select_folder_enrolment_clicked()
          {
           // save
          QDataStream& operator <<(QDataStream& out, const enrolment Entry) {
             out << Entry.person_name;
            out << Entry.person_foto;
            out << Entry.category;
            return out;
          }
          
          // load. MUST BE SAME ORDER as when saved
          QDataStream& operator >>(QDataStream& in, enrolment &Entry) { 
            in >> Entry.person_name;
            in >> Entry.person_foto;
            in >> Entry.category;
            return in;
          }
          ....
          ....
          
          
          
          
          mrjjM 1 Reply Last reply
          0
          • R RahibeMeryem

            I put the private section of the header below :

            in widget.h file
            struct enrolment {
              QString person_name;
              QPixmap person_foto;
              QString category;
            };
            
            QDataStream& operator <<(QDataStream& out, const enrolment Entry);
            QDataStream& operator >>(QDataStream& in, enrolment &Entry);
            
            

            in the widget.cpp :

            void Widget::on_select_folder_enrolment_clicked()
            {
             // save
            QDataStream& operator <<(QDataStream& out, const enrolment Entry) {
               out << Entry.person_name;
              out << Entry.person_foto;
              out << Entry.category;
              return out;
            }
            
            // load. MUST BE SAME ORDER as when saved
            QDataStream& operator >>(QDataStream& in, enrolment &Entry) { 
              in >> Entry.person_name;
              in >> Entry.person_foto;
              in >> Entry.category;
              return in;
            }
            ....
            ....
            
            
            
            
            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #10

            @RahibeMeryem

            ohhh o.O
            Ok. do not put them inside the functions.
            have them outside as a function on its own.
            it allows you to do

            QFile personel_file("/data/personelVec.dat");
                    personel_file.open(QIODevice::WriteOnly);
                    QDataStream out_personelVec(&personel_file);   // we will serialize the data into the file
                    out_personelVec.setVersion(QDataStream::Qt_5_10);
                    out_personelVec << enrolment_Db;
            

            as you tried before as we now gave it a << to be used
            out_personelVec << enrolment_Db;

            so its

            
            QDataStream& operator <<(QDataStream& out, const enrolment Entry) {
            ... not showing code. but dont delete !
            }
            
            QDataStream& operator >>(QDataStream& in, enrolment &Entry) { 
            ...
            }
            
            
            void Widget::on_select_folder_enrolment_clicked()
            {
            QFile personel_file("/data/personelVec.dat");
                    personel_file.open(QIODevice::WriteOnly);
                    QDataStream out_personelVec(&personel_file);   // we will serialize the data into the file
                    out_personelVec.setVersion(QDataStream::Qt_5_10);
                    out_personelVec << enrolment_Db;
            }
            
            1 Reply Last reply
            2
            • R Offline
              R Offline
              RahibeMeryem
              wrote on last edited by
              #11

              :))

              Thanks a lot .. I am math and deep learning expert.. most of the my time spending non main subject related struggling like this.

              Qt is Wonderfull but needs deep knowledge and operational expertise which is require time and facing problems.

              Best

              mrjjM 1 Reply Last reply
              0
              • R RahibeMeryem

                :))

                Thanks a lot .. I am math and deep learning expert.. most of the my time spending non main subject related struggling like this.

                Qt is Wonderfull but needs deep knowledge and operational expertise which is require time and facing problems.

                Best

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

                @RahibeMeryem
                well operator overloading ( << and >> are operators ) is somewhat
                intermediate level.

                Just for the fullness of this post.

                You could have done
                out_personelVec << enrolment_Db. person_name << enrolment_Db. person_foto ;
                since it know how to stream QString and pixmap already.

                However, the operator overloading is smart if you later do
                struct SomeOther {
                enrolment en;
                };

                if you then overload for "SomeOther"
                then
                SomeOther test;
                out << test;
                will just work as it calls << for enrolment
                so it allows for deep nesting of Types, each with the ability to save it self.

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

                  One small thing, it should be:

                  QDataStream& operator >>(QDataStream& in, enrolment& Entry);
                  

                  Otherwise there's no way to get the data back in the caller function.

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

                  mrjjM 1 Reply Last reply
                  2
                  • SGaistS SGaist

                    One small thing, it should be:

                    QDataStream& operator >>(QDataStream& in, enrolment& Entry);
                    

                    Otherwise there's no way to get the data back in the caller function.

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

                    @SGaist
                    Good catch. Thank you. Updated sample.
                    and remember to change both in .h and in .cpp. (RahibeMeryem)

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      RahibeMeryem
                      wrote on last edited by
                      #15

                      Serialization is ok ;

                      deserialization gives below error:

                      /Users/alpullu/Qt/5.11.0/clang_64/lib/QtCore.framework/Headers/qdatastream.h:243: error: use of overloaded operator '>>' is ambiguous (with operand types 'QDataStream' and 'typename QVector<enrolment_>::value_type' (aka 'enrolment_'))
                              s >> t;
                              ~ ^  ~
                      
                      // load. MUST BE SAME ORDER as when saved
                      QDataStream& operator >>(QDataStream& in, enrolment_& Entry) {
                        in >> Entry.person_name;
                        in >> Entry.person_foto;
                        in >> Entry.category;
                        in >> Entry.face_indexed_status;
                        return in;
                      }
                      
                      mrjjM 1 Reply Last reply
                      0
                      • R RahibeMeryem

                        Serialization is ok ;

                        deserialization gives below error:

                        /Users/alpullu/Qt/5.11.0/clang_64/lib/QtCore.framework/Headers/qdatastream.h:243: error: use of overloaded operator '>>' is ambiguous (with operand types 'QDataStream' and 'typename QVector<enrolment_>::value_type' (aka 'enrolment_'))
                                s >> t;
                                ~ ^  ~
                        
                        // load. MUST BE SAME ORDER as when saved
                        QDataStream& operator >>(QDataStream& in, enrolment_& Entry) {
                          in >> Entry.person_name;
                          in >> Entry.person_foto;
                          in >> Entry.category;
                          in >> Entry.face_indexed_status;
                          return in;
                        }
                        
                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #16

                        @RahibeMeryem

                        Please always show code as you being unused to Qt / c++ might
                        give situation where you did something in code we would never think of.

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          RahibeMeryem
                          wrote on last edited by
                          #17

                          I exactly copied your deserialization example :

                          // load. MUST BE SAME ORDER as when saved
                          QDataStream& operator >>(QDataStream& in, enrolment_& Entry) {
                            in >> Entry.person_name;
                            in >> Entry.person_foto;
                            in >> Entry.category;
                            in >> Entry.face_indexed_status;
                            return in;
                          }
                          
                          honestly didnt understand why its not working . :(
                          
                           
                          
                          mrjjM 1 Reply Last reply
                          0
                          • R RahibeMeryem

                            I exactly copied your deserialization example :

                            // load. MUST BE SAME ORDER as when saved
                            QDataStream& operator >>(QDataStream& in, enrolment_& Entry) {
                              in >> Entry.person_name;
                              in >> Entry.person_foto;
                              in >> Entry.category;
                              in >> Entry.face_indexed_status;
                              return in;
                            }
                            
                            honestly didnt understand why its not working . :(
                            
                             
                            
                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #18

                            @RahibeMeryem
                            hi
                            but does the error point to this ?
                            I assumed it was where you used it.
                            as in where you try to load in the file again.
                            Can you show that code ?

                            1 Reply Last reply
                            0
                            • R Offline
                              R Offline
                              RahibeMeryem
                              wrote on last edited by
                              #19

                              the error is in this line : in_personelVec >> enrolment_Db;
                              gives:
                              /Users/xx/Qt/5.11.0/clang_64/lib/QtCore.framework/Headers/qdatastream.h:243: error: use of overloaded operator '>>' is ambiguous (with operand types 'QDataStream' and 'typename QVector<enrolment_>::value_type' (aka 'enrolment_'))
                              s >> t;
                              ~ ^ ~

                              void Widget::load_enrollment_if_exist()
                              {
                                  bool fileExists = QFileInfo::exists(enrollment_path) && QFileInfo(enrollment_path).isFile();
                                  if (fileExists){
                                      QFile personel_file(enrollment_path);
                                              personel_file.open(QIODevice::ReadOnly);
                                              QDataStream in_personelVec(&personel_file);   // we will deserialize the data into the file
                                              in_personelVec.setVersion(QDataStream::Qt_5_10);
                                              in_personelVec >>  enrolment_Db;  ///////////THE ERROR is HERE
                                              qDebug() << " enrolment yüklendi: , personel sayısı: " << enrolment_Db.size()  ;
                                  }
                              
                              }
                              
                              // save
                              QDataStream& operator <<(QDataStream& out, const enrolment_ Entry) {
                                out << Entry.person_name;
                                out << Entry.person_foto;
                                out << Entry.category;
                                out << Entry.face_indexed_status;
                                return out;
                              }
                              
                              // load. MUST BE SAME ORDER as when saved
                              QDataStream& operator >>(QDataStream& in, enrolment_& Entry) {
                                in >> Entry.person_name;
                                in >> Entry.person_foto;
                                in >> Entry.category;
                                in >> Entry.face_indexed_status;
                                return in;
                              }
                              
                              1 Reply Last reply
                              0
                              • R Offline
                                R Offline
                                RahibeMeryem
                                wrote on last edited by
                                #20

                                Do we need to write QVector<enrolment_> size ? because it has 1000 elements ?

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

                                  What type is Entry.face_indexed_status ?

                                  No you don't have to do anything, QVector is already a supported type so as long you provide the data stream operator for your class you're good.

                                  Note that again, you have to handle that properly in your code, meaning that if you stream something, then your vector then something else, in your read code you have to again do that in the same order.

                                  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
                                  • R Offline
                                    R Offline
                                    RahibeMeryem
                                    wrote on last edited by RahibeMeryem
                                    #22

                                    in the header :

                                    struct enrolment_ {
                                    QString person_name;
                                    QPixmap person_foto;
                                    QString category;
                                    QString file;
                                    QString face_indexed_status;
                                    };

                                    QDataStream& operator <<(QDataStream& out, const enrolment_ Entry);
                                    QDataStream& operator >>(QDataStream& in, enrolment_ Entry);

                                    mrjjM 1 Reply Last reply
                                    0
                                    • R RahibeMeryem

                                      in the header :

                                      struct enrolment_ {
                                      QString person_name;
                                      QPixmap person_foto;
                                      QString category;
                                      QString file;
                                      QString face_indexed_status;
                                      };

                                      QDataStream& operator <<(QDataStream& out, const enrolment_ Entry);
                                      QDataStream& operator >>(QDataStream& in, enrolment_ Entry);

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

                                      @RahibeMeryem

                                      Did you add the missing & to BOTH .h and cpp ?
                                      seems to be missing in .h
                                      QDataStream& operator >>(QDataStream& in, enrolment_ & Entry); // NOTE the &

                                      please check u changed in both places.

                                      1 Reply Last reply
                                      1
                                      • R Offline
                                        R Offline
                                        RahibeMeryem
                                        wrote on last edited by
                                        #24

                                        Finally I figured it out ... to find that I missed '&' . in .h . took half day with 250 litre coffee :)))

                                        But I want to thanks you all Gentlemen...

                                        Really this is the best and useful answer for me all my Qt adventure

                                        THANKS
                                        THANKS..
                                        0_1541342863330_eda44b14-6526-4720-893a-23f408e590b4-image.png

                                        mrjjM 1 Reply Last reply
                                        1
                                        • R RahibeMeryem

                                          Finally I figured it out ... to find that I missed '&' . in .h . took half day with 250 litre coffee :)))

                                          But I want to thanks you all Gentlemen...

                                          Really this is the best and useful answer for me all my Qt adventure

                                          THANKS
                                          THANKS..
                                          0_1541342863330_eda44b14-6526-4720-893a-23f408e590b4-image.png

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

                                          @RahibeMeryem
                                          Super!
                                          The good part being - this is the way for all classes you make.
                                          So you now can stream anything you like.

                                          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