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.