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



  • 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>?


  • Moderators



  • @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;
    };
    


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

    • How can I send MyClass list (QVector)?

  • Moderators

    @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.



  • @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?


  • Moderators

    @Taz742

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


  • @jsulm
    and vice versa?


  • Moderators

    @Taz742 same



  • @jsulm
    please write...


  • Moderators

    @Taz742 Well

    QVector<MyClass> vector;
    stream >> vector;
    

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



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


  • Moderators

    @Taz742 No problem :-)



  • 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.


  • @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;
    }
    


  • 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;
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.