QBitArray variantToBitArray(QVariant &variant, bool &ok) { ok = true; switch (variant.type()) { case (QVariant::Bool):{ bool v = variant.toBool(); return bitArrayFromType<bool>(v); } CASE_RETURN_VARIANT_TO_BIT_ARRAY(((QVariant::Type)QMetaType::UChar),toUInt,uchar); CASE_RETURN_VARIANT_TO_BIT_ARRAY(((QVariant::Type)QMetaType::Char),toInt,char); CASE_RETURN_VARIANT_TO_BIT_ARRAY(((QVariant::Type)QMetaType::UShort),toUInt,ushort); CASE_RETURN_VARIANT_TO_BIT_ARRAY(((QVariant::Type)QMetaType::Short),toInt,short); CASE_RETURN_VARIANT_TO_BIT_ARRAY((QVariant::UInt),toUInt,uint); CASE_RETURN_VARIANT_TO_BIT_ARRAY((QVariant::ULongLong),toULongLong,qulonglong); CASE_RETURN_VARIANT_TO_BIT_ARRAY((QVariant::Int),toInt,int); CASE_RETURN_VARIANT_TO_BIT_ARRAY((QVariant::LongLong),toLongLong,qlonglong); case (QVariant::BitArray):{ QBitArray v = variant.toBitArray(); return v; } default: ok = false; Q_ASSERT(ok); return QBitArray(); } }
void DashLabel::updateValue(QVariant value){ if(value.type() == QVariant::Double || value.type() == QVariant::String){ setText(value.toString()); }else if(value.type() == QVariant::BitArray){ QString text; QBitArray bit = value.toBitArray(); for(int i=0; i<bit.count(); i++){ text.append(bit[i]?'1':'0'); setText(text); } } }
bool GCF::Message::encodeVariant(QDataStream& ds, const QVariant& value) { QString typeName; if( value.isValid() ) { typeName = QString(value.typeName()); if( typeName.isEmpty() ) return false; } else typeName = "Invalid"; if( value.type() >= QVariant::UserType ) return false; // Prepare the typename in-case of bool, char, int, double switch(value.type()) { case QVariant::Int: typeName = is32Bit() ? "qint32" : "qint64"; break; case QVariant::Char: typeName = "quint8"; break; case QVariant::UInt: typeName = is32Bit() ? "quint32" : "quint64"; break; default: break; } // Serialize value on to the byte array switch( value.type() ) { case QVariant::Invalid: ds << typeName; ds << qint32(0); break; case QVariant::Bool: ds << typeName; ds << value.toBool(); break; case QVariant::Char: ds << typeName; ds << value.toChar(); break; case QVariant::Color: ds << typeName; ds << value.value<QColor>(); break; case QVariant::Date: ds << typeName; ds << value.toDate(); break; case QVariant::DateTime: ds << typeName; ds << value.toDateTime(); break; case QVariant::Double: ds << typeName; ds << value.toDouble(); break; case QVariant::Int: ds << typeName; if( is32Bit() ) ds << (qint32)value.toInt(); else ds << (qint64)value.toInt(); break; case QVariant::Time: ds << typeName; ds << value.toTime(); break; case QVariant::UInt: ds << typeName; if( is32Bit() ) ds << (quint32)value.toUInt(); else ds << (quint64)value.toUInt(); break; case QVariant::String: ds << typeName; ds << value.toString(); break; case QVariant::Pixmap: ds << typeName; ds << value.value<QPixmap>(); break; case QVariant::ByteArray: ds << typeName; ds << value.toByteArray(); break; case QVariant::BitArray: ds << typeName; ds << value.toBitArray(); break; case QVariant::Image: ds << typeName; ds << value.value<QImage>(); break; case QVariant::Url: ds << typeName; ds << value.toUrl(); break; case QVariant::StringList: ds << typeName; ds << value.toStringList(); break; case QVariant::SizePolicy: ds << typeName; ds << value.value<QSizePolicy>(); break; case QVariant::SizeF: ds << typeName; ds << value.toSizeF(); break; case QVariant::Size: ds << typeName; ds << value.toSize(); break; case QVariant::RegExp: ds << typeName; ds << value.toRegExp(); break; case QVariant::RectF: ds << typeName; ds << value.toRectF(); break; case QVariant::Rect: ds << typeName; ds << value.toRect(); break; case QVariant::Polygon: ds << typeName; ds << value.value<QPolygon>(); break; case QVariant::PointF: ds << typeName; ds << value.toPointF(); break; case QVariant::Point: ds << typeName; ds << value.toPoint(); break; case QVariant::Matrix: ds << typeName; ds << value.value<QMatrix>(); break; case QVariant::LineF: ds << typeName; ds << value.toLineF(); break; case QVariant::Line: ds << typeName; ds << value.toLine(); break; case QVariant::Brush: ds << typeName; ds << value.value<QBrush>(); break; case QVariant::Bitmap: ds << typeName; ds << value.value<QBitmap>(); break; case QVariant::Transform: ds << typeName; ds << value.value<QTransform>(); break; default: // Other types will be supported shortly. // TODO: support user defined types. return false; } return true; }