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. Overloading QDataStream operators
QtWS25 Last Chance

Overloading QDataStream operators

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 1.5k 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.
  • U Offline
    U Offline
    Uday More
    wrote on last edited by VRonin
    #1

    I am overloading QDataStream operator with my custom class.

    #include <QCoreApplication>
    #include <QDataStream>
    #include<QVector>
    
    class MsgPointAttribute{
    public:
        double pointValue;
    };
    
    class MsgPoint{
    public:
        QVector<MsgPointAttribute> pointAtributes;
        // Additional info like point color can be appended.
    };
    
    class MsgPointCloud{
    public:
        QString pointType;
        quint32 cloudWidth;
        quint32 cloudHeight;
        quint32 pointsCount;
        QVector<MsgPoint> points;
        // Additional info like cloud properties can be added here can be added here
    };
    
    class MsgPointCloudDataResponse{
    
    private :
    
    public:
    
        quint64 recievedTimestamp;
        QString responseChannelName;
        QString responseDeviceName;
        QString cloudName;
        QVector<MsgPointCloud> pointClouds;
    
    
    //    friend QDataStream &operator <<(QDataStream &out, const MsgPointCloudDataResponse &other);
    
    };
    
    // you need this if you want to use your type with QVariant
    Q_DECLARE_METATYPE(MsgPointCloudDataResponse)
    
    QDataStream &operator <<(QDataStream &out, const MsgPointCloudDataResponse &other){
    
    //    // Register your custom type with Stream operators.
    //    qRegisterMetaTypeStreamOperators<qreal>("qreal");
    
        int pointCloudsCount = other.pointClouds.count();
    
        // Looping through the list of point clouds.
        for(int i = 0; i < pointCloudsCount; ++i)
        {
            out << other.pointClouds[i].pointType << other.pointClouds[i].cloudWidth <<
                   other.pointClouds[i].cloudHeight << other.pointClouds[i].pointsCount;
    
            int pointCount = other.pointClouds[i].pointsCount;
    
            for(int j = 0; j < pointCount; ++j)
            {
                int  iPointAttrCount = other.pointClouds[i].points[j].pointAtributes.count();
    
                for(int k = 0; k < iPointAttrCount; ++k){
                    out << other.pointClouds[i].points[j].pointAtributes[k].pointValue;
                }
            }
        }
    
        return out;
    }
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        // Register your custom type with Stream operators.
        qRegisterMetaTypeStreamOperators<MsgPointCloudDataResponse>("MsgPointCloudDataResponse");
    
        return a.exec();
    }
    

    I get the following error and what am I missing.

    C:\Qt\Qt5.7.0\5.7\msvc2015_64\include\QtCore/qmetatype.h(777): error C2679: binary '>>': no operator found which takes a right-hand operand of type 'MsgPointCloudDataResponse' (or there is no acceptable conversion)

    J.HilkJ 1 Reply Last reply
    0
    • U Uday More

      I am overloading QDataStream operator with my custom class.

      #include <QCoreApplication>
      #include <QDataStream>
      #include<QVector>
      
      class MsgPointAttribute{
      public:
          double pointValue;
      };
      
      class MsgPoint{
      public:
          QVector<MsgPointAttribute> pointAtributes;
          // Additional info like point color can be appended.
      };
      
      class MsgPointCloud{
      public:
          QString pointType;
          quint32 cloudWidth;
          quint32 cloudHeight;
          quint32 pointsCount;
          QVector<MsgPoint> points;
          // Additional info like cloud properties can be added here can be added here
      };
      
      class MsgPointCloudDataResponse{
      
      private :
      
      public:
      
          quint64 recievedTimestamp;
          QString responseChannelName;
          QString responseDeviceName;
          QString cloudName;
          QVector<MsgPointCloud> pointClouds;
      
      
      //    friend QDataStream &operator <<(QDataStream &out, const MsgPointCloudDataResponse &other);
      
      };
      
      // you need this if you want to use your type with QVariant
      Q_DECLARE_METATYPE(MsgPointCloudDataResponse)
      
      QDataStream &operator <<(QDataStream &out, const MsgPointCloudDataResponse &other){
      
      //    // Register your custom type with Stream operators.
      //    qRegisterMetaTypeStreamOperators<qreal>("qreal");
      
          int pointCloudsCount = other.pointClouds.count();
      
          // Looping through the list of point clouds.
          for(int i = 0; i < pointCloudsCount; ++i)
          {
              out << other.pointClouds[i].pointType << other.pointClouds[i].cloudWidth <<
                     other.pointClouds[i].cloudHeight << other.pointClouds[i].pointsCount;
      
              int pointCount = other.pointClouds[i].pointsCount;
      
              for(int j = 0; j < pointCount; ++j)
              {
                  int  iPointAttrCount = other.pointClouds[i].points[j].pointAtributes.count();
      
                  for(int k = 0; k < iPointAttrCount; ++k){
                      out << other.pointClouds[i].points[j].pointAtributes[k].pointValue;
                  }
              }
          }
      
          return out;
      }
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          // Register your custom type with Stream operators.
          qRegisterMetaTypeStreamOperators<MsgPointCloudDataResponse>("MsgPointCloudDataResponse");
      
          return a.exec();
      }
      

      I get the following error and what am I missing.

      C:\Qt\Qt5.7.0\5.7\msvc2015_64\include\QtCore/qmetatype.h(777): error C2679: binary '>>': no operator found which takes a right-hand operand of type 'MsgPointCloudDataResponse' (or there is no acceptable conversion)

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Uday-More

      I havn't done that myself yet, but it seems like,

      qRegisterMetaTypeStreamOperators requieres you to define/overload << as well as >> to also get the data back !?

      and you only defined << in the code you showed.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      U 1 Reply Last reply
      2
      • J.HilkJ J.Hilk

        @Uday-More

        I havn't done that myself yet, but it seems like,

        qRegisterMetaTypeStreamOperators requieres you to define/overload << as well as >> to also get the data back !?

        and you only defined << in the code you showed.

        U Offline
        U Offline
        Uday More
        wrote on last edited by
        #3

        @J.Hilk Thanks so much. It solved the above problem.
        But now eventually i get another error as described below

        moc_appcontroller.obj : error LNK2005: "class QDataStream & __cdecl operator<<(class QDataStream &,class MsgPointCloudDataResponse const &)" (??6@YAAEAVQDataStream@@AEAV0@AEBVMsgPointCloudDataResponse@@@Z) already defined in appcontroller.obj
        moc_appcontroller.obj : error LNK2005: "class QDataStream & __cdecl operator>>(class QDataStream &,class MsgPointCloudDataResponse &)" (??5@YAAEAVQDataStream@@AEAV0@AEAVMsgPointCloudDataResponse@@@Z) already defined in appcontroller.obj

        Note : In appcontroller class I've just used the statement
        qRegisterMetaTypeStreamOperators<MsgPointCloudDataResponse>("MsgPointCloudDataResponse");

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          Move the implementation of the datastream operators to a .cpp file rather than having it in the header

          "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

          U 1 Reply Last reply
          0
          • VRoninV VRonin

            Move the implementation of the datastream operators to a .cpp file rather than having it in the header

            U Offline
            U Offline
            Uday More
            wrote on last edited by
            #5

            @VRonin said in Overloading QDataStream operators:

            Move the implementation of the datastream operators to a .cpp file rather than having it in the header

            Thanks. This is the solution.

            1. Overload both operators
            2. Have the overloading code in a cpp file.
            1 Reply Last reply
            0
            • D Offline
              D Offline
              DungeonLords
              wrote on last edited by DungeonLords
              #6

              In my example I am overloading QDataStream operator with my custom struct.

              typedef struct my_struct{
                  my_struct(const quint32 my_i, const bool my_b):i{my_i}, b{my_b}{};
                  my_struct(){};
                  quint32 i{};
                  bool b{};
              }my_struct_t;
              
              QDataStream &operator<<(QDataStream &out, const my_struct_t& s){
                  out << s.i << s.b;
                  return out;
              }
              
              QDataStream &operator>>(QDataStream &in, my_struct_t& s){
                  in >> s.i >> s.b;
                  return in;
              }
              
              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