How to send from QTcpSocket QVector<MyClass> to QTcpServer and vice-versa?
Solved
General and Desktop
-
@Taz742 Take a look at http://doc.qt.io/qt-5/qdatastream.html
-
@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; };
-
@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.
-
-
Little side notes:
- you'll need to implement the stream operators for MyClass for it to work:
QDataStream &operator<<(QDataStream& destination, const MyClass& source);
andQDataStream &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.
- you'll need to implement the stream operators for MyClass for it to work:
-
@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; }