QBitArray size stays zero after trying to fill it with stream operator

StackOverflow https://stackoverflow.com/questions/16944617

  •  31-05-2022
  •  | 
  •  

سؤال

Why does the size of QBitArray stay zero? I am using Qt 5.0.2.

QByteArray bytes(4,'b'); // four bytes
QBitArray bits;
QDataStream stream(&bytes, QIODevice::ReadWrite);

stream >> bits;

qDebug() << bytes.size() << bits.size();
هل كانت مفيدة؟

المحلول

When reading from a QDataStream it expects the data to be in a certain format, as described at http://qt-project.org/doc/qt-5.0/qtcore/datastreamformat.html. In the case of streaming QBitArray this is:

The array size (quint32)
The array bits, i.e. (size + 7)/8 bytes

If this is not the case the operation will fail - you can check QDataStream::status() to detect errors.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top