Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QT QString from QDataStream

Tags:

qt

I'm working with a buffer and I'm trying to get a string from it, but isnt working...

Example:

*void myFunc(QDataStream& in)
{
    quint8 v;
    in >> v;
    // Ok, I caught v value successfuly
    QString s;
    in >> s;
    // Didnt work :<
}*

The string lenght is stored on 2 first bytes...

Thanks

like image 515
Kraup Kywpz Avatar asked Apr 28 '26 15:04

Kraup Kywpz


1 Answers

If the string was not written as a QString, you need to read its length and content separately.

quint8 v;
in >> v;

quint16 length = 0;
in >> length;

// the string is probably utf8 or latin
QByteArray buffer(length, Qt::Uninitialized);

in.readRawData(buffer.data(), length); 
QString string(buffer);

You might have to change the endianness of the QDataStream with QDataStream::setByteOrder before reading the 16-bit length.

like image 113
alexisdm Avatar answered May 01 '26 09:05

alexisdm