Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to send from QTcpSocket QVector<MyClass> to QTcpServer and vice-versa?
Qt 6.11 is out! See what's new in the release blog

How to send from QTcpSocket QVector<MyClass> to QTcpServer and vice-versa?

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 3 Posters 7.6k 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.
  • Taz742T Offline
    Taz742T Offline
    Taz742
    wrote on last edited by Taz742
    #1

    Hello guys.
    How to send QVector<MyClass> to my server and vice-versa?
    How to cast QVector<MyClass> to QByteArray and vice-versa QByteArray to QVector<MyClass>?

    Do what you want.

    jsulmJ 1 Reply Last reply
    0
    • Taz742T Taz742

      Hello guys.
      How to send QVector<MyClass> to my server and vice-versa?
      How to cast QVector<MyClass> to QByteArray and vice-versa QByteArray to QVector<MyClass>?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Taz742 Take a look at http://doc.qt.io/qt-5/qdatastream.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Taz742T 1 Reply Last reply
      4
      • jsulmJ jsulm

        @Taz742 Take a look at http://doc.qt.io/qt-5/qdatastream.html

        Taz742T Offline
        Taz742T Offline
        Taz742
        wrote on last edited by Taz742
        #3

        @jsulm
        Thank.

        this issue is solved:

        client.cpp

        template<class T>
        T get (QDataStream &stream) {
            T value;
            stream >> value;
            return value;
        }
        
        QDataStream & operator << (QDataStream &stream, const MyClass &_class)
        {
            stream << (int)_class.Age << (int)_class.School << (QString)_class.Name << (QVector<QString>)_class.vctr;
            return stream;
        }
        
        QDataStream & operator >> (QDataStream &stream, MyClass &_class)
        {
            _class.Age = get<int>(stream);
            _class.School = get<int>(stream);
            _class.Name = get<QString>(stream);
            _class.vctr = get< QVector<QString> > (stream);
        
            return stream;
        }
        
        void Client::SendMessage(std::vector<QString>)
        {
            MyClass _Send;
                    _Send.Age = 22;
                    _Send.School = 14;
                    _Send.Name = "Taz";
        
            QVector<QString> vctrSend;
                             vctrSend.push_back("foo");
                             vctrSend.push_back("bar");
        
            _Send.vctr = vctrSend;
        
            QByteArray bytes;
            QDataStream stream(&bytes, QIODevice::WriteOnly);
            stream << _Send;
            socket->write(bytes);
        
        
            MyClass _Read;
            QDataStream read(&bytes, QIODevice::ReadOnly);
            read >> _Read;
        
            QVector<QString> vctrAns = _Read.vctr;
        
            qDebug() << _Read.Age << _Read.School << _Read.Name;
        
            for (auto&& value : vctrAns) {
                qDebug() << value;
            }
        }
        

        client.h

        struct MyClass {
            public:
                int Age;
                int School;
                QString Name;
                QVector<QString> vctr;
        };
        
        class Client : public QObject
        {
            Q_OBJECT
        public:
            explicit Client(QObject *parent = 0);
        
            void SendMessage(std::vector<QString> v);
        
            friend QDataStream & operator << (QDataStream &stream, const MyClass & _class);
        
            friend QDataStream & operator >> (QDataStream &stream, const MyClass & _class);
        
        signals:
            void setMessage(QString);
        
            void Connected();
        
            void Disconnected();
        
        public slots:
            void ImReady();
        
            void ImConnected();
        
            void ImDisconnected();
        
            void StateChanged(QAbstractSocket::SocketState State);
        
        private:
            QTcpSocket *socket;
        };
        

        Do what you want.

        Taz742T 1 Reply Last reply
        1
        • Taz742T Taz742

          @jsulm
          Thank.

          this issue is solved:

          client.cpp

          template<class T>
          T get (QDataStream &stream) {
              T value;
              stream >> value;
              return value;
          }
          
          QDataStream & operator << (QDataStream &stream, const MyClass &_class)
          {
              stream << (int)_class.Age << (int)_class.School << (QString)_class.Name << (QVector<QString>)_class.vctr;
              return stream;
          }
          
          QDataStream & operator >> (QDataStream &stream, MyClass &_class)
          {
              _class.Age = get<int>(stream);
              _class.School = get<int>(stream);
              _class.Name = get<QString>(stream);
              _class.vctr = get< QVector<QString> > (stream);
          
              return stream;
          }
          
          void Client::SendMessage(std::vector<QString>)
          {
              MyClass _Send;
                      _Send.Age = 22;
                      _Send.School = 14;
                      _Send.Name = "Taz";
          
              QVector<QString> vctrSend;
                               vctrSend.push_back("foo");
                               vctrSend.push_back("bar");
          
              _Send.vctr = vctrSend;
          
              QByteArray bytes;
              QDataStream stream(&bytes, QIODevice::WriteOnly);
              stream << _Send;
              socket->write(bytes);
          
          
              MyClass _Read;
              QDataStream read(&bytes, QIODevice::ReadOnly);
              read >> _Read;
          
              QVector<QString> vctrAns = _Read.vctr;
          
              qDebug() << _Read.Age << _Read.School << _Read.Name;
          
              for (auto&& value : vctrAns) {
                  qDebug() << value;
              }
          }
          

          client.h

          struct MyClass {
              public:
                  int Age;
                  int School;
                  QString Name;
                  QVector<QString> vctr;
          };
          
          class Client : public QObject
          {
              Q_OBJECT
          public:
              explicit Client(QObject *parent = 0);
          
              void SendMessage(std::vector<QString> v);
          
              friend QDataStream & operator << (QDataStream &stream, const MyClass & _class);
          
              friend QDataStream & operator >> (QDataStream &stream, const MyClass & _class);
          
          signals:
              void setMessage(QString);
          
              void Connected();
          
              void Disconnected();
          
          public slots:
              void ImReady();
          
              void ImConnected();
          
              void ImDisconnected();
          
              void StateChanged(QAbstractSocket::SocketState State);
          
          private:
              QTcpSocket *socket;
          };
          
          Taz742T Offline
          Taz742T Offline
          Taz742
          wrote on last edited by Taz742
          #4

          I do not close this problem as SOLVED.
          That's what I did for just one class.

          • How can I send MyClass list (QVector)?

          Do what you want.

          jsulmJ 1 Reply Last reply
          0
          • Taz742T Taz742

            I do not close this problem as SOLVED.
            That's what I did for just one class.

            • How can I send MyClass list (QVector)?
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Taz742 said in How to send from QTcpSocket QVector<MyClass> to QTcpServer and vice-versa?:

            How can I send MyClass list (QVector)?

            This should now work - Qt knows how to handle QVector.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            Taz742T 1 Reply Last reply
            2
            • jsulmJ jsulm

              @Taz742 said in How to send from QTcpSocket QVector<MyClass> to QTcpServer and vice-versa?:

              How can I send MyClass list (QVector)?

              This should now work - Qt knows how to handle QVector.

              Taz742T Offline
              Taz742T Offline
              Taz742
              wrote on last edited by
              #6

              @jsulm how ?
              I dont Understed...
              I want a simple way.
              How do I cast QVector<T> to QByteArray? And vice versa?
              I know programming is not everything easy but maybe help me how to transfer the list?

              Do what you want.

              jsulmJ 1 Reply Last reply
              0
              • Taz742T Taz742

                @jsulm how ?
                I dont Understed...
                I want a simple way.
                How do I cast QVector<T> to QByteArray? And vice versa?
                I know programming is not everything easy but maybe help me how to transfer the list?

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Taz742

                QByteArray bytes;
                QDataStream stream(&bytes, QIODevice::WriteOnly);
                QVector<MyClass> vector;
                // fill the vector...
                stream << vector;
                

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                Taz742T 1 Reply Last reply
                2
                • jsulmJ jsulm

                  @Taz742

                  QByteArray bytes;
                  QDataStream stream(&bytes, QIODevice::WriteOnly);
                  QVector<MyClass> vector;
                  // fill the vector...
                  stream << vector;
                  
                  Taz742T Offline
                  Taz742T Offline
                  Taz742
                  wrote on last edited by
                  #8

                  @jsulm
                  and vice versa?

                  Do what you want.

                  jsulmJ 1 Reply Last reply
                  0
                  • Taz742T Taz742

                    @jsulm
                    and vice versa?

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Taz742 same

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    Taz742T 1 Reply Last reply
                    2
                    • jsulmJ jsulm

                      @Taz742 same

                      Taz742T Offline
                      Taz742T Offline
                      Taz742
                      wrote on last edited by
                      #10

                      @jsulm
                      please write...

                      Do what you want.

                      jsulmJ 1 Reply Last reply
                      0
                      • Taz742T Taz742

                        @jsulm
                        please write...

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Taz742 Well

                        QVector<MyClass> vector;
                        stream >> vector;
                        

                        http://doc.qt.io/qt-5/qdatastream.html

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        Taz742T 1 Reply Last reply
                        3
                        • jsulmJ jsulm

                          @Taz742 Well

                          QVector<MyClass> vector;
                          stream >> vector;
                          

                          http://doc.qt.io/qt-5/qdatastream.html

                          Taz742T Offline
                          Taz742T Offline
                          Taz742
                          wrote on last edited by
                          #12

                          @jsulm
                          Thank You Very, very, very, very, very, very, very Much :)))

                          Do what you want.

                          jsulmJ 1 Reply Last reply
                          0
                          • Taz742T Taz742

                            @jsulm
                            Thank You Very, very, very, very, very, very, very Much :)))

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @Taz742 No problem :-)

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

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

                              Little side notes:

                              • you'll need to implement the stream operators for MyClass for it to work: QDataStream &operator<<(QDataStream& destination, const MyClass& source); and QDataStream &operator>>(QDataStream& source, MyClass& destination);
                              • C++ standard does not define the size of default types so while for the vast majority of systems int is 32 bits in some it might be 16 and some others even 64. So if you send an int into datastream from a system that uses 32 bit int to a system that uses 16 bit int they will fail to communicate.
                                When writing basic numeric types to datastream always remember to cast it to a fixed size type: qint32, qint16, qint8, etc.

                              "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

                              Taz742T 1 Reply Last reply
                              4
                              • VRoninV VRonin

                                Little side notes:

                                • you'll need to implement the stream operators for MyClass for it to work: QDataStream &operator<<(QDataStream& destination, const MyClass& source); and QDataStream &operator>>(QDataStream& source, MyClass& destination);
                                • C++ standard does not define the size of default types so while for the vast majority of systems int is 32 bits in some it might be 16 and some others even 64. So if you send an int into datastream from a system that uses 32 bit int to a system that uses 16 bit int they will fail to communicate.
                                  When writing basic numeric types to datastream always remember to cast it to a fixed size type: qint32, qint16, qint8, etc.
                                Taz742T Offline
                                Taz742T Offline
                                Taz742
                                wrote on last edited by Taz742
                                #15

                                @VRonin
                                ok thank..

                                QDataStream & operator << (QDataStream &stream, const MyClass &_class)
                                {
                                    stream << (qint32)_class.Age << (qint32)_class.School << (QString)_class.Name << (QVector<QString>)_class.vctr;
                                    return stream;
                                }
                                
                                QDataStream & operator >> (QDataStream &stream, MyClass &_class)
                                {
                                    _class.Age = get<qint32>(stream);
                                    _class.School = get<qint32>(stream);
                                    _class.Name = get<QString>(stream);
                                    _class.vctr = get< QVector<QString> > (stream);
                                
                                    return stream;
                                }
                                

                                Do what you want.

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

                                  C-cast always hurts the C++ programmer a bit.

                                  QDataStream& operator<< (QDataStream &stream, const MyClass &_class)
                                  {
                                      return stream << static_cast<qint32>(_class.Age) << static_cast<qint32>(_class.School) << _class.Name << _class.vctr;
                                  }
                                  QDataStream& operator>> (QDataStream &stream, MyClass &_class)
                                  {
                                      qint32 tempInt;
                                      stream >> tempInt; _class.Age=tempInt;
                                      stream >> tempInt; _class.School =tempInt;
                                      return stream >> _class.Name >> _class.vctr;
                                  }
                                  

                                  "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
                                  3

                                  • Login

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