QDataStream to return QByteArray set
-
Is there a possibility to find which QByteArray is set to the QDataStream object?
This methodQDataStream::device()
returns the device set to it. But is there a way to return if the QDataStream object is connected to a QByteArray and finding out which QByteArray it is set to?
Thanks. -
I do not know why you would want to do this. Your code constructed the QDataStream with an associated QByteArray so I am not sure how you would not know which byte array that was.
Anyway, something like this might work:
QBuffer *internalBuffer = qobject_cast<QBuffer*>(stream.device()); if (internalBuffer) { QByteArray data = internalBuffer->buffer(); ... } else { // not an underlying QByteArray. Panic? }
-
@ChrisW67 The idea was that I want to write a function that needs the QDataStream and the size of the QByteArray that it is connected to. An example would be like this:
void sampleFn(QDataStream &ds, QByteArray &ba) { for (int i = 0; i < ba.size() ; i++) { qint8 int_8; ds >> int_8; qInfo() << int_8; } }
This is just a sample function. I just want to make sure that the user of this function does not give the wrong QByteArray here, so that I can take just one parameter
QDataStream &ds
and work with it. -
@Kevin470 said in QDataStream to return QByteArray set:
Is it possible to get the pointer or reference of the QByteArray corresponding to the QDataStream?
Why would someone need it? Why do you want to modify the data in a function in a function where it should not be done? What's the (strange) use case for this?
-
@Kevin470 That is what the code I gave you does. Not sure I understand the question (or, as @Christian-Ehrlicher and I have said, the original problem).
I just want to make sure that the user of this function does not give the wrong QByteArray here, so that I can take just one parameter QDataStream &ds and work with it.
You can derive the QByteArray, and hence its size, inside the function.
The function is misleading though; you cannot pass an arbitrary QDataStream and expect the function to work.This should fly and does not need to know the size or type of the underlying data source:
void sampleFn(QDataStream &ds) { while (!ds.atEnd()) { qint8 int_8; ds >> int_8; qInfo() << int_8; } }
Have you considered passing the function the QByteArray by const reference? If the stream in the array is complex you can still do something like this:
void sampleFn(const QByteArray &ba) { QDataStream ds(ba); while (!ds.atEnd()) { qint8 int_8; qint32 int_32; ds >> int_8 >> int_32; qInfo() << int_8 << int_32; } }
If you are using QDataStream just to read bytes from the buffer then you can do without QDataStream entirely.
-
@Christian-Ehrlicher @ChrisW67 I have not found a right solution to my problem yet.
My actual use case is to read and write strings to a QByteArray but without the extra information of the string size and everything else in it.
For example if I am to write aQString str = "Hello World"
to a QByteArray, I am trying to find a solution to write just the bytes of each character in the string in the QByteArray.
Which means, it must contain only48:65:6C:6C:6F:20:57:6F:72:6C:64
and not00:00:00:0B:48:65:6C:6C:6F:20:57:6F:72:6C:64
where0B
gives the size of the String.
Similarly, the other way around. To read each byte to a QString without the need of knowing the size. Because the size is already known during reading and writing.I was thinking of maybe manipulating the QByteArray so that the bytes containing the size of the QString is removed. But I know it is a very bad idea to do so. That is when I was asking if it is possible to know which QByteArray is corresponding with the QDataStream.
Is there a possibility or workarounds for my use case?
-
@Kevin470 said in QDataStream to return QByteArray set:
My actual use case is to read and write strings to a QByteArray but without the extra information of the string size and everything else in it.
Then you should not use QDataStream at all.
QString has some methods to convert a QByteArray to a QString (for example https://doc.qt.io/qt-6/qstring.html#fromUtf8-2).
It also has methods to convert a QString to a QByteArray: https://doc.qt.io/qt-6/qstring.html#toUtf8 -
@jsulm Therein lies my problem unfortunately. (The actual source of the bytearray that I receive is actually not written using the Qt Framework, and so it does not have any QString QByteArray concepts in it, so I have to read every byte separately in a uniform order and strings do not have their size mentioned before them)
My QByteArray consists of data from different datatypes.
For example in this instance:QByteArray ba; QDataStream write(&ba,QDataStream::WriteOnly); QDataStream read(&ba,QDataStream::ReadOnly); quint8 int8 = 14; quint16 int16 = 22; write << int8; write << int16; qInfo() << QString("Hello World").toUtf8().toHex(':'); write << QString("Hello World").toUtf8(); qInfo() << ba.toHex(':');
Here I expect
"0e:00:16:48:65:6c:6c:6f:20:57:6f:72:6c:64"
and not"0e:00:16:00:00:00:0b:48:65:6c:6c:6f:20:57:6f:72:6c:64"
I can't useQByteArray::append()
because the flexibility to choose the Endianness is lost I think. And anyway, append makes the quint16 to single byte if the number can be written in a single byte. -
I made a rudimentary solution for the problem.
//Read data to QString with a known length void readDataQString(QDataStream &ds, QString &str, uint length) { QByteArray buffer(length,Qt::Uninitialized); ds.readRawData(buffer.data(),length); str = QString(buffer); qInfo() << str; } //Write data from QString without length info void writeDataQString(QDataStream &ds, QString &str) { QByteArray ab = str.toUtf8(); for(int i = 0; i < ab.size(); ++i) { quint8 int_8 = static_cast<quint8>(ab.at(i)); ds << int_8; } QBuffer *internalBuffer = qobject_cast<QBuffer*>(ds.device()); QByteArray ba = internalBuffer->buffer(); qInfo() << ba.toHex(':'); }
I hope this works for everyone.