1 /****************************************************************************
3 ** Copyright (C) 2011 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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
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"
55 #include "private/qvariant_p.h"
57 #ifndef QT_NO_GEOM_VARIANT
75 static void construct(QVariant::Private *x, const void *copy)
80 case QVariant::String:
81 v_construct<QString>(x, copy);
84 v_construct<QChar>(x, copy);
86 case QVariant::StringList:
87 v_construct<QStringList>(x, copy);
90 v_construct<QVariantMap>(x, copy);
93 v_construct<QVariantHash>(x, copy);
96 v_construct<QVariantList>(x, copy);
99 v_construct<QDate>(x, copy);
102 v_construct<QTime>(x, copy);
104 case QVariant::DateTime:
105 v_construct<QDateTime>(x, copy);
107 case QVariant::ByteArray:
108 v_construct<QByteArray>(x, copy);
110 case QVariant::BitArray:
111 v_construct<QBitArray>(x, copy);
113 #ifndef QT_NO_GEOM_VARIANT
115 v_construct<QSize>(x, copy);
117 case QVariant::SizeF:
118 v_construct<QSizeF>(x, copy);
121 v_construct<QRect>(x, copy);
123 case QVariant::LineF:
124 v_construct<QLineF>(x, copy);
127 v_construct<QLine>(x, copy);
129 case QVariant::RectF:
130 v_construct<QRectF>(x, copy);
132 case QVariant::Point:
133 v_construct<QPoint>(x, copy);
135 case QVariant::PointF:
136 v_construct<QPointF>(x, copy);
140 v_construct<QUrl>(x, copy);
142 case QVariant::Locale:
143 v_construct<QLocale>(x, copy);
146 case QVariant::RegExp:
147 v_construct<QRegExp>(x, copy);
150 #ifndef QT_BOOTSTRAPPED
151 case QVariant::EasingCurve:
152 v_construct<QEasingCurve>(x, copy);
156 x->data.i = copy ? *static_cast<const int *>(copy) : 0;
159 x->data.u = copy ? *static_cast<const uint *>(copy) : 0u;
162 x->data.b = copy ? *static_cast<const bool *>(copy) : false;
164 case QVariant::Double:
165 x->data.d = copy ? *static_cast<const double*>(copy) : 0.0;
167 case QMetaType::Float:
168 x->data.f = copy ? *static_cast<const float*>(copy) : 0.0f;
170 case QMetaType::QObjectStar:
171 x->data.o = copy ? *static_cast<QObject *const*>(copy) : 0;
173 case QVariant::LongLong:
174 x->data.ll = copy ? *static_cast<const qlonglong *>(copy) : Q_INT64_C(0);
176 case QVariant::ULongLong:
177 x->data.ull = copy ? *static_cast<const qulonglong *>(copy) : Q_UINT64_C(0);
179 case QVariant::Invalid:
180 case QVariant::UserType:
183 void *ptr = QMetaType::construct(x->type, copy);
185 x->type = QVariant::Invalid;
188 x->data.shared = new QVariant::PrivateShared(ptr);
195 static void clear(QVariant::Private *d)
198 case QVariant::String:
204 case QVariant::StringList:
205 v_clear<QStringList>(d);
208 v_clear<QVariantMap>(d);
211 v_clear<QVariantHash>(d);
214 v_clear<QVariantList>(d);
222 case QVariant::DateTime:
223 v_clear<QDateTime>(d);
225 case QVariant::ByteArray:
226 v_clear<QByteArray>(d);
228 case QVariant::BitArray:
229 v_clear<QBitArray>(d);
231 #ifndef QT_NO_GEOM_VARIANT
232 case QVariant::Point:
235 case QVariant::PointF:
241 case QVariant::SizeF:
247 case QVariant::LineF:
253 case QVariant::RectF:
260 case QVariant::Locale:
264 case QVariant::RegExp:
268 #ifndef QT_BOOTSTRAPPED
269 case QVariant::EasingCurve:
270 v_clear<QEasingCurve>(d);
273 case QVariant::LongLong:
274 case QVariant::ULongLong:
275 case QVariant::Double:
276 case QMetaType::Float:
277 case QMetaType::QObjectStar:
279 case QVariant::Invalid:
280 case QVariant::UserType:
286 QMetaType::destroy(d->type, d->data.shared->ptr);
287 delete d->data.shared;
291 d->type = QVariant::Invalid;
293 d->is_shared = false;
296 static bool isNull(const QVariant::Private *d)
299 case QVariant::String:
300 return v_cast<QString>(d)->isNull();
302 return v_cast<QChar>(d)->isNull();
304 return v_cast<QDate>(d)->isNull();
306 return v_cast<QTime>(d)->isNull();
307 case QVariant::DateTime:
308 return v_cast<QDateTime>(d)->isNull();
309 case QVariant::ByteArray:
310 return v_cast<QByteArray>(d)->isNull();
311 case QVariant::BitArray:
312 return v_cast<QBitArray>(d)->isNull();
313 #ifndef QT_NO_GEOM_VARIANT
315 return v_cast<QSize>(d)->isNull();
316 case QVariant::SizeF:
317 return v_cast<QSizeF>(d)->isNull();
319 return v_cast<QRect>(d)->isNull();
321 return v_cast<QLine>(d)->isNull();
322 case QVariant::LineF:
323 return v_cast<QLineF>(d)->isNull();
324 case QVariant::RectF:
325 return v_cast<QRectF>(d)->isNull();
326 case QVariant::Point:
327 return v_cast<QPoint>(d)->isNull();
328 case QVariant::PointF:
329 return v_cast<QPointF>(d)->isNull();
331 #ifndef QT_BOOTSTRAPPED
332 case QVariant::EasingCurve:
335 case QVariant::Locale:
336 case QVariant::RegExp:
337 case QVariant::StringList:
341 case QVariant::Invalid:
342 case QVariant::UserType:
345 case QVariant::LongLong:
346 case QVariant::ULongLong:
348 case QVariant::Double:
349 case QMetaType::Float:
350 case QMetaType::QObjectStar:
360 We cannot use v_cast() for QMetaType's numeric types because they're smaller than QVariant::Private::Data,
361 which in turns makes v_cast() believe the value is stored in d->data.c. But
362 it's not, since we're a QMetaType type.
365 inline bool compareNumericMetaType(const QVariant::Private *const a, const QVariant::Private *const b)
367 return *static_cast<const T *>(a->data.shared->ptr) == *static_cast<const T *>(b->data.shared->ptr);
373 Compares \a a to \a b. The caller guarantees that \a a and \a b
374 are of the same type.
376 static bool compare(const QVariant::Private *a, const QVariant::Private *b)
380 return *v_cast<QVariantList>(a) == *v_cast<QVariantList>(b);
381 case QVariant::Map: {
382 const QVariantMap *m1 = v_cast<QVariantMap>(a);
383 const QVariantMap *m2 = v_cast<QVariantMap>(b);
384 if (m1->count() != m2->count())
386 QVariantMap::ConstIterator it = m1->constBegin();
387 QVariantMap::ConstIterator it2 = m2->constBegin();
388 while (it != m1->constEnd()) {
389 if (*it != *it2 || it.key() != it2.key())
397 return *v_cast<QVariantHash>(a) == *v_cast<QVariantHash>(b);
398 case QVariant::String:
399 return *v_cast<QString>(a) == *v_cast<QString>(b);
401 return *v_cast<QChar>(a) == *v_cast<QChar>(b);
402 case QVariant::StringList:
403 return *v_cast<QStringList>(a) == *v_cast<QStringList>(b);
404 #ifndef QT_NO_GEOM_VARIANT
406 return *v_cast<QSize>(a) == *v_cast<QSize>(b);
407 case QVariant::SizeF:
408 return *v_cast<QSizeF>(a) == *v_cast<QSizeF>(b);
410 return *v_cast<QRect>(a) == *v_cast<QRect>(b);
412 return *v_cast<QLine>(a) == *v_cast<QLine>(b);
413 case QVariant::LineF:
414 return *v_cast<QLineF>(a) == *v_cast<QLineF>(b);
415 case QVariant::RectF:
416 return *v_cast<QRectF>(a) == *v_cast<QRectF>(b);
417 case QVariant::Point:
418 return *v_cast<QPoint>(a) == *v_cast<QPoint>(b);
419 case QVariant::PointF:
420 return *v_cast<QPointF>(a) == *v_cast<QPointF>(b);
423 return *v_cast<QUrl>(a) == *v_cast<QUrl>(b);
424 case QVariant::Locale:
425 return *v_cast<QLocale>(a) == *v_cast<QLocale>(b);
427 case QVariant::RegExp:
428 return *v_cast<QRegExp>(a) == *v_cast<QRegExp>(b);
431 return a->data.i == b->data.i;
433 return a->data.u == b->data.u;
434 case QVariant::LongLong:
435 return a->data.ll == b->data.ll;
436 case QVariant::ULongLong:
437 return a->data.ull == b->data.ull;
439 return a->data.b == b->data.b;
440 case QVariant::Double:
441 return a->data.d == b->data.d;
442 case QMetaType::Float:
443 return a->data.f == b->data.f;
444 case QMetaType::QObjectStar:
445 return a->data.o == b->data.o;
447 return *v_cast<QDate>(a) == *v_cast<QDate>(b);
449 return *v_cast<QTime>(a) == *v_cast<QTime>(b);
450 case QVariant::DateTime:
451 return *v_cast<QDateTime>(a) == *v_cast<QDateTime>(b);
452 #ifndef QT_BOOTSTRAPPED
453 case QVariant::EasingCurve:
454 return *v_cast<QEasingCurve>(a) == *v_cast<QEasingCurve>(b);
456 case QVariant::ByteArray:
457 return *v_cast<QByteArray>(a) == *v_cast<QByteArray>(b);
458 case QVariant::BitArray:
459 return *v_cast<QBitArray>(a) == *v_cast<QBitArray>(b);
460 case QVariant::Invalid:
462 case QMetaType::Long:
463 return compareNumericMetaType<long>(a, b);
464 case QMetaType::ULong:
465 return compareNumericMetaType<ulong>(a, b);
466 case QMetaType::Short:
467 return compareNumericMetaType<short>(a, b);
468 case QMetaType::UShort:
469 return compareNumericMetaType<ushort>(a, b);
470 case QMetaType::UChar:
471 return compareNumericMetaType<uchar>(a, b);
472 case QMetaType::Char:
473 return compareNumericMetaType<char>(a, b);
477 if (!QMetaType::isRegistered(a->type))
478 qFatal("QVariant::compare: type %d unknown to QVariant.", a->type);
480 const void *a_ptr = a->is_shared ? a->data.shared->ptr : &(a->data.ptr);
481 const void *b_ptr = b->is_shared ? b->data.shared->ptr : &(b->data.ptr);
483 /* The reason we cannot place this test in a case branch above for the types
484 * QMetaType::VoidStar, QMetaType::QObjectStar and so forth, is that it wouldn't include
485 * user defined pointer types. */
486 const char *const typeName = QMetaType::typeName(a->type);
487 uint typeNameLen = qstrlen(typeName);
488 if (typeNameLen > 0 && typeName[typeNameLen - 1] == '*')
489 return *static_cast<void *const *>(a_ptr) == *static_cast<void *const *>(b_ptr);
491 if (a->is_null && b->is_null)
494 return a_ptr == b_ptr;
500 static qlonglong qMetaTypeNumber(const QVariant::Private *d)
505 case QMetaType::LongLong:
507 case QMetaType::Char:
508 return qlonglong(*static_cast<signed char *>(d->data.shared->ptr));
509 case QMetaType::Short:
510 return qlonglong(*static_cast<short *>(d->data.shared->ptr));
511 case QMetaType::Long:
512 return qlonglong(*static_cast<long *>(d->data.shared->ptr));
513 case QMetaType::Float:
514 return qRound64(d->data.f);
515 case QVariant::Double:
516 return qRound64(d->data.d);
522 static qulonglong qMetaTypeUNumber(const QVariant::Private *d)
527 case QVariant::ULongLong:
529 case QMetaType::UChar:
530 return qulonglong(*static_cast<unsigned char *>(d->data.shared->ptr));
531 case QMetaType::UShort:
532 return qulonglong(*static_cast<ushort *>(d->data.shared->ptr));
533 case QMetaType::ULong:
534 return qulonglong(*static_cast<ulong *>(d->data.shared->ptr));
540 static qlonglong qConvertToNumber(const QVariant::Private *d, bool *ok)
544 switch (uint(d->type)) {
545 case QVariant::String:
546 return v_cast<QString>(d)->toLongLong(ok);
548 return v_cast<QChar>(d)->unicode();
549 case QVariant::ByteArray:
550 return v_cast<QByteArray>(d)->toLongLong(ok);
552 return qlonglong(d->data.b);
553 case QVariant::Double:
555 case QMetaType::Char:
556 case QMetaType::Short:
557 case QMetaType::Long:
558 case QMetaType::Float:
559 case QMetaType::LongLong:
560 return qMetaTypeNumber(d);
561 case QVariant::ULongLong:
563 case QMetaType::UChar:
564 case QMetaType::UShort:
565 case QMetaType::ULong:
566 return qlonglong(qMetaTypeUNumber(d));
573 static qulonglong qConvertToUnsignedNumber(const QVariant::Private *d, bool *ok)
577 switch (uint(d->type)) {
578 case QVariant::String:
579 return v_cast<QString>(d)->toULongLong(ok);
581 return v_cast<QChar>(d)->unicode();
582 case QVariant::ByteArray:
583 return v_cast<QByteArray>(d)->toULongLong(ok);
585 return qulonglong(d->data.b);
586 case QVariant::Double:
588 case QMetaType::Char:
589 case QMetaType::Short:
590 case QMetaType::Long:
591 case QMetaType::Float:
592 case QMetaType::LongLong:
593 return qulonglong(qMetaTypeNumber(d));
594 case QVariant::ULongLong:
596 case QMetaType::UChar:
597 case QMetaType::UShort:
598 case QMetaType::ULong:
599 return qMetaTypeUNumber(d);
603 return Q_UINT64_C(0);
606 template<typename TInput, typename LiteralWrapper>
607 inline bool qt_convertToBool(const QVariant::Private *const d)
609 TInput str = v_cast<TInput>(d)->toLower();
610 return !(str == LiteralWrapper("0") || str == LiteralWrapper("false") || str.isEmpty());
616 Converts \a d to type \a t, which is placed in \a result.
618 static bool convert(const QVariant::Private *d, QVariant::Type t, void *result, bool *ok)
620 Q_ASSERT(d->type != uint(t));
630 case QVariant::String:
631 *static_cast<QUrl *>(result) = QUrl(*v_cast<QString>(d));
637 case QVariant::String: {
638 QString *str = static_cast<QString *>(result);
641 *str = QString(*v_cast<QChar>(d));
643 case QMetaType::Char:
644 case QMetaType::UChar:
645 *str = QChar::fromAscii(*static_cast<char *>(d->data.shared->ptr));
647 case QMetaType::Short:
648 case QMetaType::Long:
650 case QVariant::LongLong:
651 *str = QString::number(qMetaTypeNumber(d));
654 case QVariant::ULongLong:
655 case QMetaType::UShort:
656 case QMetaType::ULong:
657 *str = QString::number(qMetaTypeUNumber(d));
659 case QMetaType::Float:
660 *str = QString::number(d->data.f, 'g', FLT_DIG);
662 case QVariant::Double:
663 *str = QString::number(d->data.d, 'g', DBL_DIG);
665 #if !defined(QT_NO_DATESTRING)
667 *str = v_cast<QDate>(d)->toString(Qt::ISODate);
670 *str = v_cast<QTime>(d)->toString(Qt::ISODate);
672 case QVariant::DateTime:
673 *str = v_cast<QDateTime>(d)->toString(Qt::ISODate);
677 *str = QLatin1String(d->data.b ? "true" : "false");
679 case QVariant::ByteArray:
680 *str = QString::fromAscii(v_cast<QByteArray>(d)->constData());
682 case QVariant::StringList:
683 if (v_cast<QStringList>(d)->count() == 1)
684 *str = v_cast<QStringList>(d)->at(0);
687 *str = v_cast<QUrl>(d)->toString();
694 case QVariant::Char: {
695 QChar *c = static_cast<QChar *>(result);
698 case QVariant::LongLong:
699 case QMetaType::Char:
700 case QMetaType::Short:
701 case QMetaType::Long:
702 case QMetaType::Float:
703 *c = QChar(ushort(qMetaTypeNumber(d)));
706 case QVariant::ULongLong:
707 case QMetaType::UChar:
708 case QMetaType::UShort:
709 case QMetaType::ULong:
710 *c = QChar(ushort(qMetaTypeUNumber(d)));
717 #ifndef QT_NO_GEOM_VARIANT
718 case QVariant::Size: {
719 QSize *s = static_cast<QSize *>(result);
721 case QVariant::SizeF:
722 *s = v_cast<QSizeF>(d)->toSize();
730 case QVariant::SizeF: {
731 QSizeF *s = static_cast<QSizeF *>(result);
734 *s = QSizeF(*(v_cast<QSize>(d)));
742 case QVariant::Line: {
743 QLine *s = static_cast<QLine *>(result);
745 case QVariant::LineF:
746 *s = v_cast<QLineF>(d)->toLine();
754 case QVariant::LineF: {
755 QLineF *s = static_cast<QLineF *>(result);
758 *s = QLineF(*(v_cast<QLine>(d)));
766 case QVariant::StringList:
767 if (d->type == QVariant::List) {
768 QStringList *slst = static_cast<QStringList *>(result);
769 const QVariantList *list = v_cast<QVariantList >(d);
770 for (int i = 0; i < list->size(); ++i)
771 slst->append(list->at(i).toString());
772 } else if (d->type == QVariant::String) {
773 QStringList *slst = static_cast<QStringList *>(result);
774 *slst = QStringList(*v_cast<QString>(d));
779 case QVariant::Date: {
780 QDate *dt = static_cast<QDate *>(result);
781 if (d->type == QVariant::DateTime)
782 *dt = v_cast<QDateTime>(d)->date();
783 #ifndef QT_NO_DATESTRING
784 else if (d->type == QVariant::String)
785 *dt = QDate::fromString(*v_cast<QString>(d), Qt::ISODate);
790 return dt->isValid();
792 case QVariant::Time: {
793 QTime *t = static_cast<QTime *>(result);
795 case QVariant::DateTime:
796 *t = v_cast<QDateTime>(d)->time();
798 #ifndef QT_NO_DATESTRING
799 case QVariant::String:
800 *t = QTime::fromString(*v_cast<QString>(d), Qt::ISODate);
808 case QVariant::DateTime: {
809 QDateTime *dt = static_cast<QDateTime *>(result);
811 #ifndef QT_NO_DATESTRING
812 case QVariant::String:
813 *dt = QDateTime::fromString(*v_cast<QString>(d), Qt::ISODate);
817 *dt = QDateTime(*v_cast<QDate>(d));
822 return dt->isValid();
824 case QVariant::ByteArray: {
825 QByteArray *ba = static_cast<QByteArray *>(result);
827 case QVariant::String:
828 *ba = v_cast<QString>(d)->toAscii();
830 case QVariant::Double:
831 *ba = QByteArray::number(d->data.d, 'g', DBL_DIG);
833 case QMetaType::Float:
834 *ba = QByteArray::number(d->data.f, 'g', FLT_DIG);
836 case QMetaType::Char:
837 case QMetaType::UChar:
838 *ba = QByteArray(1, *static_cast<char *>(d->data.shared->ptr));
841 case QVariant::LongLong:
842 case QMetaType::Short:
843 case QMetaType::Long:
844 *ba = QByteArray::number(qMetaTypeNumber(d));
847 case QVariant::ULongLong:
848 case QMetaType::UShort:
849 case QMetaType::ULong:
850 *ba = QByteArray::number(qMetaTypeUNumber(d));
853 *ba = QByteArray(d->data.b ? "true" : "false");
860 case QMetaType::Short:
861 *static_cast<short *>(result) = short(qConvertToNumber(d, ok));
863 case QMetaType::Long:
864 *static_cast<long *>(result) = long(qConvertToNumber(d, ok));
866 case QMetaType::UShort:
867 *static_cast<ushort *>(result) = ushort(qConvertToUnsignedNumber(d, ok));
869 case QMetaType::ULong:
870 *static_cast<ulong *>(result) = ulong(qConvertToUnsignedNumber(d, ok));
873 *static_cast<int *>(result) = int(qConvertToNumber(d, ok));
876 *static_cast<uint *>(result) = uint(qConvertToUnsignedNumber(d, ok));
878 case QVariant::LongLong:
879 *static_cast<qlonglong *>(result) = qConvertToNumber(d, ok);
881 case QVariant::ULongLong: {
882 *static_cast<qulonglong *>(result) = qConvertToUnsignedNumber(d, ok);
885 case QMetaType::UChar: {
886 *static_cast<uchar *>(result) = qConvertToUnsignedNumber(d, ok);
889 case QVariant::Bool: {
890 bool *b = static_cast<bool *>(result);
892 case QVariant::ByteArray:
893 *b = qt_convertToBool<QByteArray, QByteArray>(d);
895 case QVariant::String:
896 *b = qt_convertToBool<QString, QLatin1String>(d);
899 *b = !v_cast<QChar>(d)->isNull();
901 case QVariant::Double:
903 case QVariant::LongLong:
904 case QMetaType::Char:
905 case QMetaType::Short:
906 case QMetaType::Long:
907 case QMetaType::Float:
908 *b = qMetaTypeNumber(d) != Q_INT64_C(0);
911 case QVariant::ULongLong:
912 case QMetaType::UChar:
913 case QMetaType::UShort:
914 case QMetaType::ULong:
915 *b = qMetaTypeUNumber(d) != Q_UINT64_C(0);
923 case QVariant::Double: {
924 double *f = static_cast<double *>(result);
926 case QVariant::String:
927 *f = v_cast<QString>(d)->toDouble(ok);
929 case QVariant::ByteArray:
930 *f = v_cast<QByteArray>(d)->toDouble(ok);
933 *f = double(d->data.b);
935 case QMetaType::Float:
936 *f = double(d->data.f);
938 case QVariant::LongLong:
940 case QMetaType::Char:
941 case QMetaType::Short:
942 case QMetaType::Long:
943 *f = double(qMetaTypeNumber(d));
946 case QVariant::ULongLong:
947 case QMetaType::UChar:
948 case QMetaType::UShort:
949 case QMetaType::ULong:
950 *f = double(qMetaTypeUNumber(d));
958 case QMetaType::Float: {
959 float *f = static_cast<float *>(result);
961 case QVariant::String:
962 *f = v_cast<QString>(d)->toFloat(ok);
964 case QVariant::ByteArray:
965 *f = v_cast<QByteArray>(d)->toFloat(ok);
968 *f = float(d->data.b);
970 case QVariant::Double:
971 *f = float(d->data.d);
973 case QVariant::LongLong:
975 case QMetaType::Char:
976 case QMetaType::Short:
977 case QMetaType::Long:
978 *f = float(qMetaTypeNumber(d));
981 case QVariant::ULongLong:
982 case QMetaType::UChar:
983 case QMetaType::UShort:
984 case QMetaType::ULong:
985 *f = float(qMetaTypeUNumber(d));
994 if (d->type == QVariant::StringList) {
995 QVariantList *lst = static_cast<QVariantList *>(result);
996 const QStringList *slist = v_cast<QStringList>(d);
997 for (int i = 0; i < slist->size(); ++i)
998 lst->append(QVariant(slist->at(i)));
999 } else if (qstrcmp(QMetaType::typeName(d->type), "QList<QVariant>") == 0) {
1000 *static_cast<QVariantList *>(result) =
1001 *static_cast<QList<QVariant> *>(d->data.shared->ptr);
1007 if (qstrcmp(QMetaType::typeName(d->type), "QMap<QString, QVariant>") == 0) {
1008 *static_cast<QVariantMap *>(result) =
1009 *static_cast<QMap<QString, QVariant> *>(d->data.shared->ptr);
1014 case QVariant::Hash:
1015 if (qstrcmp(QMetaType::typeName(d->type), "QHash<QString, QVariant>") == 0) {
1016 *static_cast<QVariantHash *>(result) =
1017 *static_cast<QHash<QString, QVariant> *>(d->data.shared->ptr);
1022 #ifndef QT_NO_GEOM_VARIANT
1023 case QVariant::Rect:
1024 if (d->type == QVariant::RectF)
1025 *static_cast<QRect *>(result) = (v_cast<QRectF>(d))->toRect();
1029 case QVariant::RectF:
1030 if (d->type == QVariant::Rect)
1031 *static_cast<QRectF *>(result) = *v_cast<QRect>(d);
1035 case QVariant::PointF:
1036 if (d->type == QVariant::Point)
1037 *static_cast<QPointF *>(result) = *v_cast<QPoint>(d);
1041 case QVariant::Point:
1042 if (d->type == QVariant::PointF)
1043 *static_cast<QPoint *>(result) = (v_cast<QPointF>(d))->toPoint();
1047 case QMetaType::Char:
1049 *static_cast<qint8 *>(result) = qint8(qConvertToNumber(d, ok));
1059 #if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)
1060 static void streamDebug(QDebug dbg, const QVariant &v)
1062 switch (v.userType()) {
1064 dbg.nospace() << v.toInt();
1066 case QVariant::UInt:
1067 dbg.nospace() << v.toUInt();
1069 case QVariant::LongLong:
1070 dbg.nospace() << v.toLongLong();
1072 case QVariant::ULongLong:
1073 dbg.nospace() << v.toULongLong();
1075 case QMetaType::Float:
1076 dbg.nospace() << v.toFloat();
1078 case QMetaType::QObjectStar:
1079 dbg.nospace() << qvariant_cast<QObject *>(v);
1081 case QVariant::Double:
1082 dbg.nospace() << v.toDouble();
1084 case QVariant::Bool:
1085 dbg.nospace() << v.toBool();
1087 case QVariant::String:
1088 dbg.nospace() << v.toString();
1090 case QVariant::Char:
1091 dbg.nospace() << v.toChar();
1093 case QVariant::StringList:
1094 dbg.nospace() << v.toStringList();
1097 dbg.nospace() << v.toMap();
1099 case QVariant::Hash:
1100 dbg.nospace() << v.toHash();
1102 case QVariant::List:
1103 dbg.nospace() << v.toList();
1105 case QVariant::Date:
1106 dbg.nospace() << v.toDate();
1108 case QVariant::Time:
1109 dbg.nospace() << v.toTime();
1111 case QVariant::DateTime:
1112 dbg.nospace() << v.toDateTime();
1114 #ifndef QT_BOOTSTRAPPED
1115 case QVariant::EasingCurve:
1116 dbg.nospace() << v.toEasingCurve();
1119 case QVariant::ByteArray:
1120 dbg.nospace() << v.toByteArray();
1123 dbg.nospace() << v.toUrl();
1125 #ifndef QT_NO_GEOM_VARIANT
1126 case QVariant::Point:
1127 dbg.nospace() << v.toPoint();
1129 case QVariant::PointF:
1130 dbg.nospace() << v.toPointF();
1132 case QVariant::Rect:
1133 dbg.nospace() << v.toRect();
1135 case QVariant::Size:
1136 dbg.nospace() << v.toSize();
1138 case QVariant::SizeF:
1139 dbg.nospace() << v.toSizeF();
1141 case QVariant::Line:
1142 dbg.nospace() << v.toLine();
1144 case QVariant::LineF:
1145 dbg.nospace() << v.toLineF();
1147 case QVariant::RectF:
1148 dbg.nospace() << v.toRectF();
1151 case QVariant::BitArray:
1152 //dbg.nospace() << v.toBitArray();
1160 const QVariant::Handler qt_kernel_variant_handler = {
1164 #ifndef QT_NO_DATASTREAM
1171 #if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)
1178 Q_CORE_EXPORT const QVariant::Handler *qcoreVariantHandler()
1180 return &qt_kernel_variant_handler;
1184 const QVariant::Handler *QVariant::handler = &qt_kernel_variant_handler;
1188 \brief The QVariant class acts like a union for the most common Qt data types.
1190 \ingroup objectmodel
1194 Because C++ forbids unions from including types that have
1195 non-default constructors or destructors, most interesting Qt
1196 classes cannot be used in unions. Without QVariant, this would be
1197 a problem for QObject::property() and for database work, etc.
1199 A QVariant object holds a single value of a single type() at a
1200 time. (Some type()s are multi-valued, for example a string list.)
1201 You can find out what type, T, the variant holds, convert it to a
1202 different type using convert(), get its value using one of the
1203 toT() functions (e.g., toSize()) and check whether the type can
1204 be converted to a particular type using canConvert().
1206 The methods named toT() (e.g., toInt(), toString()) are const. If
1207 you ask for the stored type, they return a copy of the stored
1208 object. If you ask for a type that can be generated from the
1209 stored type, toT() copies and converts and leaves the object
1210 itself unchanged. If you ask for a type that cannot be generated
1211 from the stored type, the result depends on the type; see the
1212 function documentation for details.
1214 Here is some example code to demonstrate the use of QVariant:
1216 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 0
1218 You can even store QList<QVariant> and QMap<QString, QVariant>
1219 values in a variant, so you can easily construct arbitrarily
1220 complex data structures of arbitrary types. This is very powerful
1221 and versatile, but may prove less memory and speed efficient than
1222 storing specific types in standard data structures.
1224 QVariant also supports the notion of null values, where you can
1225 have a defined type with no value set. However, note that QVariant
1226 types can only be cast when they have had a value set.
1228 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 1
1230 QVariant can be extended to support other types than those
1231 mentioned in the \l Type enum. See the \l QMetaType documentation
1234 \section1 A Note on GUI Types
1236 Because QVariant is part of the QtCore library, it cannot provide
1237 conversion functions to data types defined in QtGui, such as
1238 QColor, QImage, and QPixmap. In other words, there is no \c
1239 toColor() function. Instead, you can use the QVariant::value() or
1240 the qvariant_cast() template function. For example:
1242 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 2
1244 The inverse conversion (e.g., from QColor to QVariant) is
1245 automatic for all data types supported by QVariant, including
1248 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 3
1250 \section1 Using canConvert() and convert() Consecutively
1252 When using canConvert() and convert() consecutively, it is possible for
1253 canConvert() to return true, but convert() to return false. This
1254 is typically because canConvert() only reports the general ability of
1255 QVariant to convert between types given suitable data; it is still
1256 possible to supply data which cannot actually be converted.
1258 For example, canConvert() would return true when called on a variant
1259 containing a string because, in principle, QVariant is able to convert
1260 strings of numbers to integers.
1261 However, if the string contains non-numeric characters, it cannot be
1262 converted to an integer, and any attempt to convert it will fail.
1263 Hence, it is important to have both functions return true for a
1264 successful conversion.
1270 \enum QVariant::Type
1272 This enum type defines the types of variable that a QVariant can
1275 \value Invalid no type
1276 \value BitArray a QBitArray
1277 \value Bitmap a QBitmap
1279 \value Brush a QBrush
1280 \value ByteArray a QByteArray
1282 \value Color a QColor
1283 \value Cursor a QCursor
1285 \value DateTime a QDateTime
1286 \value Double a double
1287 \value EasingCurve a QEasingCurve
1289 \value Hash a QVariantHash
1291 \value Image a QImage
1293 \value KeySequence a QKeySequence
1295 \value LineF a QLineF
1296 \value List a QVariantList
1297 \value Locale a QLocale
1298 \value LongLong a \l qlonglong
1299 \value Map a QVariantMap
1300 \value Matrix a QMatrix
1301 \value Transform a QTransform
1302 \value Matrix4x4 a QMatrix4x4
1303 \value Palette a QPalette
1305 \value Pixmap a QPixmap
1306 \value Point a QPoint
1307 \value PointArray a QPointArray
1308 \value PointF a QPointF
1309 \value Polygon a QPolygon
1310 \value Quaternion a QQuaternion
1312 \value RectF a QRectF
1313 \value RegExp a QRegExp
1314 \value Region a QRegion
1316 \value SizeF a QSizeF
1317 \value SizePolicy a QSizePolicy
1318 \value String a QString
1319 \value StringList a QStringList
1320 \value TextFormat a QTextFormat
1321 \value TextLength a QTextLength
1323 \value UInt a \l uint
1324 \value ULongLong a \l qulonglong
1326 \value Vector2D a QVector2D
1327 \value Vector3D a QVector3D
1328 \value Vector4D a QVector4D
1330 \value UserType Base value for user-defined types.
1333 \omitvalue ColorGroup
1335 \omitvalue LastGuiType
1336 \omitvalue LastCoreType
1341 \fn QVariant::QVariant()
1343 Constructs an invalid variant.
1348 \fn QVariant::QVariant(int typeOrUserType, const void *copy)
1350 Constructs variant of type \a typeOrUserType, and initializes with
1351 \a copy if \a copy is not 0.
1353 Note that you have to pass the address of the variable you want stored.
1355 Usually, you never have to use this constructor, use QVariant::fromValue()
1356 instead to construct variants from the pointer types represented by
1357 \c QMetaType::VoidStar, \c QMetaType::QObjectStar and
1358 \c QMetaType::QWidgetStar.
1360 \sa QVariant::fromValue(), Type
1364 \fn QVariant::QVariant(Type type)
1366 Constructs a null variant of type \a type.
1372 \fn QVariant::create(int type, const void *copy)
1376 Constructs a variant private of type \a type, and initializes with \a copy if
1380 void QVariant::create(int type, const void *copy)
1383 handler->construct(&d, copy);
1387 \fn QVariant::~QVariant()
1389 Destroys the QVariant and the contained object.
1391 Note that subclasses that reimplement clear() should reimplement
1392 the destructor to call clear(). This destructor calls clear(), but
1393 because it is the destructor, QVariant::clear() is called rather
1394 than a subclass's clear().
1397 QVariant::~QVariant()
1399 if ((d.is_shared && !d.data.shared->ref.deref()) || (!d.is_shared && d.type > Char && d.type < UserType))
1404 \fn QVariant::QVariant(const QVariant &p)
1406 Constructs a copy of the variant, \a p, passed as the argument to
1410 QVariant::QVariant(const QVariant &p)
1414 d.data.shared->ref.ref();
1415 } else if (p.d.type > Char && p.d.type < QVariant::UserType) {
1416 handler->construct(&d, p.constData());
1417 d.is_null = p.d.is_null;
1421 #ifndef QT_NO_DATASTREAM
1423 Reads the variant from the data stream, \a s.
1425 QVariant::QVariant(QDataStream &s)
1430 #endif //QT_NO_DATASTREAM
1433 \fn QVariant::QVariant(const QString &val)
1435 Constructs a new variant with a string value, \a val.
1439 \fn QVariant::QVariant(const QLatin1String &val)
1441 Constructs a new variant with a string value, \a val.
1445 \fn QVariant::QVariant(const char *val)
1447 Constructs a new variant with a string value of \a val.
1448 The variant creates a deep copy of \a val, using the encoding
1449 set by QTextCodec::setCodecForCStrings().
1451 Note that \a val is converted to a QString for storing in the
1452 variant and QVariant::type() will return QMetaType::QString for
1455 You can disable this operator by defining \c
1456 QT_NO_CAST_FROM_ASCII when you compile your applications.
1458 \sa QTextCodec::setCodecForCStrings()
1461 #ifndef QT_NO_CAST_FROM_ASCII
1462 QVariant::QVariant(const char *val)
1464 QString s = QString::fromAscii(val);
1470 \fn QVariant::QVariant(const QStringList &val)
1472 Constructs a new variant with a string list value, \a val.
1476 \fn QVariant::QVariant(const QMap<QString, QVariant> &val)
1478 Constructs a new variant with a map of QVariants, \a val.
1482 \fn QVariant::QVariant(const QHash<QString, QVariant> &val)
1484 Constructs a new variant with a hash of QVariants, \a val.
1488 \fn QVariant::QVariant(const QDate &val)
1490 Constructs a new variant with a date value, \a val.
1494 \fn QVariant::QVariant(const QTime &val)
1496 Constructs a new variant with a time value, \a val.
1500 \fn QVariant::QVariant(const QDateTime &val)
1502 Constructs a new variant with a date/time value, \a val.
1507 \fn QVariant::QVariant(const QEasingCurve &val)
1509 Constructs a new variant with an easing curve value, \a val.
1513 \fn QVariant::QVariant(const QByteArray &val)
1515 Constructs a new variant with a bytearray value, \a val.
1519 \fn QVariant::QVariant(const QBitArray &val)
1521 Constructs a new variant with a bitarray value, \a val.
1525 \fn QVariant::QVariant(const QPoint &val)
1527 Constructs a new variant with a point value of \a val.
1531 \fn QVariant::QVariant(const QPointF &val)
1533 Constructs a new variant with a point value of \a val.
1537 \fn QVariant::QVariant(const QRectF &val)
1539 Constructs a new variant with a rect value of \a val.
1543 \fn QVariant::QVariant(const QLineF &val)
1545 Constructs a new variant with a line value of \a val.
1549 \fn QVariant::QVariant(const QLine &val)
1551 Constructs a new variant with a line value of \a val.
1555 \fn QVariant::QVariant(const QRect &val)
1557 Constructs a new variant with a rect value of \a val.
1561 \fn QVariant::QVariant(const QSize &val)
1563 Constructs a new variant with a size value of \a val.
1567 \fn QVariant::QVariant(const QSizeF &val)
1569 Constructs a new variant with a size value of \a val.
1573 \fn QVariant::QVariant(const QUrl &val)
1575 Constructs a new variant with a url value of \a val.
1579 \fn QVariant::QVariant(int val)
1581 Constructs a new variant with an integer value, \a val.
1585 \fn QVariant::QVariant(uint val)
1587 Constructs a new variant with an unsigned integer value, \a val.
1591 \fn QVariant::QVariant(qlonglong val)
1593 Constructs a new variant with a long long integer value, \a val.
1597 \fn QVariant::QVariant(qulonglong val)
1599 Constructs a new variant with an unsigned long long integer value, \a val.
1604 \fn QVariant::QVariant(bool val)
1606 Constructs a new variant with a boolean value, \a val.
1610 \fn QVariant::QVariant(double val)
1612 Constructs a new variant with a floating point value, \a val.
1616 \fn QVariant::QVariant(float val)
1618 Constructs a new variant with a floating point value, \a val.
1623 \fn QVariant::QVariant(const QList<QVariant> &val)
1625 Constructs a new variant with a list value, \a val.
1629 \fn QVariant::QVariant(const QChar &c)
1631 Constructs a new variant with a char value, \a c.
1635 \fn QVariant::QVariant(const QLocale &l)
1637 Constructs a new variant with a locale value, \a l.
1641 \fn QVariant::QVariant(const QRegExp ®Exp)
1643 Constructs a new variant with the regexp value \a regExp.
1647 \fn QVariant::QVariant(Qt::GlobalColor color)
1649 Constructs a new variant of type QVariant::Color and initializes
1652 This is a convenience constructor that allows \c{QVariant(Qt::blue);}
1653 to create a valid QVariant storing a QColor.
1655 Note: This constructor will assert if the application does not link
1656 to the Qt GUI library.
1659 QVariant::QVariant(Type type)
1660 { create(type, 0); }
1661 QVariant::QVariant(int typeOrUserType, const void *copy)
1662 { create(typeOrUserType, copy); d.is_null = false; }
1665 flags is true if it is a pointer type
1667 QVariant::QVariant(int typeOrUserType, const void *copy, uint flags)
1669 if (flags) { //type is a pointer type
1670 d.type = typeOrUserType;
1671 d.data.ptr = *reinterpret_cast<void *const*>(copy);
1674 create(typeOrUserType, copy);
1679 QVariant::QVariant(int val)
1680 { d.is_null = false; d.type = Int; d.data.i = val; }
1681 QVariant::QVariant(uint val)
1682 { d.is_null = false; d.type = UInt; d.data.u = val; }
1683 QVariant::QVariant(qlonglong val)
1684 { d.is_null = false; d.type = LongLong; d.data.ll = val; }
1685 QVariant::QVariant(qulonglong val)
1686 { d.is_null = false; d.type = ULongLong; d.data.ull = val; }
1687 QVariant::QVariant(bool val)
1688 { d.is_null = false; d.type = Bool; d.data.b = val; }
1689 QVariant::QVariant(double val)
1690 { d.is_null = false; d.type = Double; d.data.d = val; }
1692 QVariant::QVariant(const QByteArray &val)
1693 { d.is_null = false; d.type = ByteArray; v_construct<QByteArray>(&d, val); }
1694 QVariant::QVariant(const QBitArray &val)
1695 { d.is_null = false; d.type = BitArray; v_construct<QBitArray>(&d, val); }
1696 QVariant::QVariant(const QString &val)
1697 { d.is_null = false; d.type = String; v_construct<QString>(&d, val); }
1698 QVariant::QVariant(const QChar &val)
1699 { d.is_null = false; d.type = Char; v_construct<QChar>(&d, val); }
1700 QVariant::QVariant(const QLatin1String &val)
1701 { QString str(val); d.is_null = false; d.type = String; v_construct<QString>(&d, str); }
1702 QVariant::QVariant(const QStringList &val)
1703 { d.is_null = false; d.type = StringList; v_construct<QStringList>(&d, val); }
1705 QVariant::QVariant(const QDate &val)
1706 { d.is_null = false; d.type = Date; v_construct<QDate>(&d, val); }
1707 QVariant::QVariant(const QTime &val)
1708 { d.is_null = false; d.type = Time; v_construct<QTime>(&d, val); }
1709 QVariant::QVariant(const QDateTime &val)
1710 { d.is_null = false; d.type = DateTime; v_construct<QDateTime>(&d, val); }
1711 #ifndef QT_BOOTSTRAPPED
1712 QVariant::QVariant(const QEasingCurve &val)
1713 { d.is_null = false; d.type = EasingCurve; v_construct<QEasingCurve>(&d, val); }
1715 QVariant::QVariant(const QList<QVariant> &list)
1716 { d.is_null = false; d.type = List; v_construct<QVariantList>(&d, list); }
1717 QVariant::QVariant(const QMap<QString, QVariant> &map)
1718 { d.is_null = false; d.type = Map; v_construct<QVariantMap>(&d, map); }
1719 QVariant::QVariant(const QHash<QString, QVariant> &hash)
1720 { d.is_null = false; d.type = Hash; v_construct<QVariantHash>(&d, hash); }
1721 #ifndef QT_NO_GEOM_VARIANT
1722 QVariant::QVariant(const QPoint &pt) { d.is_null = false; d.type = Point; v_construct<QPoint>(&d, pt); }
1723 QVariant::QVariant(const QPointF &pt) { d.is_null = false; d.type = PointF; v_construct<QPointF>(&d, pt); }
1724 QVariant::QVariant(const QRectF &r) { d.is_null = false; d.type = RectF; v_construct<QRectF>(&d, r); }
1725 QVariant::QVariant(const QLineF &l) { d.is_null = false; d.type = LineF; v_construct<QLineF>(&d, l); }
1726 QVariant::QVariant(const QLine &l) { d.is_null = false; d.type = Line; v_construct<QLine>(&d, l); }
1727 QVariant::QVariant(const QRect &r) { d.is_null = false; d.type = Rect; v_construct<QRect>(&d, r); }
1728 QVariant::QVariant(const QSize &s) { d.is_null = false; d.type = Size; v_construct<QSize>(&d, s); }
1729 QVariant::QVariant(const QSizeF &s) { d.is_null = false; d.type = SizeF; v_construct<QSizeF>(&d, s); }
1731 QVariant::QVariant(const QUrl &u) { d.is_null = false; d.type = Url; v_construct<QUrl>(&d, u); }
1732 QVariant::QVariant(const QLocale &l) { d.is_null = false; d.type = Locale; v_construct<QLocale>(&d, l); }
1733 #ifndef QT_NO_REGEXP
1734 QVariant::QVariant(const QRegExp ®Exp) { d.is_null = false; d.type = RegExp; v_construct<QRegExp>(&d, regExp); }
1736 QVariant::QVariant(Qt::GlobalColor color) { create(62, &color); }
1739 Returns the storage type of the value stored in the variant.
1740 Although this function is declared as returning QVariant::Type,
1741 the return value should be interpreted as QMetaType::Type. In
1742 particular, QVariant::UserType is returned here only if the value
1743 is equal or greater than QMetaType::User.
1745 Note that return values in the ranges QVariant::Char through
1746 QVariant::RegExp and QVariant::Font through QVariant::Transform
1747 correspond to the values in the ranges QMetaType::QChar through
1748 QMetaType::QRegExp and QMetaType::QFont through QMetaType::QQuaternion.
1750 Pay particular attention when working with char and QChar
1751 variants. Note that there is no QVariant constructor specifically
1752 for type char, but there is one for QChar. For a variant of type
1753 QChar, this function returns QVariant::Char, which is the same as
1754 QMetaType::QChar, but for a variant of type \c char, this function
1755 returns QMetaType::Char, which is \e not the same as
1758 Also note that the types \c void*, \c long, \c short, \c unsigned
1759 \c long, \c unsigned \c short, \c unsigned \c char, \c float, \c
1760 QObject*, and \c QWidget* are represented in QMetaType::Type but
1761 not in QVariant::Type, and they can be returned by this function.
1762 However, they are considered to be user defined types when tested
1763 against QVariant::Type.
1765 To test whether an instance of QVariant contains a data type that
1766 is compatible with the data type you are interested in, use
1770 QVariant::Type QVariant::type() const
1772 return d.type >= QMetaType::User ? UserType : static_cast<Type>(d.type);
1776 Returns the storage type of the value stored in the variant. For
1777 non-user types, this is the same as type().
1782 int QVariant::userType() const
1788 Assigns the value of the variant \a variant to this variant.
1790 QVariant& QVariant::operator=(const QVariant &variant)
1792 if (this == &variant)
1796 if (variant.d.is_shared) {
1797 variant.d.data.shared->ref.ref();
1799 } else if (variant.d.type > Char && variant.d.type < UserType) {
1800 d.type = variant.d.type;
1801 handler->construct(&d, variant.constData());
1802 d.is_null = variant.d.is_null;
1811 \fn void QVariant::swap(QVariant &other)
1814 Swaps variant \a other with this variant. This operation is very
1815 fast and never fails.
1819 \fn void QVariant::detach()
1824 void QVariant::detach()
1826 if (!d.is_shared || d.data.shared->ref == 1)
1831 handler->construct(&dd, constData());
1832 if (!d.data.shared->ref.deref())
1834 d.data.shared = dd.data.shared;
1838 \fn bool QVariant::isDetached() const
1843 // ### Qt 5: change typeName()(and froends= to return a QString. Suggestion from Harald.
1845 Returns the name of the type stored in the variant. The returned
1846 strings describe the C++ datatype used to store the data: for
1847 example, "QFont", "QString", or "QVariantList". An Invalid
1850 const char *QVariant::typeName() const
1852 return typeToName(Type(d.type));
1856 Convert this variant to type Invalid and free up any resources
1859 void QVariant::clear()
1861 if ((d.is_shared && !d.data.shared->ref.deref()) || (!d.is_shared && d.type < UserType && d.type > Char))
1865 d.is_shared = false;
1869 Converts the enum representation of the storage type, \a typ, to
1870 its string representation.
1872 Returns a null pointer if the type is QVariant::Invalid or doesn't exist.
1874 const char *QVariant::typeToName(Type typ)
1878 if (typ == UserType)
1881 return QMetaType::typeName(typ);
1886 Converts the string representation of the storage type given in \a
1887 name, to its enum representation.
1889 If the string representation cannot be converted to any enum
1890 representation, the variant is set to \c Invalid.
1892 QVariant::Type QVariant::nameToType(const char *name)
1894 if (!name || !*name)
1896 if (strcmp(name, "Q3CString") == 0)
1898 if (strcmp(name, "Q_LLONG") == 0)
1900 if (strcmp(name, "Q_ULLONG") == 0)
1902 if (strcmp(name, "QIconSet") == 0)
1904 if (strcmp(name, "UserType") == 0)
1907 int metaType = QMetaType::type(name);
1908 return metaType <= int(LastGuiType) ? QVariant::Type(metaType) : UserType;
1911 #ifndef QT_NO_DATASTREAM
1912 enum { MapFromThreeCount = 36 };
1913 static const ushort map_from_three[MapFromThreeCount] =
1919 QVariant::StringList,
1935 QVariant::ByteArray,
1940 QVariant::SizePolicy,
1944 QVariant::ByteArray,
1946 QVariant::KeySequence,
1949 QVariant::ULongLong,
1950 QVariant::EasingCurve
1954 Internal function for loading a variant from stream \a s. Use the
1955 stream operators instead.
1959 void QVariant::load(QDataStream &s)
1965 if (s.version() < QDataStream::Qt_4_0) {
1966 if (u >= MapFromThreeCount)
1968 u = map_from_three[u];
1970 qint8 is_null = false;
1971 if (s.version() >= QDataStream::Qt_4_2)
1973 if (u == QVariant::UserType) {
1976 u = QMetaType::type(name);
1978 s.setStatus(QDataStream::ReadCorruptData);
1982 create(static_cast<int>(u), 0);
1983 d.is_null = is_null;
1986 // Since we wrote something, we should read something
1993 // const cast is safe since we operate on a newly constructed variant
1994 if (!QMetaType::load(s, d.type, const_cast<void *>(constData()))) {
1995 s.setStatus(QDataStream::ReadCorruptData);
1996 qWarning("QVariant::load: unable to load type %d.", d.type);
2001 Internal function for saving a variant to the stream \a s. Use the
2002 stream operators instead.
2006 void QVariant::save(QDataStream &s) const
2008 quint32 tp = type();
2009 if (s.version() < QDataStream::Qt_4_0) {
2011 for (i = MapFromThreeCount - 1; i >= 0; i--) {
2012 if (map_from_three[i] == tp) {
2023 if (s.version() >= QDataStream::Qt_4_2)
2024 s << qint8(d.is_null);
2025 if (tp == QVariant::UserType) {
2026 s << QMetaType::typeName(userType());
2034 if (!QMetaType::save(s, d.type, constData())) {
2035 Q_ASSERT_X(false, "QVariant::save", "Invalid type to save");
2036 qWarning("QVariant::save: unable to save type %d.", d.type);
2043 Reads a variant \a p from the stream \a s.
2045 \sa \link datastreamformat.html Format of the QDataStream
2048 QDataStream& operator>>(QDataStream &s, QVariant &p)
2055 Writes a variant \a p to the stream \a s.
2057 \sa \link datastreamformat.html Format of the QDataStream
2060 QDataStream& operator<<(QDataStream &s, const QVariant &p)
2067 Reads a variant type \a p in enum representation from the stream \a s.
2069 QDataStream& operator>>(QDataStream &s, QVariant::Type &p)
2073 p = (QVariant::Type)u;
2079 Writes a variant type \a p to the stream \a s.
2081 QDataStream& operator<<(QDataStream &s, const QVariant::Type p)
2083 s << static_cast<quint32>(p);
2088 #endif //QT_NO_DATASTREAM
2091 \fn bool QVariant::isValid() const
2093 Returns true if the storage type of this variant is not
2094 QVariant::Invalid; otherwise returns false.
2097 template <typename T>
2098 inline T qVariantToHelper(const QVariant::Private &d, QVariant::Type t,
2099 const QVariant::Handler *handler, T * = 0)
2102 return *v_cast<T>(&d);
2105 handler->convert(&d, t, &ret, 0);
2110 \fn QStringList QVariant::toStringList() const
2112 Returns the variant as a QStringList if the variant has type()
2113 StringList, \l String, or \l List of a type that can be converted
2114 to QString; otherwise returns an empty list.
2116 \sa canConvert(), convert()
2118 QStringList QVariant::toStringList() const
2120 return qVariantToHelper<QStringList>(d, StringList, handler);
2124 Returns the variant as a QString if the variant has type() \l
2125 String, \l Bool, \l ByteArray, \l Char, \l Date, \l DateTime, \l
2126 Double, \l Int, \l LongLong, \l StringList, \l Time, \l UInt, or
2127 \l ULongLong; otherwise returns an empty string.
2129 \sa canConvert(), convert()
2131 QString QVariant::toString() const
2133 return qVariantToHelper<QString>(d, String, handler);
2137 Returns the variant as a QMap<QString, QVariant> if the variant
2138 has type() \l Map; otherwise returns an empty map.
2140 \sa canConvert(), convert()
2142 QVariantMap QVariant::toMap() const
2144 return qVariantToHelper<QVariantMap>(d, Map, handler);
2148 Returns the variant as a QHash<QString, QVariant> if the variant
2149 has type() \l Hash; otherwise returns an empty map.
2151 \sa canConvert(), convert()
2153 QVariantHash QVariant::toHash() const
2155 return qVariantToHelper<QVariantHash>(d, Hash, handler);
2159 \fn QDate QVariant::toDate() const
2161 Returns the variant as a QDate if the variant has type() \l Date,
2162 \l DateTime, or \l String; otherwise returns an invalid date.
2164 If the type() is \l String, an invalid date will be returned if the
2165 string cannot be parsed as a Qt::ISODate format date.
2167 \sa canConvert(), convert()
2169 QDate QVariant::toDate() const
2171 return qVariantToHelper<QDate>(d, Date, handler);
2175 \fn QTime QVariant::toTime() const
2177 Returns the variant as a QTime if the variant has type() \l Time,
2178 \l DateTime, or \l String; otherwise returns an invalid time.
2180 If the type() is \l String, an invalid time will be returned if
2181 the string cannot be parsed as a Qt::ISODate format time.
2183 \sa canConvert(), convert()
2185 QTime QVariant::toTime() const
2187 return qVariantToHelper<QTime>(d, Time, handler);
2191 \fn QDateTime QVariant::toDateTime() const
2193 Returns the variant as a QDateTime if the variant has type() \l
2194 DateTime, \l Date, or \l String; otherwise returns an invalid
2197 If the type() is \l String, an invalid date/time will be returned
2198 if the string cannot be parsed as a Qt::ISODate format date/time.
2200 \sa canConvert(), convert()
2202 QDateTime QVariant::toDateTime() const
2204 return qVariantToHelper<QDateTime>(d, DateTime, handler);
2209 \fn QEasingCurve QVariant::toEasingCurve() const
2211 Returns the variant as a QEasingCurve if the variant has type() \l
2212 EasingCurve; otherwise returns a default easing curve.
2214 \sa canConvert(), convert()
2216 #ifndef QT_BOOTSTRAPPED
2217 QEasingCurve QVariant::toEasingCurve() const
2219 return qVariantToHelper<QEasingCurve>(d, EasingCurve, handler);
2224 \fn QByteArray QVariant::toByteArray() const
2226 Returns the variant as a QByteArray if the variant has type() \l
2227 ByteArray or \l String (converted using QString::fromAscii());
2228 otherwise returns an empty byte array.
2230 \sa canConvert(), convert()
2232 QByteArray QVariant::toByteArray() const
2234 return qVariantToHelper<QByteArray>(d, ByteArray, handler);
2237 #ifndef QT_NO_GEOM_VARIANT
2239 \fn QPoint QVariant::toPoint() const
2241 Returns the variant as a QPoint if the variant has type()
2242 \l Point or \l PointF; otherwise returns a null QPoint.
2244 \sa canConvert(), convert()
2246 QPoint QVariant::toPoint() const
2248 return qVariantToHelper<QPoint>(d, Point, handler);
2252 \fn QRect QVariant::toRect() const
2254 Returns the variant as a QRect if the variant has type() \l Rect;
2255 otherwise returns an invalid QRect.
2257 \sa canConvert(), convert()
2259 QRect QVariant::toRect() const
2261 return qVariantToHelper<QRect>(d, Rect, handler);
2265 \fn QSize QVariant::toSize() const
2267 Returns the variant as a QSize if the variant has type() \l Size;
2268 otherwise returns an invalid QSize.
2270 \sa canConvert(), convert()
2272 QSize QVariant::toSize() const
2274 return qVariantToHelper<QSize>(d, Size, handler);
2278 \fn QSizeF QVariant::toSizeF() const
2280 Returns the variant as a QSizeF if the variant has type() \l
2281 SizeF; otherwise returns an invalid QSizeF.
2283 \sa canConvert(), convert()
2285 QSizeF QVariant::toSizeF() const
2287 return qVariantToHelper<QSizeF>(d, SizeF, handler);
2291 \fn QRectF QVariant::toRectF() const
2293 Returns the variant as a QRectF if the variant has type() \l Rect
2294 or \l RectF; otherwise returns an invalid QRectF.
2296 \sa canConvert(), convert()
2298 QRectF QVariant::toRectF() const
2300 return qVariantToHelper<QRectF>(d, RectF, handler);
2304 \fn QLineF QVariant::toLineF() const
2306 Returns the variant as a QLineF if the variant has type() \l
2307 LineF; otherwise returns an invalid QLineF.
2309 \sa canConvert(), convert()
2311 QLineF QVariant::toLineF() const
2313 return qVariantToHelper<QLineF>(d, LineF, handler);
2317 \fn QLine QVariant::toLine() const
2319 Returns the variant as a QLine if the variant has type() \l Line;
2320 otherwise returns an invalid QLine.
2322 \sa canConvert(), convert()
2324 QLine QVariant::toLine() const
2326 return qVariantToHelper<QLine>(d, Line, handler);
2330 \fn QPointF QVariant::toPointF() const
2332 Returns the variant as a QPointF if the variant has type() \l
2333 Point or \l PointF; otherwise returns a null QPointF.
2335 \sa canConvert(), convert()
2337 QPointF QVariant::toPointF() const
2339 return qVariantToHelper<QPointF>(d, PointF, handler);
2342 #endif // QT_NO_GEOM_VARIANT
2345 \fn QUrl QVariant::toUrl() const
2347 Returns the variant as a QUrl if the variant has type()
2348 \l Url; otherwise returns an invalid QUrl.
2350 \sa canConvert(), convert()
2352 QUrl QVariant::toUrl() const
2354 return qVariantToHelper<QUrl>(d, Url, handler);
2358 \fn QLocale QVariant::toLocale() const
2360 Returns the variant as a QLocale if the variant has type()
2361 \l Locale; otherwise returns an invalid QLocale.
2363 \sa canConvert(), convert()
2365 QLocale QVariant::toLocale() const
2367 return qVariantToHelper<QLocale>(d, Locale, handler);
2371 \fn QRegExp QVariant::toRegExp() const
2374 Returns the variant as a QRegExp if the variant has type() \l
2375 RegExp; otherwise returns an empty QRegExp.
2377 \sa canConvert(), convert()
2379 #ifndef QT_NO_REGEXP
2380 QRegExp QVariant::toRegExp() const
2382 return qVariantToHelper<QRegExp>(d, RegExp, handler);
2387 \fn QChar QVariant::toChar() const
2389 Returns the variant as a QChar if the variant has type() \l Char,
2390 \l Int, or \l UInt; otherwise returns an invalid QChar.
2392 \sa canConvert(), convert()
2394 QChar QVariant::toChar() const
2396 return qVariantToHelper<QChar>(d, Char, handler);
2400 Returns the variant as a QBitArray if the variant has type()
2401 \l BitArray; otherwise returns an empty bit array.
2403 \sa canConvert(), convert()
2405 QBitArray QVariant::toBitArray() const
2407 return qVariantToHelper<QBitArray>(d, BitArray, handler);
2410 template <typename T>
2411 inline T qNumVariantToHelper(const QVariant::Private &d,
2412 const QVariant::Handler *handler, bool *ok, const T& val)
2414 uint t = qMetaTypeId<T>();
2421 if (!handler->convert(&d, QVariant::Type(t), &ret, ok) && ok)
2427 Returns the variant as an int if the variant has type() \l Int,
2428 \l Bool, \l ByteArray, \l Char, \l Double, \l LongLong, \l
2429 String, \l UInt, or \l ULongLong; otherwise returns 0.
2431 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2432 converted to an int; otherwise \c{*}\a{ok} is set to false.
2434 \bold{Warning:} If the value is convertible to a \l LongLong but is too
2435 large to be represented in an int, the resulting arithmetic overflow will
2436 not be reflected in \a ok. A simple workaround is to use QString::toInt().
2437 Fixing this bug has been postponed to Qt 5 in order to avoid breaking existing code.
2439 \sa canConvert(), convert()
2441 int QVariant::toInt(bool *ok) const
2443 return qNumVariantToHelper<int>(d, handler, ok, d.data.i);
2447 Returns the variant as an unsigned int if the variant has type()
2448 \l UInt, \l Bool, \l ByteArray, \l Char, \l Double, \l Int, \l
2449 LongLong, \l String, or \l ULongLong; otherwise returns 0.
2451 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2452 converted to an unsigned int; otherwise \c{*}\a{ok} is set to false.
2454 \bold{Warning:} If the value is convertible to a \l ULongLong but is too
2455 large to be represented in an unsigned int, the resulting arithmetic overflow will
2456 not be reflected in \a ok. A simple workaround is to use QString::toUInt().
2457 Fixing this bug has been postponed to Qt 5 in order to avoid breaking existing code.
2459 \sa canConvert(), convert()
2461 uint QVariant::toUInt(bool *ok) const
2463 return qNumVariantToHelper<uint>(d, handler, ok, d.data.u);
2467 Returns the variant as a long long int if the variant has type()
2468 \l LongLong, \l Bool, \l ByteArray, \l Char, \l Double, \l Int,
2469 \l String, \l UInt, or \l ULongLong; otherwise returns 0.
2471 If \a ok is non-null: \c{*}\c{ok} is set to true if the value could be
2472 converted to an int; otherwise \c{*}\c{ok} is set to false.
2474 \sa canConvert(), convert()
2476 qlonglong QVariant::toLongLong(bool *ok) const
2478 return qNumVariantToHelper<qlonglong>(d, handler, ok, d.data.ll);
2482 Returns the variant as as an unsigned long long int if the
2483 variant has type() \l ULongLong, \l Bool, \l ByteArray, \l Char,
2484 \l Double, \l Int, \l LongLong, \l String, or \l UInt; otherwise
2487 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2488 converted to an int; otherwise \c{*}\a{ok} is set to false.
2490 \sa canConvert(), convert()
2492 qulonglong QVariant::toULongLong(bool *ok) const
2494 return qNumVariantToHelper<qulonglong>(d, handler, ok, d.data.ull);
2498 Returns the variant as a bool if the variant has type() Bool.
2500 Returns true if the variant has type() \l Bool, \l Char, \l Double,
2501 \l Int, \l LongLong, \l UInt, or \l ULongLong and the value is
2502 non-zero, or if the variant has type \l String or \l ByteArray and
2503 its lower-case content is not empty, "0" or "false"; otherwise
2506 \sa canConvert(), convert()
2508 bool QVariant::toBool() const
2514 handler->convert(&d, Bool, &res, 0);
2520 Returns the variant as a double if the variant has type() \l
2521 Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
2522 UInt, or \l ULongLong; otherwise returns 0.0.
2524 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2525 converted to a double; otherwise \c{*}\a{ok} is set to false.
2527 \sa canConvert(), convert()
2529 double QVariant::toDouble(bool *ok) const
2531 return qNumVariantToHelper<double>(d, handler, ok, d.data.d);
2535 Returns the variant as a float if the variant has type() \l
2536 Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
2537 UInt, or \l ULongLong; otherwise returns 0.0.
2541 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2542 converted to a double; otherwise \c{*}\a{ok} is set to false.
2544 \sa canConvert(), convert()
2546 float QVariant::toFloat(bool *ok) const
2548 return qNumVariantToHelper<float>(d, handler, ok, d.data.f);
2552 Returns the variant as a qreal if the variant has type() \l
2553 Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
2554 UInt, or \l ULongLong; otherwise returns 0.0.
2558 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2559 converted to a double; otherwise \c{*}\a{ok} is set to false.
2561 \sa canConvert(), convert()
2563 qreal QVariant::toReal(bool *ok) const
2565 return qNumVariantToHelper<qreal>(d, handler, ok, d.data.real);
2569 Returns the variant as a QVariantList if the variant has type()
2570 \l List or \l StringList; otherwise returns an empty list.
2572 \sa canConvert(), convert()
2574 QVariantList QVariant::toList() const
2576 return qVariantToHelper<QVariantList>(d, List, handler);
2579 /*! \fn QVariant::canCast(Type t) const
2580 Use canConvert() instead.
2583 /*! \fn QVariant::cast(Type t)
2584 Use convert() instead.
2588 static const quint32 qCanConvertMatrix[QVariant::LastCoreType + 1] =
2592 /*Bool*/ 1 << QVariant::Double | 1 << QVariant::Int | 1 << QVariant::UInt
2593 | 1 << QVariant::LongLong | 1 << QVariant::ULongLong | 1 << QVariant::ByteArray
2594 | 1 << QVariant::String | 1 << QVariant::Char,
2596 /*Int*/ 1 << QVariant::UInt | 1 << QVariant::String | 1 << QVariant::Double
2597 | 1 << QVariant::Bool | 1 << QVariant::LongLong | 1 << QVariant::ULongLong
2598 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2600 /*UInt*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::Double
2601 | 1 << QVariant::Bool | 1 << QVariant::LongLong | 1 << QVariant::ULongLong
2602 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2604 /*LLong*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::Double
2605 | 1 << QVariant::Bool | 1 << QVariant::UInt | 1 << QVariant::ULongLong
2606 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2608 /*ULlong*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::Double
2609 | 1 << QVariant::Bool | 1 << QVariant::UInt | 1 << QVariant::LongLong
2610 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2612 /*double*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::ULongLong
2613 | 1 << QVariant::Bool | 1 << QVariant::UInt | 1 << QVariant::LongLong
2614 | 1 << QVariant::ByteArray,
2616 /*QChar*/ 1 << QVariant::Int | 1 << QVariant::UInt | 1 << QVariant::LongLong
2617 | 1 << QVariant::ULongLong,
2621 /*QList*/ 1 << QVariant::StringList,
2623 /*QString*/ 1 << QVariant::StringList | 1 << QVariant::ByteArray | 1 << QVariant::Int
2624 | 1 << QVariant::UInt | 1 << QVariant::Bool | 1 << QVariant::Double
2625 | 1 << QVariant::Date | 1 << QVariant::Time | 1 << QVariant::DateTime
2626 | 1 << QVariant::LongLong | 1 << QVariant::ULongLong | 1 << QVariant::Char
2627 | 1 << QVariant::Url,
2629 /*QStringList*/ 1 << QVariant::List | 1 << QVariant::String,
2631 /*QByteArray*/ 1 << QVariant::String | 1 << QVariant::Int | 1 << QVariant::UInt | 1 << QVariant::Bool
2632 | 1 << QVariant::Double | 1 << QVariant::LongLong | 1 << QVariant::ULongLong,
2636 /*QDate*/ 1 << QVariant::String | 1 << QVariant::DateTime,
2638 /*QTime*/ 1 << QVariant::String | 1 << QVariant::DateTime,
2640 /*QDateTime*/ 1 << QVariant::String | 1 << QVariant::Date,
2642 /*QUrl*/ 1 << QVariant::String,
2646 /*QRect*/ 1 << QVariant::RectF,
2648 /*QRectF*/ 1 << QVariant::Rect,
2650 /*QSize*/ 1 << QVariant::SizeF,
2652 /*QSizeF*/ 1 << QVariant::Size,
2654 /*QLine*/ 1 << QVariant::LineF,
2656 /*QLineF*/ 1 << QVariant::Line,
2658 /*QPoint*/ 1 << QVariant::PointF,
2660 /*QPointF*/ 1 << QVariant::Point,
2670 Returns true if the variant's type can be cast to the requested
2671 type, \a t. Such casting is done automatically when calling the
2672 toInt(), toBool(), ... methods.
2674 The following casts are done automatically:
2677 \header \o Type \o Automatically Cast To
2678 \row \o \l Bool \o \l Char, \l Double, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2679 \row \o \l ByteArray \o \l Double, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2680 \row \o \l Char \o \l Bool, \l Int, \l UInt, \l LongLong, \l ULongLong
2681 \row \o \l Color \o \l String
2682 \row \o \l Date \o \l DateTime, \l String
2683 \row \o \l DateTime \o \l Date, \l String, \l Time
2684 \row \o \l Double \o \l Bool, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2685 \row \o \l Font \o \l String
2686 \row \o \l Int \o \l Bool, \l Char, \l Double, \l LongLong, \l String, \l UInt, \l ULongLong
2687 \row \o \l KeySequence \o \l Int, \l String
2688 \row \o \l List \o \l StringList (if the list's items can be converted to strings)
2689 \row \o \l LongLong \o \l Bool, \l ByteArray, \l Char, \l Double, \l Int, \l String, \l UInt, \l ULongLong
2690 \row \o \l Point \o PointF
2691 \row \o \l Rect \o RectF
2692 \row \o \l String \o \l Bool, \l ByteArray, \l Char, \l Color, \l Date, \l DateTime, \l Double,
2693 \l Font, \l Int, \l KeySequence, \l LongLong, \l StringList, \l Time, \l UInt,
2695 \row \o \l StringList \o \l List, \l String (if the list contains exactly one item)
2696 \row \o \l Time \o \l String
2697 \row \o \l UInt \o \l Bool, \l Char, \l Double, \l Int, \l LongLong, \l String, \l ULongLong
2698 \row \o \l ULongLong \o \l Bool, \l Char, \l Double, \l Int, \l LongLong, \l String, \l UInt
2703 bool QVariant::canConvert(Type t) const
2705 //we can treat floats as double
2706 //the reason for not doing it the "proper" way is that QMetaType::Float's value is 135,
2707 //which can't be handled by qCanConvertMatrix
2708 //In addition QVariant::Type doesn't have a Float value, so we're using QMetaType::Float
2709 const uint currentType = ((d.type == QMetaType::Float) ? QVariant::Double : d.type);
2710 if (uint(t) == uint(QMetaType::Float)) t = QVariant::Double;
2712 if (currentType == uint(t))
2715 if (currentType > QVariant::LastCoreType || t > QVariant::LastCoreType) {
2718 return currentType == QVariant::KeySequence
2719 || currentType == QMetaType::ULong
2720 || currentType == QMetaType::Long
2721 || currentType == QMetaType::UShort
2722 || currentType == QMetaType::UChar
2723 || currentType == QMetaType::Char
2724 || currentType == QMetaType::Short;
2725 case QVariant::Image:
2726 return currentType == QVariant::Pixmap || currentType == QVariant::Bitmap;
2727 case QVariant::Pixmap:
2728 return currentType == QVariant::Image || currentType == QVariant::Bitmap
2729 || currentType == QVariant::Brush;
2730 case QVariant::Bitmap:
2731 return currentType == QVariant::Pixmap || currentType == QVariant::Image;
2732 case QVariant::ByteArray:
2733 return currentType == QVariant::Color;
2734 case QVariant::String:
2735 return currentType == QVariant::KeySequence || currentType == QVariant::Font
2736 || currentType == QVariant::Color;
2737 case QVariant::KeySequence:
2738 return currentType == QVariant::String || currentType == QVariant::Int;
2739 case QVariant::Font:
2740 return currentType == QVariant::String;
2741 case QVariant::Color:
2742 return currentType == QVariant::String || currentType == QVariant::ByteArray
2743 || currentType == QVariant::Brush;
2744 case QVariant::Brush:
2745 return currentType == QVariant::Color || currentType == QVariant::Pixmap;
2746 case QMetaType::Long:
2747 case QMetaType::Char:
2748 case QMetaType::UChar:
2749 case QMetaType::ULong:
2750 case QMetaType::Short:
2751 case QMetaType::UShort:
2752 return qCanConvertMatrix[QVariant::Int] & (1 << currentType) || currentType == QVariant::Int;
2758 if(t == String && currentType == StringList)
2759 return v_cast<QStringList>(&d)->count() == 1;
2761 return qCanConvertMatrix[t] & (1 << currentType);
2765 Casts the variant to the requested type, \a t. If the cast cannot be
2766 done, the variant is cleared. Returns true if the current type of
2767 the variant was successfully cast; otherwise returns false.
2769 \warning For historical reasons, converting a null QVariant results
2770 in a null value of the desired type (e.g., an empty string for
2771 QString) and a result of false.
2773 \sa canConvert(), clear()
2776 bool QVariant::convert(Type t)
2778 if (d.type == uint(t))
2781 QVariant oldValue = *this;
2784 if (!oldValue.canConvert(t))
2788 if (oldValue.isNull())
2792 if (!handler->convert(&oldValue.d, t, data(), &isOk))
2799 \fn bool operator==(const QVariant &v1, const QVariant &v2)
2803 Returns true if \a v1 and \a v2 are equal; otherwise returns false.
2805 \warning This function doesn't support custom types registered
2806 with qRegisterMetaType().
2809 \fn bool operator!=(const QVariant &v1, const QVariant &v2)
2813 Returns false if \a v1 and \a v2 are equal; otherwise returns true.
2815 \warning This function doesn't support custom types registered
2816 with qRegisterMetaType().
2819 /*! \fn bool QVariant::operator==(const QVariant &v) const
2821 Compares this QVariant with \a v and returns true if they are
2822 equal; otherwise returns false.
2824 In the case of custom types, their equalness operators are not called.
2825 Instead the values' addresses are compared.
2829 \fn bool QVariant::operator!=(const QVariant &v) const
2831 Compares this QVariant with \a v and returns true if they are not
2832 equal; otherwise returns false.
2834 \warning This function doesn't support custom types registered
2835 with qRegisterMetaType().
2838 static bool qIsNumericType(uint tp)
2840 return (tp >= QVariant::Bool && tp <= QVariant::Double)
2841 || (tp >= QMetaType::Long && tp <= QMetaType::Float);
2844 static bool qIsFloatingPoint(uint tp)
2846 return tp == QVariant::Double || tp == QMetaType::Float;
2851 bool QVariant::cmp(const QVariant &v) const
2854 if (d.type != v2.d.type) {
2855 if (qIsNumericType(d.type) && qIsNumericType(v.d.type)) {
2856 if (qIsFloatingPoint(d.type) || qIsFloatingPoint(v.d.type))
2857 return qFuzzyCompare(toReal(), v.toReal());
2859 return toLongLong() == v.toLongLong();
2861 if (!v2.canConvert(Type(d.type)) || !v2.convert(Type(d.type)))
2864 return handler->compare(&d, &v2.d);
2870 const void *QVariant::constData() const
2872 return d.is_shared ? d.data.shared->ptr : reinterpret_cast<const void *>(&d.data.ptr);
2876 \fn const void* QVariant::data() const
2882 void* QVariant::data()
2885 return const_cast<void *>(constData());
2892 void *QVariant::castOrDetach(Type t)
2894 if (d.type != uint(t)) {
2905 Returns true if this is a NULL variant, false otherwise.
2907 bool QVariant::isNull() const
2909 return handler->isNull(&d);
2912 #ifndef QT_NO_DEBUG_STREAM
2913 QDebug operator<<(QDebug dbg, const QVariant &v)
2915 #ifndef Q_BROKEN_DEBUG_STREAM
2916 dbg.nospace() << "QVariant(" << v.typeName() << ", ";
2917 QVariant::handler->debugStream(dbg, v);
2918 dbg.nospace() << ')';
2921 qWarning("This compiler doesn't support streaming QVariant to QDebug");
2927 QDebug operator<<(QDebug dbg, const QVariant::Type p)
2929 #ifndef Q_BROKEN_DEBUG_STREAM
2930 dbg.nospace() << "QVariant::" << QVariant::typeToName(p);
2933 qWarning("This compiler doesn't support streaming QVariant::Type to QDebug");
2941 \fn int &QVariant::asInt()
2943 Use toInt() instead.
2947 \fn uint &QVariant::asUInt()
2949 Use toUInt() instead.
2953 \fn qlonglong &QVariant::asLongLong()
2955 Use toLongLong() instead.
2959 \fn qulonglong &QVariant::asULongLong()
2961 Use toULongLong() instead.
2965 \fn bool &QVariant::asBool()
2967 Use toBool() instead.
2971 \fn double &QVariant::asDouble()
2973 Use toDouble() instead.
2977 \fn QByteArray &QVariant::asByteArray()
2979 Use toByteArray() instead.
2983 \fn QBitArray &QVariant::asBitArray()
2985 Use toBitArray() instead.
2989 \fn QString &QVariant::asString()
2991 Use toString() instead.
2995 \fn QStringList &QVariant::asStringList()
2997 Use toStringList() instead.
3001 \fn QDate &QVariant::asDate()
3003 Use toDate() instead.
3007 \fn QTime &QVariant::asTime()
3009 Use toTime() instead.
3013 \fn QDateTime &QVariant::asDateTime()
3015 Use toDateTime() instead.
3019 \fn QList<QVariant> &QVariant::asList()
3021 Use toList() instead.
3025 \fn QMap<QString, QVariant> &QVariant::asMap()
3027 Use toMap() instead.
3031 \fn QVariant::QVariant(bool b, int dummy)
3033 Use the QVariant(bool) constructor instead.
3038 \fn const QByteArray QVariant::toCString() const
3040 Use toByteArray() instead.
3044 \fn QByteArray &QVariant::asCString()
3046 Use toByteArray() instead.
3050 \fn QPoint &QVariant::asPoint()
3052 Use toPoint() instead.
3056 \fn QRect &QVariant::asRect()
3058 Use toRect() instead.
3062 \fn QSize &QVariant::asSize()
3064 Use toSize() instead.
3067 /*! \fn void QVariant::setValue(const T &value)
3069 Stores a copy of \a value. If \c{T} is a type that QVariant
3070 doesn't support, QMetaType is used to store the value. A compile
3071 error will occur if QMetaType doesn't handle the type.
3075 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 4
3077 \sa value(), fromValue(), canConvert()
3080 /*! \fn T QVariant::value() const
3082 Returns the stored value converted to the template type \c{T}.
3083 Call canConvert() to find out whether a type can be converted.
3084 If the value cannot be converted, \l{default-constructed value}
3087 If the type \c{T} is supported by QVariant, this function behaves
3088 exactly as toString(), toInt() etc.
3092 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 5
3094 \sa setValue(), fromValue(), canConvert()
3097 /*! \fn bool QVariant::canConvert() const
3099 Returns true if the variant can be converted to the template type \c{T},
3104 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 6
3109 /*! \fn static QVariant QVariant::fromValue(const T &value)
3111 Returns a QVariant containing a copy of \a value. Behaves
3112 exactly like setValue() otherwise.
3116 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 7
3118 \note If you are working with custom types, you should use
3119 the Q_DECLARE_METATYPE() macro to register your custom type.
3121 \sa setValue(), value()
3125 \fn QVariant qVariantFromValue(const T &value)
3129 Returns a variant containing a copy of the given \a value
3130 with template type \c{T}.
3132 This function is equivalent to QVariant::fromValue(\a value).
3134 \note This function was provided as a workaround for MSVC 6
3135 which did not support member template functions. It is advised
3136 to use the other form in new code.
3138 For example, a QObject pointer can be stored in a variant with the
3141 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 8
3143 \sa QVariant::fromValue()
3146 /*! \fn void qVariantSetValue(QVariant &variant, const T &value)
3150 Sets the contents of the given \a variant to a copy of the
3151 \a value with the specified template type \c{T}.
3153 This function is equivalent to QVariant::setValue(\a value).
3155 \note This function was provided as a workaround for MSVC 6
3156 which did not support member template functions. It is advised
3157 to use the other form in new code.
3159 \sa QVariant::setValue()
3163 \fn T qvariant_cast(const QVariant &value)
3166 Returns the given \a value converted to the template type \c{T}.
3168 This function is equivalent to QVariant::value().
3170 \sa QVariant::value()
3173 /*! \fn T qVariantValue(const QVariant &value)
3177 Returns the given \a value converted to the template type \c{T}.
3179 This function is equivalent to
3180 \l{QVariant::value()}{QVariant::value}<T>(\a value).
3182 \note This function was provided as a workaround for MSVC 6
3183 which did not support member template functions. It is advised
3184 to use the other form in new code.
3186 \sa QVariant::value(), qvariant_cast()
3189 /*! \fn bool qVariantCanConvert(const QVariant &value)
3193 Returns true if the given \a value can be converted to the
3194 template type specified; otherwise returns false.
3196 This function is equivalent to QVariant::canConvert(\a value).
3198 \note This function was provided as a workaround for MSVC 6
3199 which did not support member template functions. It is advised
3200 to use the other form in new code.
3202 \sa QVariant::canConvert()
3206 \typedef QVariantList
3209 Synonym for QList<QVariant>.
3213 \typedef QVariantMap
3216 Synonym for QMap<QString, QVariant>.
3220 \typedef QVariantHash
3224 Synonym for QHash<QString, QVariant>.
3228 \typedef QVariant::DataPtr
3233 \fn DataPtr &QVariant::data_ptr()