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 Example
Forum Update on Monday, May 27th 2025

Serialization Example

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 3 Posters 4.0k 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.
  • O Offline
    O Offline
    ofmrew
    wrote on last edited by
    #1

    I am trying to serialize my class but I cannot find an example. I tried the code below, but I get that it does not define a type.

    #include <QObject>
    #include <QDataStream>
    #include <QRectF>
    #include <QLineF>
    #include <QDebug>
    
    class MyClass : public QObject
    {
        Q_OBJECT
    public:
        explicit MyClass(QObject *parent = nullptr);
    
        QDataStream &MyClass;::operator <<(QDataStream &out, const MyClass &mc){
            out << mc.br << mc.ln;
            return out;
    
        }
        QDataStream &MyClass;::operator >>(QDataStream &in, const MyClass &mc){
            in >> mc.br >> mc.ln;
            return in;
        }
        QRectF br;
        QLineF ln;
    
    signals:
    
    public slots:
    };
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Declare the stream operators in the same file but outside of your class and implement them in the implementation file from your class.

      By the way, you have a semicolon between MyClass and the two colon.

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

      O 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        Declare the stream operators in the same file but outside of your class and implement them in the implementation file from your class.

        By the way, you have a semicolon between MyClass and the two colon.

        O Offline
        O Offline
        ofmrew
        wrote on last edited by
        #3

        @SGaist As for the semicolon, that came from an example "http://www.bogotobogo.com/Qt/Qt5_QFile_Serialization_Class.php".

        class MyClass : public QObject
        {
            Q_OBJECT
        public:
            explicit MyClass(QObject *parent = nullptr);
        
        
            QRectF br;
            QLineF ln;
        
        signals:
        
        public slots:
        };
        QDataStream &MyClass;::operator <<(QDataStream &out, const MyClass &mc){
            out << mc.br << mc.ln;
            return out;
        
        }
        QDataStream &MyClass;::operator >>(QDataStream &in, const MyClass &mc){
            in >> mc.br >> mc.ln;
            return in;
        }
        

        That did not work.

        O 1 Reply Last reply
        0
        • O ofmrew

          @SGaist As for the semicolon, that came from an example "http://www.bogotobogo.com/Qt/Qt5_QFile_Serialization_Class.php".

          class MyClass : public QObject
          {
              Q_OBJECT
          public:
              explicit MyClass(QObject *parent = nullptr);
          
          
              QRectF br;
              QLineF ln;
          
          signals:
          
          public slots:
          };
          QDataStream &MyClass;::operator <<(QDataStream &out, const MyClass &mc){
              out << mc.br << mc.ln;
              return out;
          
          }
          QDataStream &MyClass;::operator >>(QDataStream &in, const MyClass &mc){
              in >> mc.br >> mc.ln;
              return in;
          }
          

          That did not work.

          O Offline
          O Offline
          ofmrew
          wrote on last edited by
          #4

          @ofmrew Forgot to remove the semicolons. With them removed I get:

          /home/xxx/SerializationExperiments/myclass.h:24: error: ‘QDataStream& MyClass::operator<<(QDataStream&, const MyClass&)’ must take exactly one argument
           QDataStream &MyClass::operator <<(QDataStream &out, const MyClass &mc){
                                                                                ^
          
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            That's a typo, remove it.

            By the way, the operator>> takes a reference, not a const reference. Otherwise you won't be able to update the content of the object you pass.

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

            O 1 Reply Last reply
            2
            • SGaistS SGaist

              That's a typo, remove it.

              By the way, the operator>> takes a reference, not a const reference. Otherwise you won't be able to update the content of the object you pass.

              O Offline
              O Offline
              ofmrew
              wrote on last edited by
              #6

              @SGaist

              QDataStream &MyClass::operator <<(QDataStream &out, MyClass &mc){
                  out << mc.br << mc.ln;
                  return out;
              
              }
              QDataStream &MyClass::operator >>(QDataStream &in, MyClass &mc){
                  in >> mc.br >> mc.ln;
                  return in;
              }
              
              /home/xxx/SerializationExperiments/myclass.h:24: error: ‘QDataStream& MyClass::operator<<(QDataStream&, MyClass&)’ must take exactly one argument
               QDataStream &MyClass::operator <<(QDataStream &out, MyClass &mc){
                                                                              ^
              

              It dtill complains about more than one argument. How do I fix that?

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

                Remove MyClass;:: from the declaration.

                It's shown in the Reading And Writing Other Qt Classes part of QDataStream's documentation.

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

                O 1 Reply Last reply
                3
                • SGaistS SGaist

                  Remove MyClass;:: from the declaration.

                  It's shown in the Reading And Writing Other Qt Classes part of QDataStream's documentation.

                  O Offline
                  O Offline
                  ofmrew
                  wrote on last edited by
                  #8

                  @SGaist Did the "http://www.bogotobogo.com/Qt/Qt5_QFile_Serialization_Class.php" ever work? That is the only example I could find to use as a model.

                  QDataStream &operator <<(QDataStream &out, MyClass &mc);

                  Compiles, but do I need to put the implementation in the cpp file? If so, what is the format?

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

                    Copy the methods as they are in the implementation file and just leave the declaration in the header.

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

                    O 1 Reply Last reply
                    2
                    • SGaistS SGaist

                      Copy the methods as they are in the implementation file and just leave the declaration in the header.

                      O Offline
                      O Offline
                      ofmrew
                      wrote on last edited by
                      #10

                      @SGaist That compiled. Not to testing. Thanks.

                      O 1 Reply Last reply
                      0
                      • O ofmrew

                        @SGaist That compiled. Not to testing. Thanks.

                        O Offline
                        O Offline
                        ofmrew
                        wrote on last edited by
                        #11

                        @ofmrew The status after the write to datastream is 0, but the >> does not appear to work. I added MyClass to a vector and tried to serialize it, but it failed to return the vector from the stream. A breakpoint, in MyClass, on << is activated, but one on >> is not.

                        class MyCanvas : public QWidget
                        {
                            Q_OBJECT
                        public:
                            explicit MyCanvas(QWidget *parent = nullptr);
                        
                            void create();
                            void store();
                            void load();
                        
                            QVector<MyClass> cell;
                            QDataStream stream;
                        
                        
                        MyCanvas::MyCanvas(QWidget *parent) : QWidget(parent)
                        {
                        
                        }
                        
                        void MyCanvas::create()
                        {
                        //    QVector<MyClass>cell;
                        
                            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()
                        {
                        //    stream = new QDataStream();
                            stream<<cell;
                            qDebug() << "status" << stream.status();
                        //    qDebug() << stream;
                        }
                        
                        void MyCanvas::load()
                        {
                        //    cell2 = new QVector<MyClass>();
                            cell.clear();
                            stream>>cell;
                            qDebug() << cell.size();
                        //    qDebug() << cell;
                        }
                        class MyClass
                        {
                        public:
                            explicit MyClass();
                            MyClass(QRectF r, QLineF l);
                            void addBoundingRect(QRectF);
                            void addLine(QLineF);
                        
                        
                            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}
                        {
                        
                        }
                        
                        void MyClass::addBoundingRect(QRectF r)
                        {
                            br = r;
                        }
                        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;
                        }
                        
                        ```Is it failing to create MyClass?
                        O 1 Reply Last reply
                        0
                        • O ofmrew

                          @ofmrew The status after the write to datastream is 0, but the >> does not appear to work. I added MyClass to a vector and tried to serialize it, but it failed to return the vector from the stream. A breakpoint, in MyClass, on << is activated, but one on >> is not.

                          class MyCanvas : public QWidget
                          {
                              Q_OBJECT
                          public:
                              explicit MyCanvas(QWidget *parent = nullptr);
                          
                              void create();
                              void store();
                              void load();
                          
                              QVector<MyClass> cell;
                              QDataStream stream;
                          
                          
                          MyCanvas::MyCanvas(QWidget *parent) : QWidget(parent)
                          {
                          
                          }
                          
                          void MyCanvas::create()
                          {
                          //    QVector<MyClass>cell;
                          
                              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()
                          {
                          //    stream = new QDataStream();
                              stream<<cell;
                              qDebug() << "status" << stream.status();
                          //    qDebug() << stream;
                          }
                          
                          void MyCanvas::load()
                          {
                          //    cell2 = new QVector<MyClass>();
                              cell.clear();
                              stream>>cell;
                              qDebug() << cell.size();
                          //    qDebug() << cell;
                          }
                          class MyClass
                          {
                          public:
                              explicit MyClass();
                              MyClass(QRectF r, QLineF l);
                              void addBoundingRect(QRectF);
                              void addLine(QLineF);
                          
                          
                              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}
                          {
                          
                          }
                          
                          void MyClass::addBoundingRect(QRectF r)
                          {
                              br = r;
                          }
                          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;
                          }
                          
                          ```Is it failing to create MyClass?
                          O Offline
                          O Offline
                          ofmrew
                          wrote on last edited by
                          #12

                          @ofmrew Ignore the last post: Like an idiot I missed the fact that QDataStream is a file. Thanks for the help.

                          O 1 Reply Last reply
                          0
                          • O ofmrew

                            @ofmrew Ignore the last post: Like an idiot I missed the fact that QDataStream is a file. Thanks for the help.

                            O Offline
                            O Offline
                            ofmrew
                            wrote on last edited by
                            #13

                            @ofmrew I tested the serialization both out and in. Now I am trying to serialize QMap<int, QVector<PixmapTile>> levelMap;. The out seemed to work, but on in it is having a problem with QVector<PixmapTile>.

                            /home/xxx/Qt/5.10.0/gcc_64/include/QtCore/qdatastream.h:242: error: no match for ‘operator>>’ (operand types are ‘QDataStream’ and ‘QVector<PixmapTile>::value_type {aka PixmapTile}’)
                                     s >> t;
                             QDataStream &operator<<(QDataStream &s, const PixmapTile &pixmt);
                            QDataStream &operator<<(QDataStream &s, PixmapTile &pixmt);
                            QDataStream &operator<<(QDataStream &s, QVector<PixmapTile> &vofpt);
                            

                            Am I asking too much?

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

                              IIRC, you don't need to implement the QVector version because it should be automatically handled as you implemented the operators for your custom data type.

                              There might be a typo you only have operator<< declared in your last code sample.

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

                              O 1 Reply Last reply
                              0
                              • SGaistS SGaist

                                IIRC, you don't need to implement the QVector version because it should be automatically handled as you implemented the operators for your custom data type.

                                There might be a typo you only have operator<< declared in your last code sample.

                                O Offline
                                O Offline
                                ofmrew
                                wrote on last edited by
                                #15

                                @SGaist Good eyes.

                                QDataStream &operator>>(QDataStream &s, PixmapTile &pixmt);
                                //QDataStream &operator<<(QDataStream &s, QVector<PixmapTile> &vofpt);
                                

                                Got it to compile. Copy and paste can bit you. Thanks.

                                O VRoninV 2 Replies Last reply
                                0
                                • O ofmrew

                                  @SGaist Good eyes.

                                  QDataStream &operator>>(QDataStream &s, PixmapTile &pixmt);
                                  //QDataStream &operator<<(QDataStream &s, QVector<PixmapTile> &vofpt);
                                  

                                  Got it to compile. Copy and paste can bit you. Thanks.

                                  O Offline
                                  O Offline
                                  ofmrew
                                  wrote on last edited by
                                  #16

                                  @ofmrew I checked it with the debugger and it looks good. I am impressed.

                                  1 Reply Last reply
                                  0
                                  • O ofmrew

                                    @SGaist Good eyes.

                                    QDataStream &operator>>(QDataStream &s, PixmapTile &pixmt);
                                    //QDataStream &operator<<(QDataStream &s, QVector<PixmapTile> &vofpt);
                                    

                                    Got it to compile. Copy and paste can bit you. Thanks.

                                    VRoninV Offline
                                    VRoninV Offline
                                    VRonin
                                    wrote on last edited by
                                    #17

                                    @ofmrew said in Serialization Example:

                                    QDataStream &operator<<(QDataStream &s, QVector<PixmapTile> &vofpt);

                                    As mentioned by @SGaist you don't need to implement the operator for QVector<PixmapTile>. Qt already has template operators for QMap and QVector you just need to implement QDataStream &operator>>(QDataStream &s, PixmapTile &pixmt); and QDataStream &operator<<(QDataStream &s, const PixmapTile &pixmt);. the rest is for free

                                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                    ~Napoleon Bonaparte

                                    On a crusade to banish setIndexWidget() from the holy land of Qt

                                    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