Solved How to send from QTcpSocket QVector<MyClass> to QTcpServer and vice-versa?
-
I do not close this problem as SOLVED.
That's what I did for just one class.- How can I send MyClass list (QVector)?
-
@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? -
QByteArray bytes; QDataStream stream(&bytes, QIODevice::WriteOnly); QVector<MyClass> vector; // fill the vector... stream << vector;
-
@jsulm
and vice versa? -
@Taz742 same
-
@jsulm
please write... -
-
@jsulm
Thank You Very, very, very, very, very, very, very Much :))) -
@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);
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; }