1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtCore module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include "qbitarray.h"
44 #include "qbytearray.h"
45 #include "qdatastream.h"
48 #include "qdatetime.h"
49 #include "qeasingcurve.h"
52 #include "qstringlist.h"
56 #include "private/qvariant_p.h"
57 #include "qmetatype_p.h"
59 #ifndef QT_NO_GEOM_VARIANT
80 static const QVariant::Handler *Handlers[QModulesPrivate::ModulesCount];
82 const QVariant::Handler *operator[] (const int typeId) const
84 return Handlers[QModulesPrivate::moduleForType(typeId)];
87 void registerHandler(const QModulesPrivate::Names name, const QVariant::Handler *handler)
89 Handlers[name] = handler;
92 inline void unregisterHandler(const QModulesPrivate::Names name);
98 struct TypeDefiniton {
99 static const bool IsAvailable = true;
102 // Ignore these types, as incomplete
103 #ifdef QT_BOOTSTRAPPED
104 template<> struct TypeDefiniton<QEasingCurve> { static const bool IsAvailable = false; };
106 #ifdef QT_NO_GEOM_VARIANT
107 template<> struct TypeDefiniton<QRect> { static const bool IsAvailable = false; };
108 template<> struct TypeDefiniton<QRectF> { static const bool IsAvailable = false; };
109 template<> struct TypeDefiniton<QSize> { static const bool IsAvailable = false; };
110 template<> struct TypeDefiniton<QSizeF> { static const bool IsAvailable = false; };
111 template<> struct TypeDefiniton<QLine> { static const bool IsAvailable = false; };
112 template<> struct TypeDefiniton<QLineF> { static const bool IsAvailable = false; };
113 template<> struct TypeDefiniton<QPoint> { static const bool IsAvailable = false; };
114 template<> struct TypeDefiniton<QPointF> { static const bool IsAvailable = false; };
117 struct CoreTypesFilter {
120 static const bool IsAccepted = QTypeModuleInfo<T>::IsCore && TypeDefiniton<T>::IsAvailable;
123 } // annonymous used to hide TypeDefiniton
125 namespace { // annonymous used to hide QVariant handlers
127 static void construct(QVariant::Private *x, const void *copy)
129 QVariantConstructor<CoreTypesFilter> constructor(x, copy);
130 QMetaTypeSwitcher::switcher<void>(constructor, x->type, 0);
133 static void clear(QVariant::Private *d)
135 QVariantDestructor<CoreTypesFilter> cleaner(d);
136 QMetaTypeSwitcher::switcher<void>(cleaner, d->type, 0);
139 static bool isNull(const QVariant::Private *d)
141 QVariantIsNull<CoreTypesFilter> isNull(d);
142 return QMetaTypeSwitcher::switcher<bool>(isNull, d->type, 0);
148 Compares \a a to \a b. The caller guarantees that \a a and \a b
149 are of the same type.
151 static bool compare(const QVariant::Private *a, const QVariant::Private *b)
153 QVariantComparator<CoreTypesFilter> comparator(a, b);
154 return QMetaTypeSwitcher::switcher<bool>(comparator, a->type, 0);
160 static qlonglong qMetaTypeNumber(const QVariant::Private *d)
165 case QMetaType::LongLong:
167 case QMetaType::Char:
168 return qlonglong(d->data.c);
169 case QMetaType::Short:
170 return qlonglong(d->data.s);
171 case QMetaType::Long:
172 return qlonglong(d->data.l);
173 case QMetaType::Float:
174 return qRound64(d->data.f);
175 case QVariant::Double:
176 return qRound64(d->data.d);
182 static qulonglong qMetaTypeUNumber(const QVariant::Private *d)
187 case QVariant::ULongLong:
189 case QMetaType::UChar:
191 case QMetaType::UShort:
193 case QMetaType::ULong:
200 static qlonglong qConvertToNumber(const QVariant::Private *d, bool *ok)
204 switch (uint(d->type)) {
205 case QVariant::String:
206 return v_cast<QString>(d)->toLongLong(ok);
208 return v_cast<QChar>(d)->unicode();
209 case QVariant::ByteArray:
210 return v_cast<QByteArray>(d)->toLongLong(ok);
212 return qlonglong(d->data.b);
213 case QVariant::Double:
215 case QMetaType::Char:
216 case QMetaType::Short:
217 case QMetaType::Long:
218 case QMetaType::Float:
219 case QMetaType::LongLong:
220 return qMetaTypeNumber(d);
221 case QVariant::ULongLong:
223 case QMetaType::UChar:
224 case QMetaType::UShort:
225 case QMetaType::ULong:
226 return qlonglong(qMetaTypeUNumber(d));
233 static qulonglong qConvertToUnsignedNumber(const QVariant::Private *d, bool *ok)
237 switch (uint(d->type)) {
238 case QVariant::String:
239 return v_cast<QString>(d)->toULongLong(ok);
241 return v_cast<QChar>(d)->unicode();
242 case QVariant::ByteArray:
243 return v_cast<QByteArray>(d)->toULongLong(ok);
245 return qulonglong(d->data.b);
246 case QVariant::Double:
248 case QMetaType::Char:
249 case QMetaType::Short:
250 case QMetaType::Long:
251 case QMetaType::Float:
252 case QMetaType::LongLong:
253 return qulonglong(qMetaTypeNumber(d));
254 case QVariant::ULongLong:
256 case QMetaType::UChar:
257 case QMetaType::UShort:
258 case QMetaType::ULong:
259 return qMetaTypeUNumber(d);
263 return Q_UINT64_C(0);
266 template<typename TInput, typename LiteralWrapper>
267 inline bool qt_convertToBool(const QVariant::Private *const d)
269 TInput str = v_cast<TInput>(d)->toLower();
270 return !(str == LiteralWrapper("0") || str == LiteralWrapper("false") || str.isEmpty());
276 Converts \a d to type \a t, which is placed in \a result.
278 static bool convert(const QVariant::Private *d, QVariant::Type t, void *result, bool *ok)
280 Q_ASSERT(d->type != uint(t));
290 case QVariant::String:
291 *static_cast<QUrl *>(result) = QUrl(*v_cast<QString>(d));
297 case QVariant::String: {
298 QString *str = static_cast<QString *>(result);
301 *str = QString(*v_cast<QChar>(d));
303 case QMetaType::Char:
304 case QMetaType::UChar:
305 *str = QChar::fromAscii(d->data.c);
307 case QMetaType::Short:
308 case QMetaType::Long:
310 case QVariant::LongLong:
311 *str = QString::number(qMetaTypeNumber(d));
314 case QVariant::ULongLong:
315 case QMetaType::UShort:
316 case QMetaType::ULong:
317 *str = QString::number(qMetaTypeUNumber(d));
319 case QMetaType::Float:
320 *str = QString::number(d->data.f, 'g', FLT_DIG);
322 case QVariant::Double:
323 *str = QString::number(d->data.d, 'g', DBL_DIG);
325 #if !defined(QT_NO_DATESTRING)
327 *str = v_cast<QDate>(d)->toString(Qt::ISODate);
330 *str = v_cast<QTime>(d)->toString(Qt::ISODate);
332 case QVariant::DateTime:
333 *str = v_cast<QDateTime>(d)->toString(Qt::ISODate);
337 *str = QLatin1String(d->data.b ? "true" : "false");
339 case QVariant::ByteArray:
340 *str = QString::fromAscii(v_cast<QByteArray>(d)->constData());
342 case QVariant::StringList:
343 if (v_cast<QStringList>(d)->count() == 1)
344 *str = v_cast<QStringList>(d)->at(0);
347 *str = v_cast<QUrl>(d)->toString();
350 *str = v_cast<QUuid>(d)->toString();
357 case QVariant::Char: {
358 QChar *c = static_cast<QChar *>(result);
361 case QVariant::LongLong:
362 case QMetaType::Char:
363 case QMetaType::Short:
364 case QMetaType::Long:
365 case QMetaType::Float:
366 *c = QChar(ushort(qMetaTypeNumber(d)));
369 case QVariant::ULongLong:
370 case QMetaType::UChar:
371 case QMetaType::UShort:
372 case QMetaType::ULong:
373 *c = QChar(ushort(qMetaTypeUNumber(d)));
380 #ifndef QT_NO_GEOM_VARIANT
381 case QVariant::Size: {
382 QSize *s = static_cast<QSize *>(result);
384 case QVariant::SizeF:
385 *s = v_cast<QSizeF>(d)->toSize();
393 case QVariant::SizeF: {
394 QSizeF *s = static_cast<QSizeF *>(result);
397 *s = QSizeF(*(v_cast<QSize>(d)));
405 case QVariant::Line: {
406 QLine *s = static_cast<QLine *>(result);
408 case QVariant::LineF:
409 *s = v_cast<QLineF>(d)->toLine();
417 case QVariant::LineF: {
418 QLineF *s = static_cast<QLineF *>(result);
421 *s = QLineF(*(v_cast<QLine>(d)));
429 case QVariant::StringList:
430 if (d->type == QVariant::List) {
431 QStringList *slst = static_cast<QStringList *>(result);
432 const QVariantList *list = v_cast<QVariantList >(d);
433 for (int i = 0; i < list->size(); ++i)
434 slst->append(list->at(i).toString());
435 } else if (d->type == QVariant::String) {
436 QStringList *slst = static_cast<QStringList *>(result);
437 *slst = QStringList(*v_cast<QString>(d));
442 case QVariant::Date: {
443 QDate *dt = static_cast<QDate *>(result);
444 if (d->type == QVariant::DateTime)
445 *dt = v_cast<QDateTime>(d)->date();
446 #ifndef QT_NO_DATESTRING
447 else if (d->type == QVariant::String)
448 *dt = QDate::fromString(*v_cast<QString>(d), Qt::ISODate);
453 return dt->isValid();
455 case QVariant::Time: {
456 QTime *t = static_cast<QTime *>(result);
458 case QVariant::DateTime:
459 *t = v_cast<QDateTime>(d)->time();
461 #ifndef QT_NO_DATESTRING
462 case QVariant::String:
463 *t = QTime::fromString(*v_cast<QString>(d), Qt::ISODate);
471 case QVariant::DateTime: {
472 QDateTime *dt = static_cast<QDateTime *>(result);
474 #ifndef QT_NO_DATESTRING
475 case QVariant::String:
476 *dt = QDateTime::fromString(*v_cast<QString>(d), Qt::ISODate);
480 *dt = QDateTime(*v_cast<QDate>(d));
485 return dt->isValid();
487 case QVariant::ByteArray: {
488 QByteArray *ba = static_cast<QByteArray *>(result);
490 case QVariant::String:
491 *ba = v_cast<QString>(d)->toAscii();
493 case QVariant::Double:
494 *ba = QByteArray::number(d->data.d, 'g', DBL_DIG);
496 case QMetaType::Float:
497 *ba = QByteArray::number(d->data.f, 'g', FLT_DIG);
499 case QMetaType::Char:
500 case QMetaType::UChar:
501 *ba = QByteArray(1, d->data.c);
504 case QVariant::LongLong:
505 case QMetaType::Short:
506 case QMetaType::Long:
507 *ba = QByteArray::number(qMetaTypeNumber(d));
510 case QVariant::ULongLong:
511 case QMetaType::UShort:
512 case QMetaType::ULong:
513 *ba = QByteArray::number(qMetaTypeUNumber(d));
516 *ba = QByteArray(d->data.b ? "true" : "false");
523 case QMetaType::Short:
524 *static_cast<short *>(result) = short(qConvertToNumber(d, ok));
526 case QMetaType::Long:
527 *static_cast<long *>(result) = long(qConvertToNumber(d, ok));
529 case QMetaType::UShort:
530 *static_cast<ushort *>(result) = ushort(qConvertToUnsignedNumber(d, ok));
532 case QMetaType::ULong:
533 *static_cast<ulong *>(result) = ulong(qConvertToUnsignedNumber(d, ok));
536 *static_cast<int *>(result) = int(qConvertToNumber(d, ok));
539 *static_cast<uint *>(result) = uint(qConvertToUnsignedNumber(d, ok));
541 case QVariant::LongLong:
542 *static_cast<qlonglong *>(result) = qConvertToNumber(d, ok);
544 case QVariant::ULongLong: {
545 *static_cast<qulonglong *>(result) = qConvertToUnsignedNumber(d, ok);
548 case QMetaType::UChar: {
549 *static_cast<uchar *>(result) = qConvertToUnsignedNumber(d, ok);
552 case QVariant::Bool: {
553 bool *b = static_cast<bool *>(result);
555 case QVariant::ByteArray:
556 *b = qt_convertToBool<QByteArray, QByteArray>(d);
558 case QVariant::String:
559 *b = qt_convertToBool<QString, QLatin1String>(d);
562 *b = !v_cast<QChar>(d)->isNull();
564 case QVariant::Double:
566 case QVariant::LongLong:
567 case QMetaType::Char:
568 case QMetaType::Short:
569 case QMetaType::Long:
570 case QMetaType::Float:
571 *b = qMetaTypeNumber(d) != Q_INT64_C(0);
574 case QVariant::ULongLong:
575 case QMetaType::UChar:
576 case QMetaType::UShort:
577 case QMetaType::ULong:
578 *b = qMetaTypeUNumber(d) != Q_UINT64_C(0);
586 case QVariant::Double: {
587 double *f = static_cast<double *>(result);
589 case QVariant::String:
590 *f = v_cast<QString>(d)->toDouble(ok);
592 case QVariant::ByteArray:
593 *f = v_cast<QByteArray>(d)->toDouble(ok);
596 *f = double(d->data.b);
598 case QMetaType::Float:
599 *f = double(d->data.f);
601 case QVariant::LongLong:
603 case QMetaType::Char:
604 case QMetaType::Short:
605 case QMetaType::Long:
606 *f = double(qMetaTypeNumber(d));
609 case QVariant::ULongLong:
610 case QMetaType::UChar:
611 case QMetaType::UShort:
612 case QMetaType::ULong:
613 *f = double(qMetaTypeUNumber(d));
621 case QMetaType::Float: {
622 float *f = static_cast<float *>(result);
624 case QVariant::String:
625 *f = v_cast<QString>(d)->toFloat(ok);
627 case QVariant::ByteArray:
628 *f = v_cast<QByteArray>(d)->toFloat(ok);
631 *f = float(d->data.b);
633 case QVariant::Double:
634 *f = float(d->data.d);
636 case QVariant::LongLong:
638 case QMetaType::Char:
639 case QMetaType::Short:
640 case QMetaType::Long:
641 *f = float(qMetaTypeNumber(d));
644 case QVariant::ULongLong:
645 case QMetaType::UChar:
646 case QMetaType::UShort:
647 case QMetaType::ULong:
648 *f = float(qMetaTypeUNumber(d));
657 if (d->type == QVariant::StringList) {
658 QVariantList *lst = static_cast<QVariantList *>(result);
659 const QStringList *slist = v_cast<QStringList>(d);
660 for (int i = 0; i < slist->size(); ++i)
661 lst->append(QVariant(slist->at(i)));
662 } else if (qstrcmp(QMetaType::typeName(d->type), "QList<QVariant>") == 0) {
663 *static_cast<QVariantList *>(result) =
664 *static_cast<QList<QVariant> *>(d->data.shared->ptr);
670 if (qstrcmp(QMetaType::typeName(d->type), "QMap<QString, QVariant>") == 0) {
671 *static_cast<QVariantMap *>(result) =
672 *static_cast<QMap<QString, QVariant> *>(d->data.shared->ptr);
678 if (qstrcmp(QMetaType::typeName(d->type), "QHash<QString, QVariant>") == 0) {
679 *static_cast<QVariantHash *>(result) =
680 *static_cast<QHash<QString, QVariant> *>(d->data.shared->ptr);
685 #ifndef QT_NO_GEOM_VARIANT
687 if (d->type == QVariant::RectF)
688 *static_cast<QRect *>(result) = (v_cast<QRectF>(d))->toRect();
692 case QVariant::RectF:
693 if (d->type == QVariant::Rect)
694 *static_cast<QRectF *>(result) = *v_cast<QRect>(d);
698 case QVariant::PointF:
699 if (d->type == QVariant::Point)
700 *static_cast<QPointF *>(result) = *v_cast<QPoint>(d);
704 case QVariant::Point:
705 if (d->type == QVariant::PointF)
706 *static_cast<QPoint *>(result) = (v_cast<QPointF>(d))->toPoint();
710 case QMetaType::Char:
712 *static_cast<qint8 *>(result) = qint8(qConvertToNumber(d, ok));
718 case QVariant::String:
719 *static_cast<QUuid *>(result) = QUuid(*v_cast<QString>(d));
731 #if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)
732 static void streamDebug(QDebug dbg, const QVariant &v)
734 switch (v.userType()) {
736 dbg.nospace() << v.toInt();
739 dbg.nospace() << v.toUInt();
741 case QVariant::LongLong:
742 dbg.nospace() << v.toLongLong();
744 case QVariant::ULongLong:
745 dbg.nospace() << v.toULongLong();
747 case QMetaType::Float:
748 dbg.nospace() << v.toFloat();
750 case QMetaType::QObjectStar:
751 dbg.nospace() << qvariant_cast<QObject *>(v);
753 case QVariant::Double:
754 dbg.nospace() << v.toDouble();
757 dbg.nospace() << v.toBool();
759 case QVariant::String:
760 dbg.nospace() << v.toString();
763 dbg.nospace() << v.toChar();
765 case QVariant::StringList:
766 dbg.nospace() << v.toStringList();
769 dbg.nospace() << v.toMap();
772 dbg.nospace() << v.toHash();
775 dbg.nospace() << v.toList();
778 dbg.nospace() << v.toDate();
781 dbg.nospace() << v.toTime();
783 case QVariant::DateTime:
784 dbg.nospace() << v.toDateTime();
786 #ifndef QT_BOOTSTRAPPED
787 case QVariant::EasingCurve:
788 dbg.nospace() << v.toEasingCurve();
791 case QVariant::ByteArray:
792 dbg.nospace() << v.toByteArray();
795 dbg.nospace() << v.toUrl();
797 #ifndef QT_NO_GEOM_VARIANT
798 case QVariant::Point:
799 dbg.nospace() << v.toPoint();
801 case QVariant::PointF:
802 dbg.nospace() << v.toPointF();
805 dbg.nospace() << v.toRect();
808 dbg.nospace() << v.toSize();
810 case QVariant::SizeF:
811 dbg.nospace() << v.toSizeF();
814 dbg.nospace() << v.toLine();
816 case QVariant::LineF:
817 dbg.nospace() << v.toLineF();
819 case QVariant::RectF:
820 dbg.nospace() << v.toRectF();
824 dbg.nospace() << v.value<QUuid>().toString();
826 case QVariant::BitArray:
827 //dbg.nospace() << v.toBitArray();
835 const QVariant::Handler qt_kernel_variant_handler = {
839 #ifndef QT_NO_DATASTREAM
846 #if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)
853 static void dummyConstruct(QVariant::Private *, const void *) { Q_ASSERT_X(false, "QVariant", "Trying to construct an unknown type"); }
854 static void dummyClear(QVariant::Private *) { Q_ASSERT_X(false, "QVariant", "Trying to clear an unknown type"); }
855 static bool dummyIsNull(const QVariant::Private *d) { Q_ASSERT_X(false, "QVariant::isNull", "Trying to call isNull on an unknown type"); return d->is_null; }
856 static bool dummyCompare(const QVariant::Private *, const QVariant::Private *) { Q_ASSERT_X(false, "QVariant", "Trying to compare an unknown types"); return false; }
857 static bool dummyConvert(const QVariant::Private *, QVariant::Type , void *, bool *) { Q_ASSERT_X(false, "QVariant", "Trying to convert an unknown type"); return false; }
858 #if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)
859 static void dummyStreamDebug(QDebug, const QVariant &) { Q_ASSERT_X(false, "QVariant", "Trying to convert an unknown type"); }
861 const QVariant::Handler qt_dummy_variant_handler = {
865 #ifndef QT_NO_DATASTREAM
872 #if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)
879 static void customConstruct(QVariant::Private *d, const void *copy)
881 const uint size = QMetaType::sizeOf(d->type);
883 d->type = QVariant::Invalid;
887 // this logic should match with QVariantIntegrator::CanUseInternalSpace
888 if (size <= sizeof(QVariant::Private::Data)
889 && (QMetaType::typeFlags(d->type) & QMetaType::MovableType)) {
890 QMetaType::construct(d->type, &d->data.ptr, copy);
891 d->is_shared = false;
893 void *ptr = QMetaType::create(d->type, copy);
895 d->data.shared = new QVariant::PrivateShared(ptr);
899 static void customClear(QVariant::Private *d)
902 QMetaType::destruct(d->type, &d->data.ptr);
904 QMetaType::destroy(d->type, d->data.shared->ptr);
905 delete d->data.shared;
909 static bool customIsNull(const QVariant::Private *d)
914 static bool customCompare(const QVariant::Private *a, const QVariant::Private *b)
916 const char *const typeName = QMetaType::typeName(a->type);
917 if (Q_UNLIKELY(!typeName) && Q_LIKELY(!QMetaType::isRegistered(a->type)))
918 qFatal("QVariant::compare: type %d unknown to QVariant.", a->type);
920 const void *a_ptr = a->is_shared ? a->data.shared->ptr : &(a->data.ptr);
921 const void *b_ptr = b->is_shared ? b->data.shared->ptr : &(b->data.ptr);
923 uint typeNameLen = qstrlen(typeName);
924 if (typeNameLen > 0 && typeName[typeNameLen - 1] == '*')
925 return *static_cast<void *const *>(a_ptr) == *static_cast<void *const *>(b_ptr);
927 if (a->is_null && b->is_null)
930 return !memcmp(a_ptr, b_ptr, QMetaType::sizeOf(a->type));
933 static bool customConvert(const QVariant::Private *, QVariant::Type, void *, bool *ok)
940 #if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)
941 static void customStreamDebug(QDebug, const QVariant &) {}
944 const QVariant::Handler qt_custom_variant_handler = {
948 #ifndef QT_NO_DATASTREAM
955 #if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)
962 } // annonymous used to hide QVariant handlers
964 static HandlersManager handlerManager;
965 Q_STATIC_ASSERT_X(!QModulesPrivate::Core, "Initialization assumes that ModulesNames::Core is 0");
966 const QVariant::Handler *HandlersManager::Handlers[QModulesPrivate::ModulesCount]
967 = { &qt_kernel_variant_handler, &qt_dummy_variant_handler,
968 &qt_dummy_variant_handler, &qt_custom_variant_handler };
970 Q_CORE_EXPORT const QVariant::Handler *qcoreVariantHandler()
972 return &qt_kernel_variant_handler;
975 inline void HandlersManager::unregisterHandler(const QModulesPrivate::Names name)
977 Handlers[name] = &qt_dummy_variant_handler;
980 Q_CORE_EXPORT void QVariantPrivate::registerHandler(const int /* Modules::Names */name, const QVariant::Handler *handler)
982 handlerManager.registerHandler(static_cast<QModulesPrivate::Names>(name), handler);
985 Q_CORE_EXPORT void QVariantPrivate::unregisterHandler(const int /* Modules::Names */ name)
987 handlerManager.unregisterHandler(static_cast<QModulesPrivate::Names>(name));
992 \brief The QVariant class acts like a union for the most common Qt data types.
998 Because C++ forbids unions from including types that have
999 non-default constructors or destructors, most interesting Qt
1000 classes cannot be used in unions. Without QVariant, this would be
1001 a problem for QObject::property() and for database work, etc.
1003 A QVariant object holds a single value of a single type() at a
1004 time. (Some type()s are multi-valued, for example a string list.)
1005 You can find out what type, T, the variant holds, convert it to a
1006 different type using convert(), get its value using one of the
1007 toT() functions (e.g., toSize()) and check whether the type can
1008 be converted to a particular type using canConvert().
1010 The methods named toT() (e.g., toInt(), toString()) are const. If
1011 you ask for the stored type, they return a copy of the stored
1012 object. If you ask for a type that can be generated from the
1013 stored type, toT() copies and converts and leaves the object
1014 itself unchanged. If you ask for a type that cannot be generated
1015 from the stored type, the result depends on the type; see the
1016 function documentation for details.
1018 Here is some example code to demonstrate the use of QVariant:
1020 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 0
1022 You can even store QList<QVariant> and QMap<QString, QVariant>
1023 values in a variant, so you can easily construct arbitrarily
1024 complex data structures of arbitrary types. This is very powerful
1025 and versatile, but may prove less memory and speed efficient than
1026 storing specific types in standard data structures.
1028 QVariant also supports the notion of null values, where you can
1029 have a defined type with no value set. However, note that QVariant
1030 types can only be cast when they have had a value set.
1032 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 1
1034 QVariant can be extended to support other types than those
1035 mentioned in the \l Type enum. See the \l QMetaType documentation
1038 \section1 A Note on GUI Types
1040 Because QVariant is part of the QtCore library, it cannot provide
1041 conversion functions to data types defined in QtGui, such as
1042 QColor, QImage, and QPixmap. In other words, there is no \c
1043 toColor() function. Instead, you can use the QVariant::value() or
1044 the qvariant_cast() template function. For example:
1046 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 2
1048 The inverse conversion (e.g., from QColor to QVariant) is
1049 automatic for all data types supported by QVariant, including
1052 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 3
1054 \section1 Using canConvert() and convert() Consecutively
1056 When using canConvert() and convert() consecutively, it is possible for
1057 canConvert() to return true, but convert() to return false. This
1058 is typically because canConvert() only reports the general ability of
1059 QVariant to convert between types given suitable data; it is still
1060 possible to supply data which cannot actually be converted.
1062 For example, canConvert() would return true when called on a variant
1063 containing a string because, in principle, QVariant is able to convert
1064 strings of numbers to integers.
1065 However, if the string contains non-numeric characters, it cannot be
1066 converted to an integer, and any attempt to convert it will fail.
1067 Hence, it is important to have both functions return true for a
1068 successful conversion.
1074 \enum QVariant::Type
1076 This enum type defines the types of variable that a QVariant can
1079 \value Invalid no type
1080 \value BitArray a QBitArray
1081 \value Bitmap a QBitmap
1083 \value Brush a QBrush
1084 \value ByteArray a QByteArray
1086 \value Color a QColor
1087 \value Cursor a QCursor
1089 \value DateTime a QDateTime
1090 \value Double a double
1091 \value EasingCurve a QEasingCurve
1094 \value Hash a QVariantHash
1096 \value Image a QImage
1098 \value KeySequence a QKeySequence
1100 \value LineF a QLineF
1101 \value List a QVariantList
1102 \value Locale a QLocale
1103 \value LongLong a \l qlonglong
1104 \value Map a QVariantMap
1105 \value Matrix a QMatrix
1106 \value Transform a QTransform
1107 \value Matrix4x4 a QMatrix4x4
1108 \value Palette a QPalette
1110 \value Pixmap a QPixmap
1111 \value Point a QPoint
1112 \value PointF a QPointF
1113 \value Polygon a QPolygon
1114 \value PolygonF a QPolygonF
1115 \value Quaternion a QQuaternion
1117 \value RectF a QRectF
1118 \value RegExp a QRegExp
1119 \value Region a QRegion
1121 \value SizeF a QSizeF
1122 \value SizePolicy a QSizePolicy
1123 \value String a QString
1124 \value StringList a QStringList
1125 \value TextFormat a QTextFormat
1126 \value TextLength a QTextLength
1128 \value UInt a \l uint
1129 \value ULongLong a \l qulonglong
1131 \value Vector2D a QVector2D
1132 \value Vector3D a QVector3D
1133 \value Vector4D a QVector4D
1135 \value UserType Base value for user-defined types.
1138 \omitvalue ColorGroup
1140 \omitvalue LastGuiType
1141 \omitvalue LastCoreType
1146 \fn QVariant::QVariant()
1148 Constructs an invalid variant.
1153 \fn QVariant::QVariant(int typeOrUserType, const void *copy)
1155 Constructs variant of type \a typeOrUserType, and initializes with
1156 \a copy if \a copy is not 0.
1158 Note that you have to pass the address of the variable you want stored.
1160 Usually, you never have to use this constructor, use QVariant::fromValue()
1161 instead to construct variants from the pointer types represented by
1162 \c QMetaType::VoidStar, \c QMetaType::QObjectStar and
1163 \c QMetaType::QWidgetStar.
1165 \sa QVariant::fromValue(), Type
1169 \fn QVariant::QVariant(Type type)
1171 Constructs a null variant of type \a type.
1177 \fn QVariant::create(int type, const void *copy)
1181 Constructs a variant private of type \a type, and initializes with \a copy if
1185 void QVariant::create(int type, const void *copy)
1188 handlerManager[type]->construct(&d, copy);
1192 \fn QVariant::~QVariant()
1194 Destroys the QVariant and the contained object.
1196 Note that subclasses that reimplement clear() should reimplement
1197 the destructor to call clear(). This destructor calls clear(), but
1198 because it is the destructor, QVariant::clear() is called rather
1199 than a subclass's clear().
1202 QVariant::~QVariant()
1204 if ((d.is_shared && !d.data.shared->ref.deref()) || (!d.is_shared && d.type > Char))
1205 handlerManager[d.type]->clear(&d);
1209 \fn QVariant::QVariant(const QVariant &p)
1211 Constructs a copy of the variant, \a p, passed as the argument to
1215 QVariant::QVariant(const QVariant &p)
1219 d.data.shared->ref.ref();
1220 } else if (p.d.type > Char) {
1221 handlerManager[d.type]->construct(&d, p.constData());
1222 d.is_null = p.d.is_null;
1226 #ifndef QT_NO_DATASTREAM
1228 Reads the variant from the data stream, \a s.
1230 QVariant::QVariant(QDataStream &s)
1235 #endif //QT_NO_DATASTREAM
1238 \fn QVariant::QVariant(const QString &val)
1240 Constructs a new variant with a string value, \a val.
1244 \fn QVariant::QVariant(const QLatin1String &val)
1246 Constructs a new variant with a string value, \a val.
1250 \fn QVariant::QVariant(const char *val)
1252 Constructs a new variant with a string value of \a val.
1253 The variant creates a deep copy of \a val, using the encoding
1254 set by QTextCodec::setCodecForCStrings().
1256 Note that \a val is converted to a QString for storing in the
1257 variant and QVariant::type() will return QMetaType::QString for
1260 You can disable this operator by defining \c
1261 QT_NO_CAST_FROM_ASCII when you compile your applications.
1263 \sa QTextCodec::setCodecForCStrings()
1266 #ifndef QT_NO_CAST_FROM_ASCII
1267 QVariant::QVariant(const char *val)
1269 QString s = QString::fromAscii(val);
1275 \fn QVariant::QVariant(const QStringList &val)
1277 Constructs a new variant with a string list value, \a val.
1281 \fn QVariant::QVariant(const QMap<QString, QVariant> &val)
1283 Constructs a new variant with a map of QVariants, \a val.
1287 \fn QVariant::QVariant(const QHash<QString, QVariant> &val)
1289 Constructs a new variant with a hash of QVariants, \a val.
1293 \fn QVariant::QVariant(const QDate &val)
1295 Constructs a new variant with a date value, \a val.
1299 \fn QVariant::QVariant(const QTime &val)
1301 Constructs a new variant with a time value, \a val.
1305 \fn QVariant::QVariant(const QDateTime &val)
1307 Constructs a new variant with a date/time value, \a val.
1312 \fn QVariant::QVariant(const QEasingCurve &val)
1314 Constructs a new variant with an easing curve value, \a val.
1318 \fn QVariant::QVariant(const QByteArray &val)
1320 Constructs a new variant with a bytearray value, \a val.
1324 \fn QVariant::QVariant(const QBitArray &val)
1326 Constructs a new variant with a bitarray value, \a val.
1330 \fn QVariant::QVariant(const QPoint &val)
1332 Constructs a new variant with a point value of \a val.
1336 \fn QVariant::QVariant(const QPointF &val)
1338 Constructs a new variant with a point value of \a val.
1342 \fn QVariant::QVariant(const QRectF &val)
1344 Constructs a new variant with a rect value of \a val.
1348 \fn QVariant::QVariant(const QLineF &val)
1350 Constructs a new variant with a line value of \a val.
1354 \fn QVariant::QVariant(const QLine &val)
1356 Constructs a new variant with a line value of \a val.
1360 \fn QVariant::QVariant(const QRect &val)
1362 Constructs a new variant with a rect value of \a val.
1366 \fn QVariant::QVariant(const QSize &val)
1368 Constructs a new variant with a size value of \a val.
1372 \fn QVariant::QVariant(const QSizeF &val)
1374 Constructs a new variant with a size value of \a val.
1378 \fn QVariant::QVariant(const QUrl &val)
1380 Constructs a new variant with a url value of \a val.
1384 \fn QVariant::QVariant(int val)
1386 Constructs a new variant with an integer value, \a val.
1390 \fn QVariant::QVariant(uint val)
1392 Constructs a new variant with an unsigned integer value, \a val.
1396 \fn QVariant::QVariant(qlonglong val)
1398 Constructs a new variant with a long long integer value, \a val.
1402 \fn QVariant::QVariant(qulonglong val)
1404 Constructs a new variant with an unsigned long long integer value, \a val.
1409 \fn QVariant::QVariant(bool val)
1411 Constructs a new variant with a boolean value, \a val.
1415 \fn QVariant::QVariant(double val)
1417 Constructs a new variant with a floating point value, \a val.
1421 \fn QVariant::QVariant(float val)
1423 Constructs a new variant with a floating point value, \a val.
1428 \fn QVariant::QVariant(const QList<QVariant> &val)
1430 Constructs a new variant with a list value, \a val.
1434 \fn QVariant::QVariant(const QChar &c)
1436 Constructs a new variant with a char value, \a c.
1440 \fn QVariant::QVariant(const QLocale &l)
1442 Constructs a new variant with a locale value, \a l.
1446 \fn QVariant::QVariant(const QRegExp ®Exp)
1448 Constructs a new variant with the regexp value \a regExp.
1452 \fn QVariant::QVariant(Qt::GlobalColor color)
1454 Constructs a new variant of type QVariant::Color and initializes
1457 This is a convenience constructor that allows \c{QVariant(Qt::blue);}
1458 to create a valid QVariant storing a QColor.
1460 Note: This constructor will assert if the application does not link
1461 to the Qt GUI library.
1464 QVariant::QVariant(Type type)
1465 { create(type, 0); }
1466 QVariant::QVariant(int typeOrUserType, const void *copy)
1467 { create(typeOrUserType, copy); d.is_null = false; }
1470 flags is true if it is a pointer type
1472 QVariant::QVariant(int typeOrUserType, const void *copy, uint flags)
1474 if (flags) { //type is a pointer type
1475 d.type = typeOrUserType;
1476 d.data.ptr = *reinterpret_cast<void *const*>(copy);
1478 create(typeOrUserType, copy);
1483 QVariant::QVariant(int val)
1484 { d.is_null = false; d.type = Int; d.data.i = val; }
1485 QVariant::QVariant(uint val)
1486 { d.is_null = false; d.type = UInt; d.data.u = val; }
1487 QVariant::QVariant(qlonglong val)
1488 { d.is_null = false; d.type = LongLong; d.data.ll = val; }
1489 QVariant::QVariant(qulonglong val)
1490 { d.is_null = false; d.type = ULongLong; d.data.ull = val; }
1491 QVariant::QVariant(bool val)
1492 { d.is_null = false; d.type = Bool; d.data.b = val; }
1493 QVariant::QVariant(double val)
1494 { d.is_null = false; d.type = Double; d.data.d = val; }
1496 QVariant::QVariant(const QByteArray &val)
1497 { d.is_null = false; d.type = ByteArray; v_construct<QByteArray>(&d, val); }
1498 QVariant::QVariant(const QBitArray &val)
1499 { d.is_null = false; d.type = BitArray; v_construct<QBitArray>(&d, val); }
1500 QVariant::QVariant(const QString &val)
1501 { d.is_null = false; d.type = String; v_construct<QString>(&d, val); }
1502 QVariant::QVariant(const QChar &val)
1503 { d.is_null = false; d.type = Char; v_construct<QChar>(&d, val); }
1504 QVariant::QVariant(const QLatin1String &val)
1505 { QString str(val); d.is_null = false; d.type = String; v_construct<QString>(&d, str); }
1506 QVariant::QVariant(const QStringList &val)
1507 { d.is_null = false; d.type = StringList; v_construct<QStringList>(&d, val); }
1509 QVariant::QVariant(const QDate &val)
1510 { d.is_null = false; d.type = Date; v_construct<QDate>(&d, val); }
1511 QVariant::QVariant(const QTime &val)
1512 { d.is_null = false; d.type = Time; v_construct<QTime>(&d, val); }
1513 QVariant::QVariant(const QDateTime &val)
1514 { d.is_null = false; d.type = DateTime; v_construct<QDateTime>(&d, val); }
1515 #ifndef QT_BOOTSTRAPPED
1516 QVariant::QVariant(const QEasingCurve &val)
1517 { d.is_null = false; d.type = EasingCurve; v_construct<QEasingCurve>(&d, val); }
1519 QVariant::QVariant(const QList<QVariant> &list)
1520 { d.is_null = false; d.type = List; v_construct<QVariantList>(&d, list); }
1521 QVariant::QVariant(const QMap<QString, QVariant> &map)
1522 { d.is_null = false; d.type = Map; v_construct<QVariantMap>(&d, map); }
1523 QVariant::QVariant(const QHash<QString, QVariant> &hash)
1524 { d.is_null = false; d.type = Hash; v_construct<QVariantHash>(&d, hash); }
1525 #ifndef QT_NO_GEOM_VARIANT
1526 QVariant::QVariant(const QPoint &pt) { d.is_null = false; d.type = Point; v_construct<QPoint>(&d, pt); }
1527 QVariant::QVariant(const QPointF &pt) { d.is_null = false; d.type = PointF; v_construct<QPointF>(&d, pt); }
1528 QVariant::QVariant(const QRectF &r) { d.is_null = false; d.type = RectF; v_construct<QRectF>(&d, r); }
1529 QVariant::QVariant(const QLineF &l) { d.is_null = false; d.type = LineF; v_construct<QLineF>(&d, l); }
1530 QVariant::QVariant(const QLine &l) { d.is_null = false; d.type = Line; v_construct<QLine>(&d, l); }
1531 QVariant::QVariant(const QRect &r) { d.is_null = false; d.type = Rect; v_construct<QRect>(&d, r); }
1532 QVariant::QVariant(const QSize &s) { d.is_null = false; d.type = Size; v_construct<QSize>(&d, s); }
1533 QVariant::QVariant(const QSizeF &s) { d.is_null = false; d.type = SizeF; v_construct<QSizeF>(&d, s); }
1535 QVariant::QVariant(const QUrl &u) { d.is_null = false; d.type = Url; v_construct<QUrl>(&d, u); }
1536 QVariant::QVariant(const QLocale &l) { d.is_null = false; d.type = Locale; v_construct<QLocale>(&d, l); }
1537 #ifndef QT_NO_REGEXP
1538 QVariant::QVariant(const QRegExp ®Exp) { d.is_null = false; d.type = RegExp; v_construct<QRegExp>(&d, regExp); }
1540 QVariant::QVariant(Qt::GlobalColor color) { create(62, &color); }
1543 Returns the storage type of the value stored in the variant.
1544 Although this function is declared as returning QVariant::Type,
1545 the return value should be interpreted as QMetaType::Type. In
1546 particular, QVariant::UserType is returned here only if the value
1547 is equal or greater than QMetaType::User.
1549 Note that return values in the ranges QVariant::Char through
1550 QVariant::RegExp and QVariant::Font through QVariant::Transform
1551 correspond to the values in the ranges QMetaType::QChar through
1552 QMetaType::QRegExp and QMetaType::QFont through QMetaType::QQuaternion.
1554 Pay particular attention when working with char and QChar
1555 variants. Note that there is no QVariant constructor specifically
1556 for type char, but there is one for QChar. For a variant of type
1557 QChar, this function returns QVariant::Char, which is the same as
1558 QMetaType::QChar, but for a variant of type \c char, this function
1559 returns QMetaType::Char, which is \e not the same as
1562 Also note that the types \c void*, \c long, \c short, \c unsigned
1563 \c long, \c unsigned \c short, \c unsigned \c char, \c float, \c
1564 QObject*, and \c QWidget* are represented in QMetaType::Type but
1565 not in QVariant::Type, and they can be returned by this function.
1566 However, they are considered to be user defined types when tested
1567 against QVariant::Type.
1569 To test whether an instance of QVariant contains a data type that
1570 is compatible with the data type you are interested in, use
1574 QVariant::Type QVariant::type() const
1576 return d.type >= QMetaType::User ? UserType : static_cast<Type>(d.type);
1580 Returns the storage type of the value stored in the variant. For
1581 non-user types, this is the same as type().
1586 int QVariant::userType() const
1592 Assigns the value of the variant \a variant to this variant.
1594 QVariant& QVariant::operator=(const QVariant &variant)
1596 if (this == &variant)
1600 if (variant.d.is_shared) {
1601 variant.d.data.shared->ref.ref();
1603 } else if (variant.d.type > Char) {
1604 d.type = variant.d.type;
1605 handlerManager[d.type]->construct(&d, variant.constData());
1606 d.is_null = variant.d.is_null;
1615 \fn void QVariant::swap(QVariant &other)
1618 Swaps variant \a other with this variant. This operation is very
1619 fast and never fails.
1623 \fn void QVariant::detach()
1628 void QVariant::detach()
1630 if (!d.is_shared || d.data.shared->ref.load() == 1)
1635 handlerManager[d.type]->construct(&dd, constData());
1636 if (!d.data.shared->ref.deref())
1637 handlerManager[d.type]->clear(&d);
1638 d.data.shared = dd.data.shared;
1642 \fn bool QVariant::isDetached() const
1647 // ### Qt 5: change typeName()(and froends= to return a QString. Suggestion from Harald.
1649 Returns the name of the type stored in the variant. The returned
1650 strings describe the C++ datatype used to store the data: for
1651 example, "QFont", "QString", or "QVariantList". An Invalid
1654 const char *QVariant::typeName() const
1656 return typeToName(Type(d.type));
1660 Convert this variant to type Invalid and free up any resources
1663 void QVariant::clear()
1665 if ((d.is_shared && !d.data.shared->ref.deref()) || (!d.is_shared && d.type > Char))
1666 handlerManager[d.type]->clear(&d);
1669 d.is_shared = false;
1673 Converts the enum representation of the storage type, \a typ, to
1674 its string representation.
1676 Returns a null pointer if the type is QVariant::Invalid or doesn't exist.
1678 const char *QVariant::typeToName(Type typ)
1682 if (typ == UserType)
1685 return QMetaType::typeName(typ);
1690 Converts the string representation of the storage type given in \a
1691 name, to its enum representation.
1693 If the string representation cannot be converted to any enum
1694 representation, the variant is set to \c Invalid.
1696 QVariant::Type QVariant::nameToType(const char *name)
1698 if (!name || !*name)
1700 if (strcmp(name, "Q3CString") == 0)
1702 if (strcmp(name, "Q_LLONG") == 0)
1704 if (strcmp(name, "Q_ULLONG") == 0)
1706 if (strcmp(name, "QIconSet") == 0)
1708 if (strcmp(name, "UserType") == 0)
1711 int metaType = QMetaType::type(name);
1712 return metaType <= int(LastGuiType) ? QVariant::Type(metaType) : UserType;
1715 #ifndef QT_NO_DATASTREAM
1716 enum { MapFromThreeCount = 36 };
1717 static const ushort map_from_three[MapFromThreeCount] =
1723 QVariant::StringList,
1739 QVariant::ByteArray,
1744 QVariant::SizePolicy,
1748 QVariant::ByteArray,
1750 QVariant::KeySequence,
1753 QVariant::ULongLong,
1754 QVariant::EasingCurve
1758 Internal function for loading a variant from stream \a s. Use the
1759 stream operators instead.
1763 void QVariant::load(QDataStream &s)
1769 if (s.version() < QDataStream::Qt_4_0) {
1770 if (u >= MapFromThreeCount)
1772 u = map_from_three[u];
1774 qint8 is_null = false;
1775 if (s.version() >= QDataStream::Qt_4_2)
1777 if (u == QVariant::UserType) {
1780 u = QMetaType::type(name);
1782 s.setStatus(QDataStream::ReadCorruptData);
1786 create(static_cast<int>(u), 0);
1787 d.is_null = is_null;
1790 // Since we wrote something, we should read something
1797 // const cast is safe since we operate on a newly constructed variant
1798 if (!QMetaType::load(s, d.type, const_cast<void *>(constData()))) {
1799 s.setStatus(QDataStream::ReadCorruptData);
1800 qWarning("QVariant::load: unable to load type %d.", d.type);
1805 Internal function for saving a variant to the stream \a s. Use the
1806 stream operators instead.
1810 void QVariant::save(QDataStream &s) const
1812 quint32 tp = type();
1813 if (s.version() < QDataStream::Qt_4_0) {
1815 for (i = MapFromThreeCount - 1; i >= 0; i--) {
1816 if (map_from_three[i] == tp) {
1827 if (s.version() >= QDataStream::Qt_4_2)
1828 s << qint8(d.is_null);
1829 if (tp == QVariant::UserType) {
1830 s << QMetaType::typeName(userType());
1838 if (!QMetaType::save(s, d.type, constData())) {
1839 Q_ASSERT_X(false, "QVariant::save", "Invalid type to save");
1840 qWarning("QVariant::save: unable to save type %d.", d.type);
1847 Reads a variant \a p from the stream \a s.
1849 \sa \link datastreamformat.html Format of the QDataStream
1852 QDataStream& operator>>(QDataStream &s, QVariant &p)
1859 Writes a variant \a p to the stream \a s.
1861 \sa \link datastreamformat.html Format of the QDataStream
1864 QDataStream& operator<<(QDataStream &s, const QVariant &p)
1871 Reads a variant type \a p in enum representation from the stream \a s.
1873 QDataStream& operator>>(QDataStream &s, QVariant::Type &p)
1877 p = (QVariant::Type)u;
1883 Writes a variant type \a p to the stream \a s.
1885 QDataStream& operator<<(QDataStream &s, const QVariant::Type p)
1887 s << static_cast<quint32>(p);
1892 #endif //QT_NO_DATASTREAM
1895 \fn bool QVariant::isValid() const
1897 Returns true if the storage type of this variant is not
1898 QVariant::Invalid; otherwise returns false.
1901 template <typename T>
1902 inline T qVariantToHelper(const QVariant::Private &d, QVariant::Type t, const HandlersManager &handler)
1905 return *v_cast<T>(&d);
1908 handler[d.type]->convert(&d, t, &ret, 0);
1913 \fn QStringList QVariant::toStringList() const
1915 Returns the variant as a QStringList if the variant has type()
1916 StringList, \l String, or \l List of a type that can be converted
1917 to QString; otherwise returns an empty list.
1919 \sa canConvert(), convert()
1921 QStringList QVariant::toStringList() const
1923 return qVariantToHelper<QStringList>(d, StringList, handlerManager);
1927 Returns the variant as a QString if the variant has type() \l
1928 String, \l Bool, \l ByteArray, \l Char, \l Date, \l DateTime, \l
1929 Double, \l Int, \l LongLong, \l StringList, \l Time, \l UInt, or
1930 \l ULongLong; otherwise returns an empty string.
1932 \sa canConvert(), convert()
1934 QString QVariant::toString() const
1936 return qVariantToHelper<QString>(d, String, handlerManager);
1940 Returns the variant as a QMap<QString, QVariant> if the variant
1941 has type() \l Map; otherwise returns an empty map.
1943 \sa canConvert(), convert()
1945 QVariantMap QVariant::toMap() const
1947 return qVariantToHelper<QVariantMap>(d, Map, handlerManager);
1951 Returns the variant as a QHash<QString, QVariant> if the variant
1952 has type() \l Hash; otherwise returns an empty map.
1954 \sa canConvert(), convert()
1956 QVariantHash QVariant::toHash() const
1958 return qVariantToHelper<QVariantHash>(d, Hash, handlerManager);
1962 \fn QDate QVariant::toDate() const
1964 Returns the variant as a QDate if the variant has type() \l Date,
1965 \l DateTime, or \l String; otherwise returns an invalid date.
1967 If the type() is \l String, an invalid date will be returned if the
1968 string cannot be parsed as a Qt::ISODate format date.
1970 \sa canConvert(), convert()
1972 QDate QVariant::toDate() const
1974 return qVariantToHelper<QDate>(d, Date, handlerManager);
1978 \fn QTime QVariant::toTime() const
1980 Returns the variant as a QTime if the variant has type() \l Time,
1981 \l DateTime, or \l String; otherwise returns an invalid time.
1983 If the type() is \l String, an invalid time will be returned if
1984 the string cannot be parsed as a Qt::ISODate format time.
1986 \sa canConvert(), convert()
1988 QTime QVariant::toTime() const
1990 return qVariantToHelper<QTime>(d, Time, handlerManager);
1994 \fn QDateTime QVariant::toDateTime() const
1996 Returns the variant as a QDateTime if the variant has type() \l
1997 DateTime, \l Date, or \l String; otherwise returns an invalid
2000 If the type() is \l String, an invalid date/time will be returned
2001 if the string cannot be parsed as a Qt::ISODate format date/time.
2003 \sa canConvert(), convert()
2005 QDateTime QVariant::toDateTime() const
2007 return qVariantToHelper<QDateTime>(d, DateTime, handlerManager);
2012 \fn QEasingCurve QVariant::toEasingCurve() const
2014 Returns the variant as a QEasingCurve if the variant has type() \l
2015 EasingCurve; otherwise returns a default easing curve.
2017 \sa canConvert(), convert()
2019 #ifndef QT_BOOTSTRAPPED
2020 QEasingCurve QVariant::toEasingCurve() const
2022 return qVariantToHelper<QEasingCurve>(d, EasingCurve, handlerManager);
2027 \fn QByteArray QVariant::toByteArray() const
2029 Returns the variant as a QByteArray if the variant has type() \l
2030 ByteArray or \l String (converted using QString::fromAscii());
2031 otherwise returns an empty byte array.
2033 \sa canConvert(), convert()
2035 QByteArray QVariant::toByteArray() const
2037 return qVariantToHelper<QByteArray>(d, ByteArray, handlerManager);
2040 #ifndef QT_NO_GEOM_VARIANT
2042 \fn QPoint QVariant::toPoint() const
2044 Returns the variant as a QPoint if the variant has type()
2045 \l Point or \l PointF; otherwise returns a null QPoint.
2047 \sa canConvert(), convert()
2049 QPoint QVariant::toPoint() const
2051 return qVariantToHelper<QPoint>(d, Point, handlerManager);
2055 \fn QRect QVariant::toRect() const
2057 Returns the variant as a QRect if the variant has type() \l Rect;
2058 otherwise returns an invalid QRect.
2060 \sa canConvert(), convert()
2062 QRect QVariant::toRect() const
2064 return qVariantToHelper<QRect>(d, Rect, handlerManager);
2068 \fn QSize QVariant::toSize() const
2070 Returns the variant as a QSize if the variant has type() \l Size;
2071 otherwise returns an invalid QSize.
2073 \sa canConvert(), convert()
2075 QSize QVariant::toSize() const
2077 return qVariantToHelper<QSize>(d, Size, handlerManager);
2081 \fn QSizeF QVariant::toSizeF() const
2083 Returns the variant as a QSizeF if the variant has type() \l
2084 SizeF; otherwise returns an invalid QSizeF.
2086 \sa canConvert(), convert()
2088 QSizeF QVariant::toSizeF() const
2090 return qVariantToHelper<QSizeF>(d, SizeF, handlerManager);
2094 \fn QRectF QVariant::toRectF() const
2096 Returns the variant as a QRectF if the variant has type() \l Rect
2097 or \l RectF; otherwise returns an invalid QRectF.
2099 \sa canConvert(), convert()
2101 QRectF QVariant::toRectF() const
2103 return qVariantToHelper<QRectF>(d, RectF, handlerManager);
2107 \fn QLineF QVariant::toLineF() const
2109 Returns the variant as a QLineF if the variant has type() \l
2110 LineF; otherwise returns an invalid QLineF.
2112 \sa canConvert(), convert()
2114 QLineF QVariant::toLineF() const
2116 return qVariantToHelper<QLineF>(d, LineF, handlerManager);
2120 \fn QLine QVariant::toLine() const
2122 Returns the variant as a QLine if the variant has type() \l Line;
2123 otherwise returns an invalid QLine.
2125 \sa canConvert(), convert()
2127 QLine QVariant::toLine() const
2129 return qVariantToHelper<QLine>(d, Line, handlerManager);
2133 \fn QPointF QVariant::toPointF() const
2135 Returns the variant as a QPointF if the variant has type() \l
2136 Point or \l PointF; otherwise returns a null QPointF.
2138 \sa canConvert(), convert()
2140 QPointF QVariant::toPointF() const
2142 return qVariantToHelper<QPointF>(d, PointF, handlerManager);
2145 #endif // QT_NO_GEOM_VARIANT
2148 \fn QUrl QVariant::toUrl() const
2150 Returns the variant as a QUrl if the variant has type()
2151 \l Url; otherwise returns an invalid QUrl.
2153 \sa canConvert(), convert()
2155 QUrl QVariant::toUrl() const
2157 return qVariantToHelper<QUrl>(d, Url, handlerManager);
2161 \fn QLocale QVariant::toLocale() const
2163 Returns the variant as a QLocale if the variant has type()
2164 \l Locale; otherwise returns an invalid QLocale.
2166 \sa canConvert(), convert()
2168 QLocale QVariant::toLocale() const
2170 return qVariantToHelper<QLocale>(d, Locale, handlerManager);
2174 \fn QRegExp QVariant::toRegExp() const
2177 Returns the variant as a QRegExp if the variant has type() \l
2178 RegExp; otherwise returns an empty QRegExp.
2180 \sa canConvert(), convert()
2182 #ifndef QT_NO_REGEXP
2183 QRegExp QVariant::toRegExp() const
2185 return qVariantToHelper<QRegExp>(d, RegExp, handlerManager);
2190 \fn QChar QVariant::toChar() const
2192 Returns the variant as a QChar if the variant has type() \l Char,
2193 \l Int, or \l UInt; otherwise returns an invalid QChar.
2195 \sa canConvert(), convert()
2197 QChar QVariant::toChar() const
2199 return qVariantToHelper<QChar>(d, Char, handlerManager);
2203 Returns the variant as a QBitArray if the variant has type()
2204 \l BitArray; otherwise returns an empty bit array.
2206 \sa canConvert(), convert()
2208 QBitArray QVariant::toBitArray() const
2210 return qVariantToHelper<QBitArray>(d, BitArray, handlerManager);
2213 template <typename T>
2214 inline T qNumVariantToHelper(const QVariant::Private &d,
2215 const HandlersManager &handler, bool *ok, const T& val)
2217 uint t = qMetaTypeId<T>();
2224 if (!handler[d.type]->convert(&d, QVariant::Type(t), &ret, ok) && ok)
2230 Returns the variant as an int if the variant has type() \l Int,
2231 \l Bool, \l ByteArray, \l Char, \l Double, \l LongLong, \l
2232 String, \l UInt, or \l ULongLong; otherwise returns 0.
2234 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2235 converted to an int; otherwise \c{*}\a{ok} is set to false.
2237 \bold{Warning:} If the value is convertible to a \l LongLong but is too
2238 large to be represented in an int, the resulting arithmetic overflow will
2239 not be reflected in \a ok. A simple workaround is to use QString::toInt().
2240 Fixing this bug has been postponed to Qt 5 in order to avoid breaking existing code.
2242 \sa canConvert(), convert()
2244 int QVariant::toInt(bool *ok) const
2246 return qNumVariantToHelper<int>(d, handlerManager, ok, d.data.i);
2250 Returns the variant as an unsigned int if the variant has type()
2251 \l UInt, \l Bool, \l ByteArray, \l Char, \l Double, \l Int, \l
2252 LongLong, \l String, or \l ULongLong; otherwise returns 0.
2254 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2255 converted to an unsigned int; otherwise \c{*}\a{ok} is set to false.
2257 \bold{Warning:} If the value is convertible to a \l ULongLong but is too
2258 large to be represented in an unsigned int, the resulting arithmetic overflow will
2259 not be reflected in \a ok. A simple workaround is to use QString::toUInt().
2260 Fixing this bug has been postponed to Qt 5 in order to avoid breaking existing code.
2262 \sa canConvert(), convert()
2264 uint QVariant::toUInt(bool *ok) const
2266 return qNumVariantToHelper<uint>(d, handlerManager, ok, d.data.u);
2270 Returns the variant as a long long int if the variant has type()
2271 \l LongLong, \l Bool, \l ByteArray, \l Char, \l Double, \l Int,
2272 \l String, \l UInt, or \l ULongLong; otherwise returns 0.
2274 If \a ok is non-null: \c{*}\c{ok} is set to true if the value could be
2275 converted to an int; otherwise \c{*}\c{ok} is set to false.
2277 \sa canConvert(), convert()
2279 qlonglong QVariant::toLongLong(bool *ok) const
2281 return qNumVariantToHelper<qlonglong>(d, handlerManager, ok, d.data.ll);
2285 Returns the variant as as an unsigned long long int if the
2286 variant has type() \l ULongLong, \l Bool, \l ByteArray, \l Char,
2287 \l Double, \l Int, \l LongLong, \l String, or \l UInt; otherwise
2290 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2291 converted to an int; otherwise \c{*}\a{ok} is set to false.
2293 \sa canConvert(), convert()
2295 qulonglong QVariant::toULongLong(bool *ok) const
2297 return qNumVariantToHelper<qulonglong>(d, handlerManager, ok, d.data.ull);
2301 Returns the variant as a bool if the variant has type() Bool.
2303 Returns true if the variant has type() \l Bool, \l Char, \l Double,
2304 \l Int, \l LongLong, \l UInt, or \l ULongLong and the value is
2305 non-zero, or if the variant has type \l String or \l ByteArray and
2306 its lower-case content is not empty, "0" or "false"; otherwise
2309 \sa canConvert(), convert()
2311 bool QVariant::toBool() const
2317 handlerManager[d.type]->convert(&d, Bool, &res, 0);
2323 Returns the variant as a double if the variant has type() \l
2324 Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
2325 UInt, or \l ULongLong; otherwise returns 0.0.
2327 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2328 converted to a double; otherwise \c{*}\a{ok} is set to false.
2330 \sa canConvert(), convert()
2332 double QVariant::toDouble(bool *ok) const
2334 return qNumVariantToHelper<double>(d, handlerManager, ok, d.data.d);
2338 Returns the variant as a float if the variant has type() \l
2339 Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
2340 UInt, or \l ULongLong; otherwise returns 0.0.
2344 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2345 converted to a double; otherwise \c{*}\a{ok} is set to false.
2347 \sa canConvert(), convert()
2349 float QVariant::toFloat(bool *ok) const
2351 return qNumVariantToHelper<float>(d, handlerManager, ok, d.data.f);
2355 Returns the variant as a qreal if the variant has type() \l
2356 Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
2357 UInt, or \l ULongLong; otherwise returns 0.0.
2361 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2362 converted to a double; otherwise \c{*}\a{ok} is set to false.
2364 \sa canConvert(), convert()
2366 qreal QVariant::toReal(bool *ok) const
2368 return qNumVariantToHelper<qreal>(d, handlerManager, ok, d.data.real);
2372 Returns the variant as a QVariantList if the variant has type()
2373 \l List or \l StringList; otherwise returns an empty list.
2375 \sa canConvert(), convert()
2377 QVariantList QVariant::toList() const
2379 return qVariantToHelper<QVariantList>(d, List, handlerManager);
2383 static const quint32 qCanConvertMatrix[QVariant::LastCoreType + 1] =
2387 /*Bool*/ 1 << QVariant::Double | 1 << QVariant::Int | 1 << QVariant::UInt
2388 | 1 << QVariant::LongLong | 1 << QVariant::ULongLong | 1 << QVariant::ByteArray
2389 | 1 << QVariant::String | 1 << QVariant::Char,
2391 /*Int*/ 1 << QVariant::UInt | 1 << QVariant::String | 1 << QVariant::Double
2392 | 1 << QVariant::Bool | 1 << QVariant::LongLong | 1 << QVariant::ULongLong
2393 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2395 /*UInt*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::Double
2396 | 1 << QVariant::Bool | 1 << QVariant::LongLong | 1 << QVariant::ULongLong
2397 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2399 /*LLong*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::Double
2400 | 1 << QVariant::Bool | 1 << QVariant::UInt | 1 << QVariant::ULongLong
2401 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2403 /*ULlong*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::Double
2404 | 1 << QVariant::Bool | 1 << QVariant::UInt | 1 << QVariant::LongLong
2405 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2407 /*double*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::ULongLong
2408 | 1 << QVariant::Bool | 1 << QVariant::UInt | 1 << QVariant::LongLong
2409 | 1 << QVariant::ByteArray,
2411 /*QChar*/ 1 << QVariant::Int | 1 << QVariant::UInt | 1 << QVariant::LongLong
2412 | 1 << QVariant::ULongLong,
2416 /*QList*/ 1 << QVariant::StringList,
2418 /*QString*/ 1 << QVariant::StringList | 1 << QVariant::ByteArray | 1 << QVariant::Int
2419 | 1 << QVariant::UInt | 1 << QVariant::Bool | 1 << QVariant::Double
2420 | 1 << QVariant::Date | 1 << QVariant::Time | 1 << QVariant::DateTime
2421 | 1 << QVariant::LongLong | 1 << QVariant::ULongLong | 1 << QVariant::Char
2422 | 1 << QVariant::Url | 1 << QVariant::Uuid,
2424 /*QStringList*/ 1 << QVariant::List | 1 << QVariant::String,
2426 /*QByteArray*/ 1 << QVariant::String | 1 << QVariant::Int | 1 << QVariant::UInt | 1 << QVariant::Bool
2427 | 1 << QVariant::Double | 1 << QVariant::LongLong | 1 << QVariant::ULongLong,
2431 /*QDate*/ 1 << QVariant::String | 1 << QVariant::DateTime,
2433 /*QTime*/ 1 << QVariant::String | 1 << QVariant::DateTime,
2435 /*QDateTime*/ 1 << QVariant::String | 1 << QVariant::Date,
2437 /*QUrl*/ 1 << QVariant::String,
2441 /*QRect*/ 1 << QVariant::RectF,
2443 /*QRectF*/ 1 << QVariant::Rect,
2445 /*QSize*/ 1 << QVariant::SizeF,
2447 /*QSizeF*/ 1 << QVariant::Size,
2449 /*QLine*/ 1 << QVariant::LineF,
2451 /*QLineF*/ 1 << QVariant::Line,
2453 /*QPoint*/ 1 << QVariant::PointF,
2455 /*QPointF*/ 1 << QVariant::Point,
2463 /*QUuid*/ 1 << QVariant::String
2467 Returns true if the variant's type can be cast to the requested
2468 type, \a t. Such casting is done automatically when calling the
2469 toInt(), toBool(), ... methods.
2471 The following casts are done automatically:
2474 \header \o Type \o Automatically Cast To
2475 \row \o \l Bool \o \l Char, \l Double, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2476 \row \o \l ByteArray \o \l Double, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2477 \row \o \l Char \o \l Bool, \l Int, \l UInt, \l LongLong, \l ULongLong
2478 \row \o \l Color \o \l String
2479 \row \o \l Date \o \l DateTime, \l String
2480 \row \o \l DateTime \o \l Date, \l String, \l Time
2481 \row \o \l Double \o \l Bool, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2482 \row \o \l Font \o \l String
2483 \row \o \l Int \o \l Bool, \l Char, \l Double, \l LongLong, \l String, \l UInt, \l ULongLong
2484 \row \o \l KeySequence \o \l Int, \l String
2485 \row \o \l List \o \l StringList (if the list's items can be converted to strings)
2486 \row \o \l LongLong \o \l Bool, \l ByteArray, \l Char, \l Double, \l Int, \l String, \l UInt, \l ULongLong
2487 \row \o \l Point \o PointF
2488 \row \o \l Rect \o RectF
2489 \row \o \l String \o \l Bool, \l ByteArray, \l Char, \l Color, \l Date, \l DateTime, \l Double,
2490 \l Font, \l Int, \l KeySequence, \l LongLong, \l StringList, \l Time, \l UInt,
2492 \row \o \l StringList \o \l List, \l String (if the list contains exactly one item)
2493 \row \o \l Time \o \l String
2494 \row \o \l UInt \o \l Bool, \l Char, \l Double, \l Int, \l LongLong, \l String, \l ULongLong
2495 \row \o \l ULongLong \o \l Bool, \l Char, \l Double, \l Int, \l LongLong, \l String, \l UInt
2500 bool QVariant::canConvert(Type t) const
2502 //we can treat floats as double
2503 //the reason for not doing it the "proper" way is that QMetaType::Float's value is 135,
2504 //which can't be handled by qCanConvertMatrix
2505 //In addition QVariant::Type doesn't have a Float value, so we're using QMetaType::Float
2506 const uint currentType = ((d.type == QMetaType::Float) ? QVariant::Double : d.type);
2507 if (uint(t) == uint(QMetaType::Float)) t = QVariant::Double;
2509 if (currentType == uint(t))
2512 if (currentType > QVariant::LastCoreType || t > QVariant::LastCoreType) {
2515 return currentType == QVariant::KeySequence
2516 || currentType == QMetaType::ULong
2517 || currentType == QMetaType::Long
2518 || currentType == QMetaType::UShort
2519 || currentType == QMetaType::UChar
2520 || currentType == QMetaType::Char
2521 || currentType == QMetaType::Short;
2522 case QVariant::Image:
2523 return currentType == QVariant::Pixmap || currentType == QVariant::Bitmap;
2524 case QVariant::Pixmap:
2525 return currentType == QVariant::Image || currentType == QVariant::Bitmap
2526 || currentType == QVariant::Brush;
2527 case QVariant::Bitmap:
2528 return currentType == QVariant::Pixmap || currentType == QVariant::Image;
2529 case QVariant::ByteArray:
2530 return currentType == QVariant::Color;
2531 case QVariant::String:
2532 return currentType == QVariant::KeySequence || currentType == QVariant::Font
2533 || currentType == QVariant::Color;
2534 case QVariant::KeySequence:
2535 return currentType == QVariant::String || currentType == QVariant::Int;
2536 case QVariant::Font:
2537 return currentType == QVariant::String;
2538 case QVariant::Color:
2539 return currentType == QVariant::String || currentType == QVariant::ByteArray
2540 || currentType == QVariant::Brush;
2541 case QVariant::Brush:
2542 return currentType == QVariant::Color || currentType == QVariant::Pixmap;
2543 case QMetaType::Long:
2544 case QMetaType::Char:
2545 case QMetaType::UChar:
2546 case QMetaType::ULong:
2547 case QMetaType::Short:
2548 case QMetaType::UShort:
2549 return qCanConvertMatrix[QVariant::Int] & (1 << currentType) || currentType == QVariant::Int;
2555 if(t == String && currentType == StringList)
2556 return v_cast<QStringList>(&d)->count() == 1;
2558 return qCanConvertMatrix[t] & (1 << currentType);
2562 Casts the variant to the requested type, \a t. If the cast cannot be
2563 done, the variant is cleared. Returns true if the current type of
2564 the variant was successfully cast; otherwise returns false.
2566 \warning For historical reasons, converting a null QVariant results
2567 in a null value of the desired type (e.g., an empty string for
2568 QString) and a result of false.
2570 \sa canConvert(), clear()
2573 bool QVariant::convert(Type t)
2575 if (d.type == uint(t))
2578 QVariant oldValue = *this;
2581 if (!oldValue.canConvert(t))
2585 if (oldValue.isNull())
2589 if (!handlerManager[d.type]->convert(&oldValue.d, t, data(), &isOk))
2596 \fn convert(const int type, void *ptr) const
2598 Created for qvariant_cast() usage
2600 bool QVariant::convert(const int type, void *ptr) const
2602 Q_ASSERT(type < int(QMetaType::User));
2603 return handlerManager[type]->convert(&d, QVariant::Type(type), ptr, 0);
2608 \fn bool operator==(const QVariant &v1, const QVariant &v2)
2612 Returns true if \a v1 and \a v2 are equal; otherwise returns false.
2614 \warning This function doesn't support custom types registered
2615 with qRegisterMetaType().
2618 \fn bool operator!=(const QVariant &v1, const QVariant &v2)
2622 Returns false if \a v1 and \a v2 are equal; otherwise returns true.
2624 \warning This function doesn't support custom types registered
2625 with qRegisterMetaType().
2628 /*! \fn bool QVariant::operator==(const QVariant &v) const
2630 Compares this QVariant with \a v and returns true if they are
2631 equal; otherwise returns false.
2633 In the case of custom types, their equalness operators are not called.
2634 Instead the values' addresses are compared.
2638 \fn bool QVariant::operator!=(const QVariant &v) const
2640 Compares this QVariant with \a v and returns true if they are not
2641 equal; otherwise returns false.
2643 \warning This function doesn't support custom types registered
2644 with qRegisterMetaType().
2647 static bool qIsNumericType(uint tp)
2649 return (tp >= QVariant::Bool && tp <= QVariant::Double)
2650 || (tp >= QMetaType::Long && tp <= QMetaType::Float);
2653 static bool qIsFloatingPoint(uint tp)
2655 return tp == QVariant::Double || tp == QMetaType::Float;
2660 bool QVariant::cmp(const QVariant &v) const
2663 if (d.type != v2.d.type) {
2664 if (qIsNumericType(d.type) && qIsNumericType(v.d.type)) {
2665 if (qIsFloatingPoint(d.type) || qIsFloatingPoint(v.d.type))
2666 return qFuzzyCompare(toReal(), v.toReal());
2668 return toLongLong() == v.toLongLong();
2670 if (!v2.canConvert(Type(d.type)) || !v2.convert(Type(d.type)))
2673 return handlerManager[d.type]->compare(&d, &v2.d);
2679 const void *QVariant::constData() const
2681 return d.is_shared ? d.data.shared->ptr : reinterpret_cast<const void *>(&d.data.ptr);
2685 \fn const void* QVariant::data() const
2691 void* QVariant::data()
2694 return const_cast<void *>(constData());
2699 Returns true if this is a NULL variant, false otherwise.
2701 bool QVariant::isNull() const
2703 return handlerManager[d.type]->isNull(&d);
2706 #ifndef QT_NO_DEBUG_STREAM
2707 QDebug operator<<(QDebug dbg, const QVariant &v)
2709 #ifndef Q_BROKEN_DEBUG_STREAM
2710 dbg.nospace() << "QVariant(" << v.typeName() << ", ";
2711 handlerManager[v.d.type]->debugStream(dbg, v);
2712 dbg.nospace() << ')';
2715 qWarning("This compiler doesn't support streaming QVariant to QDebug");
2721 QDebug operator<<(QDebug dbg, const QVariant::Type p)
2723 #ifndef Q_BROKEN_DEBUG_STREAM
2724 dbg.nospace() << "QVariant::" << QVariant::typeToName(p);
2727 qWarning("This compiler doesn't support streaming QVariant::Type to QDebug");
2735 /*! \fn void QVariant::setValue(const T &value)
2737 Stores a copy of \a value. If \c{T} is a type that QVariant
2738 doesn't support, QMetaType is used to store the value. A compile
2739 error will occur if QMetaType doesn't handle the type.
2743 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 4
2745 \sa value(), fromValue(), canConvert()
2748 /*! \fn T QVariant::value() const
2750 Returns the stored value converted to the template type \c{T}.
2751 Call canConvert() to find out whether a type can be converted.
2752 If the value cannot be converted, \l{default-constructed value}
2755 If the type \c{T} is supported by QVariant, this function behaves
2756 exactly as toString(), toInt() etc.
2760 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 5
2762 \sa setValue(), fromValue(), canConvert()
2765 /*! \fn bool QVariant::canConvert() const
2767 Returns true if the variant can be converted to the template type \c{T},
2772 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 6
2777 /*! \fn static QVariant QVariant::fromValue(const T &value)
2779 Returns a QVariant containing a copy of \a value. Behaves
2780 exactly like setValue() otherwise.
2784 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 7
2786 \note If you are working with custom types, you should use
2787 the Q_DECLARE_METATYPE() macro to register your custom type.
2789 \sa setValue(), value()
2793 \fn QVariant qVariantFromValue(const T &value)
2797 Returns a variant containing a copy of the given \a value
2798 with template type \c{T}.
2800 This function is equivalent to QVariant::fromValue(\a value).
2802 \note This function was provided as a workaround for MSVC 6
2803 which did not support member template functions. It is advised
2804 to use the other form in new code.
2806 For example, a QObject pointer can be stored in a variant with the
2809 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 8
2811 \sa QVariant::fromValue()
2814 /*! \fn void qVariantSetValue(QVariant &variant, const T &value)
2818 Sets the contents of the given \a variant to a copy of the
2819 \a value with the specified template type \c{T}.
2821 This function is equivalent to QVariant::setValue(\a value).
2823 \note This function was provided as a workaround for MSVC 6
2824 which did not support member template functions. It is advised
2825 to use the other form in new code.
2827 \sa QVariant::setValue()
2831 \fn T qvariant_cast(const QVariant &value)
2834 Returns the given \a value converted to the template type \c{T}.
2836 This function is equivalent to QVariant::value().
2838 \sa QVariant::value()
2841 /*! \fn T qVariantValue(const QVariant &value)
2845 Returns the given \a value converted to the template type \c{T}.
2847 This function is equivalent to
2848 \l{QVariant::value()}{QVariant::value}<T>(\a value).
2850 \note This function was provided as a workaround for MSVC 6
2851 which did not support member template functions. It is advised
2852 to use the other form in new code.
2854 \sa QVariant::value(), qvariant_cast()
2857 /*! \fn bool qVariantCanConvert(const QVariant &value)
2861 Returns true if the given \a value can be converted to the
2862 template type specified; otherwise returns false.
2864 This function is equivalent to QVariant::canConvert(\a value).
2866 \note This function was provided as a workaround for MSVC 6
2867 which did not support member template functions. It is advised
2868 to use the other form in new code.
2870 \sa QVariant::canConvert()
2874 \typedef QVariantList
2877 Synonym for QList<QVariant>.
2881 \typedef QVariantMap
2884 Synonym for QMap<QString, QVariant>.
2888 \typedef QVariantHash
2892 Synonym for QHash<QString, QVariant>.
2896 \typedef QVariant::DataPtr
2901 \fn DataPtr &QVariant::data_ptr()