0

I would like to send via a QUdpSocket 2 or 3 QVector :

  • One QVector < bool >
  • One QVector < int >
  • One QVector < float >

Is it possible to do that, and if so how to do it ?

0xPunt
  • 313
  • 1
  • 4
  • 21
  • 1
    [Serialize it](https://en.wikipedia.org/wiki/Serialization). – user4581301 May 17 '16 at 07:11
  • I did somthing like this (just same) , you can send QVector too. [check this link](https://stackoverflow.com/questions/61538601/converting-qimage-to-qbytearray-using-qdatastream/62632893#62632893) – H.M Dec 16 '20 at 19:21

1 Answers1

3

From Qt documentation:

The Qt container classes can also be serialized to a QDataStream. These include QList, QLinkedList, QVector, QSet, QHash, and QMap. The stream operators are declared as non-members of the classes.

You can do it using 'QDataStream' like so:

QBuffer buffer;
buffer.open(QIODevice::ReadWrite);
QDataStream out(&buffer);
out.setVersion(QDataStream::Qt_5_6);
out << m_your_data;
out.device()->seek(0);
if(!write(buffer.data()))
{
    // handle your error
}
buffer.close();
albertTaberner
  • 1,969
  • 1
  • 26
  • 35
  • you can use [QByteArray](http://stackoverflow.com/questions/23271029/append-a-qbytearray-to-qdatastream) as buffer and then write out with [QUdpSocket::writeDatagram](http://www.qtforum.org/article/34385/qudpsocket-broadcast-example.html) it accepts QByteArray as input.. maybe it can be done with the QBuffer as well.. I am lazy to write my own answer.. – nayana May 17 '16 at 07:23