QVariant to QBitArray
-
After next code:
@QHashIterator<QString, QVariant> iter(props);while(iter.hasNext()) { iter.next(); QString property = iter.key(); if (property.startsWith("mus")) { int num = QString("%1").arg(property.remove("mus")).toInt(); QBitArray bitValue = iter.value().toBitArray(); m_address = sender; m_socket->bind(m_address, 0x6358); connect(m_socket, SIGNAL(readyRead()), this, SLOT(onReadyRead())); if (num > 0 && num <= m_musList.count()) { MusWidget * musWg = m_musList.at(num-1); if (musWg) musWg->changeState(bitValue); } } }@
I've got an empty bit array with zero size in bitValue variable. But if I look debug information of my IDE I can see that iter.value() contains some data in format of bits like "11111". So question how can I convert QVariant value to QBitArray value ?
-
Hi,
I am not an expert in this area, but maybe this will help
-
did you try to check what type is stored by
"QVariant:Type":http://qt-project.org/doc/qt-4.8/qvariant.html#type -
and if type is different maybe you could cast your Variant to desired type with "QVarian:convert":http://qt-project.org/doc/qt-4.8/qvariant.html#convert
I am not sure if Qt cast Variant to BitArray automatically.
BR,
-
-
QVariant::toBitArray() is correct way - given the QVariant really contains a QBitArray object.
Are you sure you have a QBitArray inside your QVariant? Maybe it's a QByteArray or something different.
In any case, you should check QVariant::typeName() to see what it actually is...
-
It's a string value inserted inside which now stored in QHash iter.value(). So i need to convert from string but now it's type is QVariant. Something like this.
-
Do I understand correctly that you are trying to achieve such conversion
String -> QVariant -> QBitArrayAFAIK you will be not able to make such conversion.
-
I mean I insert string value into hash QHash<QString,QVariant>, but this string has format of bits "1111" or "1010" or any other binary value. After that I need to read value from hash and thats why I get QVarant value and after that I need to convert this QVariant value tu QBitArray.
-
Well, if you have those bits in String then maybe better solution is to convert String to unsigned int and then with bitwise operators extract bits values.
-
I can convert to int: @iter.value().toString().toInt(NULL,2)@ but how now I can insert integer value in QBitArray ?
-
The snippet below shows how you can convert some integer value to bitArray. It is small main so you can run it and test.
@
#include <QBitArray>
#include <QDebug>int main(int argc, char *argv[])
{
int someVal = 10; //value to convert
QBitArray bitArray(sizeof(int)*8);
long long refVal = 1;//check in loop which bit is set
for ( int i = 0; i < static_cast<int>(sizeof(int)*8); i++)
{
qDebug() << "i" << i << "refVal" << refVal;if (false != (someVal & refVal)) { bitArray.setBit(i, true); } refVal <<= 1; }
//display bit array
for ( int i = 0; i < bitArray.size(); i++)
{
qDebug() << "Bit at i" << i << " value = "<< bitArray.at(i);
}return 0;
@
Hope it will help