QBitArray instantiate with a sequence of bits
Unsolved
General and Desktop
-
Hello
I have a question. How to instantiate QBitArray with a sequence of bits?QBitArray data = QBitArray::fromBits("1110", 4); qInfo() << data[0]; qInfo() << data[1]; qInfo() << data[2]; qInfo() << data[3];
This prints "1000", obviously, perfect and logical.
It's not correct in my case though.Now how to do it so that it sets 1110 instead, so i don't have to do:
data[0] = 1; data[1] = 1; data[2] = 1; data[3] = 0;
which is stupid.
-
@roditu you don't initialiser list is not supported for QBitArray or for QByteArray
you can use the from bits function correctly:
const char *data = "\x7"; // 1110 in binary depending on endian QBitArray bits = QBitArray::fromBits(data, 4);
keep in mind, that data needs to live as long as bits!
or you could make a helper function :
QBitArray bitArrayFromList(std::initializer_list<bool> list) { QBitArray bits(list.size()); int i = 0; for (bool b : list) { bits[i] = b; ++i; } return bits; }
QBitArray bits = bitArrayFromList({true, true, true, false}); qDebug() << bits << bits[0] << bits[1] << bits[2] << bits[3]; //QBitArray(1110) true true true false