There was a problem with the transfer of the structure type below, and indeed with any structures. With other types, everything is fine: QStringList , QImage , QString , no problem, the server sends, and the client receives them.
Are there any options how to transfer the structure in the most reasonable way?

 struct CommandStruct { uint id; QString text_cmd; QString text_btn; CommandStruct(uint _id, QString _text_cmd, QString _text_btn) { id = _id; text_cmd = _text_cmd; text_btn = _text_btn; } CommandStruct(){} }; 

    2 answers 2

    To do this, override 2 statements for your structure:

     QDataStream & operator<< (QDataStream& stream, const CommandStruct& command); QDataStream & operator>> (QDataStream& stream, CommandStruct& command); 

    For Q * classes, everything works for you, because for each of them there is such an overload with QDataStream

    • Thank you, I will do. - Disastricks

    To transfer arbitrary data, you need to decide on the application-level protocol - that is, the format in which your structure will be stored in a packet. There are cases when you can use your own protocols, and there are those when you can use ready ones. Among the finished ones, you can select the JSON format, and Qt has special classes for working with this format. In this format, you can transfer data of absolutely any structure, dimension and type.

    In case the volume of transmitted data is critical, it is better to make your own protocol, specially adapted for your task. But this approach has a serious disadvantage - it will be very difficult to scale the created solution, for example, if you need to transfer some more data besides your structure. In your case, the solution may be, for example: 4 bytes - identifier, 4 bytes - the length of the first line, the first line of variable length, 4 bytes - the length of the second line, the second line. Sample implementation:

     QByteArray packet; QBuffer packBuffer(packet); QDataStream packStream(packBuffer); packStream.setByteOrder(QDataStream::BigEndian); packStream << id; packStream << text_cmd.size(); packStream.writeBytes(text_cmd.toLatin1().data(), text_cmd.size()); 

    In order not to write all the structure fields into a package each time, you can, as @ixSci already noted, reload the << operator at your structure.

    • Thank you, I will do. - Disastricks