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 serialze & deserialize 'myclass' pointer
Forum Updated to NodeBB v4.3 + New Features

QDataStream serialze & deserialize 'myclass' pointer

Scheduled Pinned Locked Moved Solved General and Desktop
24 Posts 4 Posters 9.4k 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.
  • V VRonin
    18 Oct 2017, 15:10

    You are leaking memory like a bandit here.

    QList<myclass> here instead of QVector<myclass*> is as efficient, less prone to memory leaks and doesn't require you to hack operator>> and operator<< for pointers type

    T Offline
    T Offline
    Taz742
    wrote on 18 Oct 2017, 18:08 last edited by
    #5

    @VRonin waitttt. If I use QList, I will not have to teach DataStream how to arrange my class?

    Do what you want.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 18 Oct 2017, 18:12 last edited by VRonin
      #6

      yes but you wouldn't have to hack your way through it. You'd implement QDataStream & operator << (QDataStream &stream, const myclass& cls) and QDataStream & operator >> (QDataStream &stream, myclass& cls)
      QList doesn't require a copy of the elements when it grows in size

      "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

      T 1 Reply Last reply 18 Oct 2017, 18:17
      0
      • V VRonin
        18 Oct 2017, 18:12

        yes but you wouldn't have to hack your way through it. You'd implement QDataStream & operator << (QDataStream &stream, const myclass& cls) and QDataStream & operator >> (QDataStream &stream, myclass& cls)
        QList doesn't require a copy of the elements when it grows in size

        T Offline
        T Offline
        Taz742
        wrote on 18 Oct 2017, 18:17 last edited by
        #7

        @VRonin I still use std :: vector <T> when I write programmatic tasks.
        It's so deeply in my subconscious that I'm not trying to see what QList gives me better than QVector.

        Do what you want.

        1 Reply Last reply
        0
        • V Offline
          V Offline
          VRonin
          wrote on 18 Oct 2017, 18:27 last edited by VRonin
          #8

          you can imagine both QVector<T>(n) and std::vector<T>(n) as wrappers around T* vec = new T[n] while QList<T>(n) can be imagined as a wrapper around T** vec=new T*[n]; for(int i=0;i<n;++i) vec[i] = new T;

          Now imagine inserting an item in the middle in those 2 cases.

          That's why the docs suggests using QVector for types that are Q_MOVABLE_TYPE (the name can be misleading, see Q_DECLARE_TYPEINFO) and QList for the others

          QVector<myclass*> containing heap allocated objects that QVector (should) own is, in practice, the same as QList<myclass>

          "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

          T 1 Reply Last reply 19 Oct 2017, 09:52
          4
          • V VRonin
            18 Oct 2017, 18:27

            you can imagine both QVector<T>(n) and std::vector<T>(n) as wrappers around T* vec = new T[n] while QList<T>(n) can be imagined as a wrapper around T** vec=new T*[n]; for(int i=0;i<n;++i) vec[i] = new T;

            Now imagine inserting an item in the middle in those 2 cases.

            That's why the docs suggests using QVector for types that are Q_MOVABLE_TYPE (the name can be misleading, see Q_DECLARE_TYPEINFO) and QList for the others

            QVector<myclass*> containing heap allocated objects that QVector (should) own is, in practice, the same as QList<myclass>

            T Offline
            T Offline
            Taz742
            wrote on 19 Oct 2017, 09:52 last edited by Taz742
            #9

            @VRonin Do you mean this?

                   if (query.exec()) {
                        QList<Contact> *_list = new QList<Contact>();
                        while (query.next()) {
                            _list->push_back(Contact(query.value("UID").toInt(),
                                                     query.value("USERNAME").toString(),
                                                     query.value("MOBILE").toString(),
                                                     )
                                             );
                        }
            
                        qDebug() << _list->size();
            
                        QByteArray bytes;
                        QDataStream stream(&bytes, QIODevice::WriteOnly);
                        stream << sSelecAllContacts << _list;
                        globalall->modify(bytes);
                        socket->write(bytes);
                    }
            

            I have just one pointer now?

            Do what you want.

            K 1 Reply Last reply 19 Oct 2017, 10:59
            0
            • T Taz742
              19 Oct 2017, 09:52

              @VRonin Do you mean this?

                     if (query.exec()) {
                          QList<Contact> *_list = new QList<Contact>();
                          while (query.next()) {
                              _list->push_back(Contact(query.value("UID").toInt(),
                                                       query.value("USERNAME").toString(),
                                                       query.value("MOBILE").toString(),
                                                       )
                                               );
                          }
              
                          qDebug() << _list->size();
              
                          QByteArray bytes;
                          QDataStream stream(&bytes, QIODevice::WriteOnly);
                          stream << sSelecAllContacts << _list;
                          globalall->modify(bytes);
                          socket->write(bytes);
                      }
              

              I have just one pointer now?

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 19 Oct 2017, 10:59 last edited by
              #10

              @Taz742 said in QDataStream serialze & deserialize 'myclass' pointer:

              @VRonin Do you mean this?

              No. He means this:

              QList<Contact> _list;
              while (query.next()) {
                  _list.push_back(Contact(query.value("UID").toInt(),
                      query.value("USERNAME").toString(),
                      query.value("MOBILE").toString(),
                  ));
              }
              

              There is no gain at all in creating a Qt container in the heap.

              Read and abide by the Qt Code of Conduct

              T 1 Reply Last reply 19 Oct 2017, 11:13
              1
              • K kshegunov
                19 Oct 2017, 10:59

                @Taz742 said in QDataStream serialze & deserialize 'myclass' pointer:

                @VRonin Do you mean this?

                No. He means this:

                QList<Contact> _list;
                while (query.next()) {
                    _list.push_back(Contact(query.value("UID").toInt(),
                        query.value("USERNAME").toString(),
                        query.value("MOBILE").toString(),
                    ));
                }
                

                There is no gain at all in creating a Qt container in the heap.

                T Offline
                T Offline
                Taz742
                wrote on 19 Oct 2017, 11:13 last edited by Taz742
                #11

                @kshegunov
                Well, let's consider this case.
                What I wrote this is the server side.

                When I get it to the client side, if I get the same kind of list and get the value from it.
                for example:

                    QList<faf> list;
                
                    list.push_back(faf(2, 13));
                    list.push_back(faf(15, 23));
                    list.push_back(faf(30, 46));
                
                    faf test = list[0];
                    test.x = test.x * 2;
                    test.y = test.y * 2;
                
                    for (int i = 0; i < list.size(); i++) {
                        qDebug() << list[i].x << list[i].y; //Prints the same values
                    }
                
                
                

                If I want the object to change in the list.
                Is it right?

                    QList<faf> list;
                
                    list.push_back(faf(2, 13));
                    list.push_back(faf(15, 23));
                    list.push_back(faf(30, 46));
                
                    faf *test = &list[0];
                    test->x = test->x * 2;
                    test->y = test->y * 2;
                
                    for (int i = 0; i < list.size(); i++) {
                        qDebug() << list[i].x << list[i].y; //list[0].x = 4 and list[0].y = 46;
                    }
                

                Do what you want.

                K 1 Reply Last reply 19 Oct 2017, 12:44
                0
                • T Taz742
                  19 Oct 2017, 11:13

                  @kshegunov
                  Well, let's consider this case.
                  What I wrote this is the server side.

                  When I get it to the client side, if I get the same kind of list and get the value from it.
                  for example:

                      QList<faf> list;
                  
                      list.push_back(faf(2, 13));
                      list.push_back(faf(15, 23));
                      list.push_back(faf(30, 46));
                  
                      faf test = list[0];
                      test.x = test.x * 2;
                      test.y = test.y * 2;
                  
                      for (int i = 0; i < list.size(); i++) {
                          qDebug() << list[i].x << list[i].y; //Prints the same values
                      }
                  
                  
                  

                  If I want the object to change in the list.
                  Is it right?

                      QList<faf> list;
                  
                      list.push_back(faf(2, 13));
                      list.push_back(faf(15, 23));
                      list.push_back(faf(30, 46));
                  
                      faf *test = &list[0];
                      test->x = test->x * 2;
                      test->y = test->y * 2;
                  
                      for (int i = 0; i < list.size(); i++) {
                          qDebug() << list[i].x << list[i].y; //list[0].x = 4 and list[0].y = 46;
                      }
                  
                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 19 Oct 2017, 12:44 last edited by
                  #12

                  @Taz742 said in QDataStream serialze & deserialize 'myclass' pointer:

                  // Prints the same values
                  

                  Of course it does, you're copying the object that's stored in the list and modifying the copy.

                  If I want the object to change in the list.

                  Why convert to pointers? Just use references:

                  faf & test = list[0];
                  test.x *= 2;
                  test.y *= 2;
                  

                  Read and abide by the Qt Code of Conduct

                  T 1 Reply Last reply 19 Oct 2017, 13:02
                  2
                  • K kshegunov
                    19 Oct 2017, 12:44

                    @Taz742 said in QDataStream serialze & deserialize 'myclass' pointer:

                    // Prints the same values
                    

                    Of course it does, you're copying the object that's stored in the list and modifying the copy.

                    If I want the object to change in the list.

                    Why convert to pointers? Just use references:

                    faf & test = list[0];
                    test.x *= 2;
                    test.y *= 2;
                    
                    T Offline
                    T Offline
                    Taz742
                    wrote on 19 Oct 2017, 13:02 last edited by Taz742
                    #13

                    @kshegunov good :))

                    Now there are new problems.
                    Recently everything was good.
                    I do not know what happened to me or why.

                    alt text

                    C:\Users\User\Desktop\tazsmsorigin\enumeration.h:69: error: no match for 'operator>>' (operand types are 'QDataStream' and 'qint32 {aka int}')
                         stream >> tempint; _class.UID = tempint;
                                ^
                    C:\Users\User\Desktop\tazsmsorigin\enumeration.h:50: error: ambiguous overload for 'operator<<' (operand types are 'QDataStream' and 'int')
                         stream  << static_cast<qint32>(_class.UID)
                                 ^
                    C:\Users\User\Desktop\tazsmsorigin\enumeration.h:46: error: no match for 'operator>>' (operand types are 'QDataStream' and 'bool')
                         return stream >> _class.TChecked >> _class.Message;
                                       ^
                    

                    Do what you want.

                    K 1 Reply Last reply 19 Oct 2017, 16:03
                    0
                    • T Taz742
                      19 Oct 2017, 13:02

                      @kshegunov good :))

                      Now there are new problems.
                      Recently everything was good.
                      I do not know what happened to me or why.

                      alt text

                      C:\Users\User\Desktop\tazsmsorigin\enumeration.h:69: error: no match for 'operator>>' (operand types are 'QDataStream' and 'qint32 {aka int}')
                           stream >> tempint; _class.UID = tempint;
                                  ^
                      C:\Users\User\Desktop\tazsmsorigin\enumeration.h:50: error: ambiguous overload for 'operator<<' (operand types are 'QDataStream' and 'int')
                           stream  << static_cast<qint32>(_class.UID)
                                   ^
                      C:\Users\User\Desktop\tazsmsorigin\enumeration.h:46: error: no match for 'operator>>' (operand types are 'QDataStream' and 'bool')
                           return stream >> _class.TChecked >> _class.Message;
                                         ^
                      
                      K Offline
                      K Offline
                      kshegunov
                      Moderators
                      wrote on 19 Oct 2017, 16:03 last edited by
                      #14

                      Did you forget to include the <QDataStream> header?

                      Read and abide by the Qt Code of Conduct

                      T 1 Reply Last reply 19 Oct 2017, 16:05
                      1
                      • K kshegunov
                        19 Oct 2017, 16:03

                        Did you forget to include the <QDataStream> header?

                        T Offline
                        T Offline
                        Taz742
                        wrote on 19 Oct 2017, 16:05 last edited by
                        #15

                        @kshegunov no. I have

                        Do what you want.

                        K 1 Reply Last reply 19 Oct 2017, 16:12
                        0
                        • T Taz742
                          19 Oct 2017, 16:05

                          @kshegunov no. I have

                          K Offline
                          K Offline
                          kshegunov
                          Moderators
                          wrote on 19 Oct 2017, 16:12 last edited by
                          #16

                          Then you need to follow the compiler errors, it has given you exact directions where the errors are:
                          C:\Users\User\Desktop\tazsmsorigin\enumeration.h @ line 69

                          error: no match for 'operator>>' (operand types are 'QDataStream' and 'qint32 {aka int}')

                          I'm pretty sure this comes from not including the header. Also there's something amiss, it should be QDataStream & not QDataStream. Please post what's on line 69 in enumeration.h.

                          Read and abide by the Qt Code of Conduct

                          T 1 Reply Last reply 19 Oct 2017, 19:05
                          2
                          • K kshegunov
                            19 Oct 2017, 16:12

                            Then you need to follow the compiler errors, it has given you exact directions where the errors are:
                            C:\Users\User\Desktop\tazsmsorigin\enumeration.h @ line 69

                            error: no match for 'operator>>' (operand types are 'QDataStream' and 'qint32 {aka int}')

                            I'm pretty sure this comes from not including the header. Also there's something amiss, it should be QDataStream & not QDataStream. Please post what's on line 69 in enumeration.h.

                            T Offline
                            T Offline
                            Taz742
                            wrote on 19 Oct 2017, 19:05 last edited by Taz742
                            #17

                            @kshegunov
                            Yes the library was nat included, but I had it in globaldefines.h file.
                            He has caused a redifination proxy, QDataStream also has been described in MySocket.h file.

                            Do what you want.

                            K 1 Reply Last reply 19 Oct 2017, 20:51
                            0
                            • T Taz742
                              19 Oct 2017, 19:05

                              @kshegunov
                              Yes the library was nat included, but I had it in globaldefines.h file.
                              He has caused a redifination proxy, QDataStream also has been described in MySocket.h file.

                              K Offline
                              K Offline
                              kshegunov
                              Moderators
                              wrote on 19 Oct 2017, 20:51 last edited by
                              #18

                              @Taz742 said in QDataStream serialze & deserialize 'myclass' pointer:

                              He has caused a redifination proxy, QDataStream also has been described in MySocket.h file.

                              I don't follow.

                              Read and abide by the Qt Code of Conduct

                              T 2 Replies Last reply 20 Oct 2017, 06:00
                              0
                              • K kshegunov
                                19 Oct 2017, 20:51

                                @Taz742 said in QDataStream serialze & deserialize 'myclass' pointer:

                                He has caused a redifination proxy, QDataStream also has been described in MySocket.h file.

                                I don't follow.

                                T Offline
                                T Offline
                                Taz742
                                wrote on 20 Oct 2017, 06:00 last edited by
                                #19

                                @kshegunov I had code again mysocket.h, I taught at different places how to deal with my classes.

                                Do what you want.

                                1 Reply Last reply
                                0
                                • K kshegunov
                                  19 Oct 2017, 20:51

                                  @Taz742 said in QDataStream serialze & deserialize 'myclass' pointer:

                                  He has caused a redifination proxy, QDataStream also has been described in MySocket.h file.

                                  I don't follow.

                                  T Offline
                                  T Offline
                                  Taz742
                                  wrote on 20 Oct 2017, 06:40 last edited by Taz742
                                  #20

                                  @kshegunov @VRonin
                                  How to handle QDataStream to my classes, this are stored in one 'header' file.
                                  It's uncomfortable for me because I have many issues.
                                  I learnt to my classes QDataStream >> and << operators.

                                  For Example:

                                  #ifndef POINT3D_H
                                  #define POINT3D_H
                                  
                                  #include "QObject"
                                  #include "QDataStream"
                                  
                                  class Point3D
                                  {
                                  public:
                                      Point3D();
                                      int x;
                                      int y;
                                      int z;
                                  
                                      Point3D(int _x, int _y, int _z) {
                                          this->x = _x;
                                          this->y = _y;
                                          this->z = _z;
                                      }
                                  
                                      friend QDataStream & operator << (QDataStream &stream, const Point3D &obj);
                                      friend QDataStream & operator >> (QDataStream &stream, Point3D &obj);
                                  };
                                  
                                  Q_DECLARE_TYPEINFO(Point3D, Q_MOVABLE_TYPE);
                                  
                                  #endif // POINT3D_H
                                  
                                  #include "point3d.h"
                                  
                                  Point3D::Point3D()
                                  {
                                  
                                  }
                                  
                                  QDataStream & operator << (QDataStream &stream, const Point3D &obj) {
                                      stream << static_cast<qint32>(obj.x) << static_cast<qint32>(obj.y) << static_cast<qint32>(obj.z);
                                      return stream;
                                  }
                                  
                                  QDataStream & operator >> (QDataStream &stream, Point3D &obj) {
                                      qint32 tempint;
                                      stream >> tempint; obj.x = static_cast<int>(tempint);
                                      stream >> tempint; obj.y = static_cast<int>(tempint);
                                      stream >> tempint; obj.z = static_cast<int>(tempint);
                                  
                                      return stream;
                                  }
                                  

                                  I want to be sure that this will work from anywhere. Can you confirm it?

                                  Do what you want.

                                  K 1 Reply Last reply 20 Oct 2017, 10:49
                                  0
                                  • T Taz742
                                    20 Oct 2017, 06:40

                                    @kshegunov @VRonin
                                    How to handle QDataStream to my classes, this are stored in one 'header' file.
                                    It's uncomfortable for me because I have many issues.
                                    I learnt to my classes QDataStream >> and << operators.

                                    For Example:

                                    #ifndef POINT3D_H
                                    #define POINT3D_H
                                    
                                    #include "QObject"
                                    #include "QDataStream"
                                    
                                    class Point3D
                                    {
                                    public:
                                        Point3D();
                                        int x;
                                        int y;
                                        int z;
                                    
                                        Point3D(int _x, int _y, int _z) {
                                            this->x = _x;
                                            this->y = _y;
                                            this->z = _z;
                                        }
                                    
                                        friend QDataStream & operator << (QDataStream &stream, const Point3D &obj);
                                        friend QDataStream & operator >> (QDataStream &stream, Point3D &obj);
                                    };
                                    
                                    Q_DECLARE_TYPEINFO(Point3D, Q_MOVABLE_TYPE);
                                    
                                    #endif // POINT3D_H
                                    
                                    #include "point3d.h"
                                    
                                    Point3D::Point3D()
                                    {
                                    
                                    }
                                    
                                    QDataStream & operator << (QDataStream &stream, const Point3D &obj) {
                                        stream << static_cast<qint32>(obj.x) << static_cast<qint32>(obj.y) << static_cast<qint32>(obj.z);
                                        return stream;
                                    }
                                    
                                    QDataStream & operator >> (QDataStream &stream, Point3D &obj) {
                                        qint32 tempint;
                                        stream >> tempint; obj.x = static_cast<int>(tempint);
                                        stream >> tempint; obj.y = static_cast<int>(tempint);
                                        stream >> tempint; obj.z = static_cast<int>(tempint);
                                    
                                        return stream;
                                    }
                                    

                                    I want to be sure that this will work from anywhere. Can you confirm it?

                                    K Offline
                                    K Offline
                                    kshegunov
                                    Moderators
                                    wrote on 20 Oct 2017, 10:49 last edited by
                                    #21

                                    @Taz742 said in QDataStream serialze & deserialize 'myclass' pointer:

                                    I want to be sure that this will work from anywhere. Can you confirm it?

                                    I don't understand the question, could you elaborate.

                                    Read and abide by the Qt Code of Conduct

                                    T 1 Reply Last reply 20 Oct 2017, 11:13
                                    0
                                    • K kshegunov
                                      20 Oct 2017, 10:49

                                      @Taz742 said in QDataStream serialze & deserialize 'myclass' pointer:

                                      I want to be sure that this will work from anywhere. Can you confirm it?

                                      I don't understand the question, could you elaborate.

                                      T Offline
                                      T Offline
                                      Taz742
                                      wrote on 20 Oct 2017, 11:13 last edited by
                                      #22

                                      @kshegunov said in QDataStream serialze & deserialize 'myclass' pointer:

                                      I don't understand the question, could you elaborate.

                                      I have class Point3D which know how to behave when datastream requires input(<<) or output(>>) operators.
                                      I want to know if it will works from anyclasses, for example: from mainwindows and etc..

                                      Do what you want.

                                      K 1 Reply Last reply 20 Oct 2017, 11:15
                                      0
                                      • T Taz742
                                        20 Oct 2017, 11:13

                                        @kshegunov said in QDataStream serialze & deserialize 'myclass' pointer:

                                        I don't understand the question, could you elaborate.

                                        I have class Point3D which know how to behave when datastream requires input(<<) or output(>>) operators.
                                        I want to know if it will works from anyclasses, for example: from mainwindows and etc..

                                        K Offline
                                        K Offline
                                        kshegunov
                                        Moderators
                                        wrote on 20 Oct 2017, 11:15 last edited by
                                        #23

                                        Why wouldn't it? As long as the functions have declarations at the point of usage it's the linker's problem to tie it all together ... it's the singular purpose for which the linker exists actually.

                                        Read and abide by the Qt Code of Conduct

                                        T 1 Reply Last reply 20 Oct 2017, 11:17
                                        3
                                        • K kshegunov
                                          20 Oct 2017, 11:15

                                          Why wouldn't it? As long as the functions have declarations at the point of usage it's the linker's problem to tie it all together ... it's the singular purpose for which the linker exists actually.

                                          T Offline
                                          T Offline
                                          Taz742
                                          wrote on 20 Oct 2017, 11:17 last edited by
                                          #24

                                          @kshegunov Now I'm sure. thanks.

                                          Do what you want.

                                          1 Reply Last reply
                                          0

                                          14/24

                                          19 Oct 2017, 16:03

                                          • Login

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