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 ** 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"
55 #include "private/qvariant_p.h"
56 #include "qmetatype_p.h"
58 #ifndef QT_NO_GEOM_VARIANT
79 static const QVariant::Handler *Handlers[QModulesPrivate::ModulesCount];
81 const QVariant::Handler *operator[] (const int typeId) const
83 return Handlers[QModulesPrivate::moduleForType(typeId)];
86 void registerHandler(const QModulesPrivate::Names name, const QVariant::Handler *handler)
88 Handlers[name] = handler;
91 inline void unregisterHandler(const QModulesPrivate::Names name);
97 struct TypeDefiniton {
98 static const bool IsAvailable = true;
101 // Ignore these types, as incomplete
102 #ifdef QT_BOOTSTRAPPED
103 template<> struct TypeDefiniton<QEasingCurve> { static const bool IsAvailable = false; };
105 #ifdef QT_NO_GEOM_VARIANT
106 template<> struct TypeDefiniton<QRect> { static const bool IsAvailable = false; };
107 template<> struct TypeDefiniton<QRectF> { static const bool IsAvailable = false; };
108 template<> struct TypeDefiniton<QSize> { static const bool IsAvailable = false; };
109 template<> struct TypeDefiniton<QSizeF> { static const bool IsAvailable = false; };
110 template<> struct TypeDefiniton<QLine> { static const bool IsAvailable = false; };
111 template<> struct TypeDefiniton<QLineF> { static const bool IsAvailable = false; };
112 template<> struct TypeDefiniton<QPoint> { static const bool IsAvailable = false; };
113 template<> struct TypeDefiniton<QPointF> { static const bool IsAvailable = false; };
116 struct CoreTypesFilter {
119 static const bool IsAccepted = QTypeModuleInfo<T>::IsCore && TypeDefiniton<T>::IsAvailable;
122 } // annonymous used to hide TypeDefiniton
124 namespace { // annonymous used to hide QVariant handlers
126 static void construct(QVariant::Private *x, const void *copy)
128 QVariantConstructor<CoreTypesFilter> constructor(x, copy);
129 QMetaTypeSwitcher::switcher<void>(constructor, x->type, 0);
132 static void clear(QVariant::Private *d)
134 QVariantDestructor<CoreTypesFilter> cleaner(d);
135 QMetaTypeSwitcher::switcher<void>(cleaner, d->type, 0);
138 static bool isNull(const QVariant::Private *d)
140 QVariantIsNull<CoreTypesFilter> isNull(d);
141 return QMetaTypeSwitcher::switcher<bool>(isNull, d->type, 0);
147 Compares \a a to \a b. The caller guarantees that \a a and \a b
148 are of the same type.
150 static bool compare(const QVariant::Private *a, const QVariant::Private *b)
152 QVariantComparator<CoreTypesFilter> comparator(a, b);
153 return QMetaTypeSwitcher::switcher<bool>(comparator, a->type, 0);
159 static qlonglong qMetaTypeNumber(const QVariant::Private *d)
164 case QMetaType::LongLong:
166 case QMetaType::Char:
167 return qlonglong(d->data.c);
168 case QMetaType::Short:
169 return qlonglong(d->data.s);
170 case QMetaType::Long:
171 return qlonglong(d->data.l);
172 case QMetaType::Float:
173 return qRound64(d->data.f);
174 case QVariant::Double:
175 return qRound64(d->data.d);
181 static qulonglong qMetaTypeUNumber(const QVariant::Private *d)
186 case QVariant::ULongLong:
188 case QMetaType::UChar:
190 case QMetaType::UShort:
192 case QMetaType::ULong:
199 static qlonglong qConvertToNumber(const QVariant::Private *d, bool *ok)
203 switch (uint(d->type)) {
204 case QVariant::String:
205 return v_cast<QString>(d)->toLongLong(ok);
207 return v_cast<QChar>(d)->unicode();
208 case QVariant::ByteArray:
209 return v_cast<QByteArray>(d)->toLongLong(ok);
211 return qlonglong(d->data.b);
212 case QVariant::Double:
214 case QMetaType::Char:
215 case QMetaType::Short:
216 case QMetaType::Long:
217 case QMetaType::Float:
218 case QMetaType::LongLong:
219 return qMetaTypeNumber(d);
220 case QVariant::ULongLong:
222 case QMetaType::UChar:
223 case QMetaType::UShort:
224 case QMetaType::ULong:
225 return qlonglong(qMetaTypeUNumber(d));
232 static qulonglong qConvertToUnsignedNumber(const QVariant::Private *d, bool *ok)
236 switch (uint(d->type)) {
237 case QVariant::String:
238 return v_cast<QString>(d)->toULongLong(ok);
240 return v_cast<QChar>(d)->unicode();
241 case QVariant::ByteArray:
242 return v_cast<QByteArray>(d)->toULongLong(ok);
244 return qulonglong(d->data.b);
245 case QVariant::Double:
247 case QMetaType::Char:
248 case QMetaType::Short:
249 case QMetaType::Long:
250 case QMetaType::Float:
251 case QMetaType::LongLong:
252 return qulonglong(qMetaTypeNumber(d));
253 case QVariant::ULongLong:
255 case QMetaType::UChar:
256 case QMetaType::UShort:
257 case QMetaType::ULong:
258 return qMetaTypeUNumber(d);
262 return Q_UINT64_C(0);
265 template<typename TInput, typename LiteralWrapper>
266 inline bool qt_convertToBool(const QVariant::Private *const d)
268 TInput str = v_cast<TInput>(d)->toLower();
269 return !(str == LiteralWrapper("0") || str == LiteralWrapper("false") || str.isEmpty());
275 Converts \a d to type \a t, which is placed in \a result.
277 static bool convert(const QVariant::Private *d, QVariant::Type t, void *result, bool *ok)
279 Q_ASSERT(d->type != uint(t));
289 case QVariant::String:
290 *static_cast<QUrl *>(result) = QUrl(*v_cast<QString>(d));
296 case QVariant::String: {
297 QString *str = static_cast<QString *>(result);
300 *str = QString(*v_cast<QChar>(d));
302 case QMetaType::Char:
303 case QMetaType::UChar:
304 *str = QChar::fromAscii(d->data.c);
306 case QMetaType::Short:
307 case QMetaType::Long:
309 case QVariant::LongLong:
310 *str = QString::number(qMetaTypeNumber(d));
313 case QVariant::ULongLong:
314 case QMetaType::UShort:
315 case QMetaType::ULong:
316 *str = QString::number(qMetaTypeUNumber(d));
318 case QMetaType::Float:
319 *str = QString::number(d->data.f, 'g', FLT_DIG);
321 case QVariant::Double:
322 *str = QString::number(d->data.d, 'g', DBL_DIG);
324 #if !defined(QT_NO_DATESTRING)
326 *str = v_cast<QDate>(d)->toString(Qt::ISODate);
329 *str = v_cast<QTime>(d)->toString(Qt::ISODate);
331 case QVariant::DateTime:
332 *str = v_cast<QDateTime>(d)->toString(Qt::ISODate);
336 *str = QLatin1String(d->data.b ? "true" : "false");
338 case QVariant::ByteArray:
339 *str = QString::fromAscii(v_cast<QByteArray>(d)->constData());
341 case QVariant::StringList:
342 if (v_cast<QStringList>(d)->count() == 1)
343 *str = v_cast<QStringList>(d)->at(0);
346 *str = v_cast<QUrl>(d)->toString();
353 case QVariant::Char: {
354 QChar *c = static_cast<QChar *>(result);
357 case QVariant::LongLong:
358 case QMetaType::Char:
359 case QMetaType::Short:
360 case QMetaType::Long:
361 case QMetaType::Float:
362 *c = QChar(ushort(qMetaTypeNumber(d)));
365 case QVariant::ULongLong:
366 case QMetaType::UChar:
367 case QMetaType::UShort:
368 case QMetaType::ULong:
369 *c = QChar(ushort(qMetaTypeUNumber(d)));
376 #ifndef QT_NO_GEOM_VARIANT
377 case QVariant::Size: {
378 QSize *s = static_cast<QSize *>(result);
380 case QVariant::SizeF:
381 *s = v_cast<QSizeF>(d)->toSize();
389 case QVariant::SizeF: {
390 QSizeF *s = static_cast<QSizeF *>(result);
393 *s = QSizeF(*(v_cast<QSize>(d)));
401 case QVariant::Line: {
402 QLine *s = static_cast<QLine *>(result);
404 case QVariant::LineF:
405 *s = v_cast<QLineF>(d)->toLine();
413 case QVariant::LineF: {
414 QLineF *s = static_cast<QLineF *>(result);
417 *s = QLineF(*(v_cast<QLine>(d)));
425 case QVariant::StringList:
426 if (d->type == QVariant::List) {
427 QStringList *slst = static_cast<QStringList *>(result);
428 const QVariantList *list = v_cast<QVariantList >(d);
429 for (int i = 0; i < list->size(); ++i)
430 slst->append(list->at(i).toString());
431 } else if (d->type == QVariant::String) {
432 QStringList *slst = static_cast<QStringList *>(result);
433 *slst = QStringList(*v_cast<QString>(d));
438 case QVariant::Date: {
439 QDate *dt = static_cast<QDate *>(result);
440 if (d->type == QVariant::DateTime)
441 *dt = v_cast<QDateTime>(d)->date();
442 #ifndef QT_NO_DATESTRING
443 else if (d->type == QVariant::String)
444 *dt = QDate::fromString(*v_cast<QString>(d), Qt::ISODate);
449 return dt->isValid();
451 case QVariant::Time: {
452 QTime *t = static_cast<QTime *>(result);
454 case QVariant::DateTime:
455 *t = v_cast<QDateTime>(d)->time();
457 #ifndef QT_NO_DATESTRING
458 case QVariant::String:
459 *t = QTime::fromString(*v_cast<QString>(d), Qt::ISODate);
467 case QVariant::DateTime: {
468 QDateTime *dt = static_cast<QDateTime *>(result);
470 #ifndef QT_NO_DATESTRING
471 case QVariant::String:
472 *dt = QDateTime::fromString(*v_cast<QString>(d), Qt::ISODate);
476 *dt = QDateTime(*v_cast<QDate>(d));
481 return dt->isValid();
483 case QVariant::ByteArray: {
484 QByteArray *ba = static_cast<QByteArray *>(result);
486 case QVariant::String:
487 *ba = v_cast<QString>(d)->toAscii();
489 case QVariant::Double:
490 *ba = QByteArray::number(d->data.d, 'g', DBL_DIG);
492 case QMetaType::Float:
493 *ba = QByteArray::number(d->data.f, 'g', FLT_DIG);
495 case QMetaType::Char:
496 case QMetaType::UChar:
497 *ba = QByteArray(1, d->data.c);
500 case QVariant::LongLong:
501 case QMetaType::Short:
502 case QMetaType::Long:
503 *ba = QByteArray::number(qMetaTypeNumber(d));
506 case QVariant::ULongLong:
507 case QMetaType::UShort:
508 case QMetaType::ULong:
509 *ba = QByteArray::number(qMetaTypeUNumber(d));
512 *ba = QByteArray(d->data.b ? "true" : "false");
519 case QMetaType::Short:
520 *static_cast<short *>(result) = short(qConvertToNumber(d, ok));
522 case QMetaType::Long:
523 *static_cast<long *>(result) = long(qConvertToNumber(d, ok));
525 case QMetaType::UShort:
526 *static_cast<ushort *>(result) = ushort(qConvertToUnsignedNumber(d, ok));
528 case QMetaType::ULong:
529 *static_cast<ulong *>(result) = ulong(qConvertToUnsignedNumber(d, ok));
532 *static_cast<int *>(result) = int(qConvertToNumber(d, ok));
535 *static_cast<uint *>(result) = uint(qConvertToUnsignedNumber(d, ok));
537 case QVariant::LongLong:
538 *static_cast<qlonglong *>(result) = qConvertToNumber(d, ok);
540 case QVariant::ULongLong: {
541 *static_cast<qulonglong *>(result) = qConvertToUnsignedNumber(d, ok);
544 case QMetaType::UChar: {
545 *static_cast<uchar *>(result) = qConvertToUnsignedNumber(d, ok);
548 case QVariant::Bool: {
549 bool *b = static_cast<bool *>(result);
551 case QVariant::ByteArray:
552 *b = qt_convertToBool<QByteArray, QByteArray>(d);
554 case QVariant::String:
555 *b = qt_convertToBool<QString, QLatin1String>(d);
558 *b = !v_cast<QChar>(d)->isNull();
560 case QVariant::Double:
562 case QVariant::LongLong:
563 case QMetaType::Char:
564 case QMetaType::Short:
565 case QMetaType::Long:
566 case QMetaType::Float:
567 *b = qMetaTypeNumber(d) != Q_INT64_C(0);
570 case QVariant::ULongLong:
571 case QMetaType::UChar:
572 case QMetaType::UShort:
573 case QMetaType::ULong:
574 *b = qMetaTypeUNumber(d) != Q_UINT64_C(0);
582 case QVariant::Double: {
583 double *f = static_cast<double *>(result);
585 case QVariant::String:
586 *f = v_cast<QString>(d)->toDouble(ok);
588 case QVariant::ByteArray:
589 *f = v_cast<QByteArray>(d)->toDouble(ok);
592 *f = double(d->data.b);
594 case QMetaType::Float:
595 *f = double(d->data.f);
597 case QVariant::LongLong:
599 case QMetaType::Char:
600 case QMetaType::Short:
601 case QMetaType::Long:
602 *f = double(qMetaTypeNumber(d));
605 case QVariant::ULongLong:
606 case QMetaType::UChar:
607 case QMetaType::UShort:
608 case QMetaType::ULong:
609 *f = double(qMetaTypeUNumber(d));
617 case QMetaType::Float: {
618 float *f = static_cast<float *>(result);
620 case QVariant::String:
621 *f = v_cast<QString>(d)->toFloat(ok);
623 case QVariant::ByteArray:
624 *f = v_cast<QByteArray>(d)->toFloat(ok);
627 *f = float(d->data.b);
629 case QVariant::Double:
630 *f = float(d->data.d);
632 case QVariant::LongLong:
634 case QMetaType::Char:
635 case QMetaType::Short:
636 case QMetaType::Long:
637 *f = float(qMetaTypeNumber(d));
640 case QVariant::ULongLong:
641 case QMetaType::UChar:
642 case QMetaType::UShort:
643 case QMetaType::ULong:
644 *f = float(qMetaTypeUNumber(d));
653 if (d->type == QVariant::StringList) {
654 QVariantList *lst = static_cast<QVariantList *>(result);
655 const QStringList *slist = v_cast<QStringList>(d);
656 for (int i = 0; i < slist->size(); ++i)
657 lst->append(QVariant(slist->at(i)));
658 } else if (qstrcmp(QMetaType::typeName(d->type), "QList<QVariant>") == 0) {
659 *static_cast<QVariantList *>(result) =
660 *static_cast<QList<QVariant> *>(d->data.shared->ptr);
666 if (qstrcmp(QMetaType::typeName(d->type), "QMap<QString, QVariant>") == 0) {
667 *static_cast<QVariantMap *>(result) =
668 *static_cast<QMap<QString, QVariant> *>(d->data.shared->ptr);
674 if (qstrcmp(QMetaType::typeName(d->type), "QHash<QString, QVariant>") == 0) {
675 *static_cast<QVariantHash *>(result) =
676 *static_cast<QHash<QString, QVariant> *>(d->data.shared->ptr);
681 #ifndef QT_NO_GEOM_VARIANT
683 if (d->type == QVariant::RectF)
684 *static_cast<QRect *>(result) = (v_cast<QRectF>(d))->toRect();
688 case QVariant::RectF:
689 if (d->type == QVariant::Rect)
690 *static_cast<QRectF *>(result) = *v_cast<QRect>(d);
694 case QVariant::PointF:
695 if (d->type == QVariant::Point)
696 *static_cast<QPointF *>(result) = *v_cast<QPoint>(d);
700 case QVariant::Point:
701 if (d->type == QVariant::PointF)
702 *static_cast<QPoint *>(result) = (v_cast<QPointF>(d))->toPoint();
706 case QMetaType::Char:
708 *static_cast<qint8 *>(result) = qint8(qConvertToNumber(d, ok));
718 #if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)
719 static void streamDebug(QDebug dbg, const QVariant &v)
721 switch (v.userType()) {
723 dbg.nospace() << v.toInt();
726 dbg.nospace() << v.toUInt();
728 case QVariant::LongLong:
729 dbg.nospace() << v.toLongLong();
731 case QVariant::ULongLong:
732 dbg.nospace() << v.toULongLong();
734 case QMetaType::Float:
735 dbg.nospace() << v.toFloat();
737 case QMetaType::QObjectStar:
738 dbg.nospace() << qvariant_cast<QObject *>(v);
740 case QVariant::Double:
741 dbg.nospace() << v.toDouble();
744 dbg.nospace() << v.toBool();
746 case QVariant::String:
747 dbg.nospace() << v.toString();
750 dbg.nospace() << v.toChar();
752 case QVariant::StringList:
753 dbg.nospace() << v.toStringList();
756 dbg.nospace() << v.toMap();
759 dbg.nospace() << v.toHash();
762 dbg.nospace() << v.toList();
765 dbg.nospace() << v.toDate();
768 dbg.nospace() << v.toTime();
770 case QVariant::DateTime:
771 dbg.nospace() << v.toDateTime();
773 #ifndef QT_BOOTSTRAPPED
774 case QVariant::EasingCurve:
775 dbg.nospace() << v.toEasingCurve();
778 case QVariant::ByteArray:
779 dbg.nospace() << v.toByteArray();
782 dbg.nospace() << v.toUrl();
784 #ifndef QT_NO_GEOM_VARIANT
785 case QVariant::Point:
786 dbg.nospace() << v.toPoint();
788 case QVariant::PointF:
789 dbg.nospace() << v.toPointF();
792 dbg.nospace() << v.toRect();
795 dbg.nospace() << v.toSize();
797 case QVariant::SizeF:
798 dbg.nospace() << v.toSizeF();
801 dbg.nospace() << v.toLine();
803 case QVariant::LineF:
804 dbg.nospace() << v.toLineF();
806 case QVariant::RectF:
807 dbg.nospace() << v.toRectF();
810 case QVariant::BitArray:
811 //dbg.nospace() << v.toBitArray();
819 const QVariant::Handler qt_kernel_variant_handler = {
823 #ifndef QT_NO_DATASTREAM
830 #if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)
837 static void dummyConstruct(QVariant::Private *, const void *) { Q_ASSERT_X(false, "QVariant", "Trying to construct an unknown type"); }
838 static void dummyClear(QVariant::Private *) { Q_ASSERT_X(false, "QVariant", "Trying to clear an unknown type"); }
839 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; }
840 static bool dummyCompare(const QVariant::Private *, const QVariant::Private *) { Q_ASSERT_X(false, "QVariant", "Trying to compare an unknown types"); return false; }
841 static bool dummyConvert(const QVariant::Private *, QVariant::Type , void *, bool *) { Q_ASSERT_X(false, "QVariant", "Trying to convert an unknown type"); return false; }
842 #if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)
843 static void dummyStreamDebug(QDebug, const QVariant &) { Q_ASSERT_X(false, "QVariant", "Trying to convert an unknown type"); }
845 const QVariant::Handler qt_dummy_variant_handler = {
849 #ifndef QT_NO_DATASTREAM
856 #if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)
863 static void customConstruct(QVariant::Private *d, const void *copy)
865 const uint size = QMetaType::sizeOf(d->type);
867 d->type = QVariant::Invalid;
871 // this logic should match with QVariantIntegrator::CanUseInternalSpace
872 if (size <= sizeof(QVariant::Private::Data)
873 && (QMetaType::typeFlags(d->type) & QMetaType::MovableType)) {
874 QMetaType::construct(d->type, &d->data.ptr, copy);
875 d->is_shared = false;
877 void *ptr = QMetaType::create(d->type, copy);
879 d->data.shared = new QVariant::PrivateShared(ptr);
883 static void customClear(QVariant::Private *d)
886 QMetaType::destruct(d->type, &d->data.ptr);
888 QMetaType::destroy(d->type, d->data.shared->ptr);
889 delete d->data.shared;
893 static bool customIsNull(const QVariant::Private *d)
898 static bool customCompare(const QVariant::Private *a, const QVariant::Private *b)
900 const char *const typeName = QMetaType::typeName(a->type);
901 if (Q_UNLIKELY(!typeName) && Q_LIKELY(!QMetaType::isRegistered(a->type)))
902 qFatal("QVariant::compare: type %d unknown to QVariant.", a->type);
904 const void *a_ptr = a->is_shared ? a->data.shared->ptr : &(a->data.ptr);
905 const void *b_ptr = b->is_shared ? b->data.shared->ptr : &(b->data.ptr);
907 uint typeNameLen = qstrlen(typeName);
908 if (typeNameLen > 0 && typeName[typeNameLen - 1] == '*')
909 return *static_cast<void *const *>(a_ptr) == *static_cast<void *const *>(b_ptr);
911 if (a->is_null && b->is_null)
914 return !memcmp(a_ptr, b_ptr, QMetaType::sizeOf(a->type));
917 static bool customConvert(const QVariant::Private *, QVariant::Type, void *, bool *ok)
924 #if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)
925 static void customStreamDebug(QDebug, const QVariant &) {}
928 const QVariant::Handler qt_custom_variant_handler = {
932 #ifndef QT_NO_DATASTREAM
939 #if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)
946 } // annonymous used to hide QVariant handlers
948 static HandlersManager handlerManager;
949 Q_STATIC_ASSERT_X(!QModulesPrivate::Core, "Initialization assumes that ModulesNames::Core is 0");
950 const QVariant::Handler *HandlersManager::Handlers[QModulesPrivate::ModulesCount]
951 = { &qt_kernel_variant_handler, &qt_dummy_variant_handler,
952 &qt_dummy_variant_handler, &qt_custom_variant_handler };
954 Q_CORE_EXPORT const QVariant::Handler *qcoreVariantHandler()
956 return &qt_kernel_variant_handler;
959 inline void HandlersManager::unregisterHandler(const QModulesPrivate::Names name)
961 Handlers[name] = &qt_dummy_variant_handler;
964 Q_CORE_EXPORT void QVariantPrivate::registerHandler(const int /* Modules::Names */name, const QVariant::Handler *handler)
966 handlerManager.registerHandler(static_cast<QModulesPrivate::Names>(name), handler);
969 Q_CORE_EXPORT void QVariantPrivate::unregisterHandler(const int /* Modules::Names */ name)
971 handlerManager.unregisterHandler(static_cast<QModulesPrivate::Names>(name));
976 \brief The QVariant class acts like a union for the most common Qt data types.
982 Because C++ forbids unions from including types that have
983 non-default constructors or destructors, most interesting Qt
984 classes cannot be used in unions. Without QVariant, this would be
985 a problem for QObject::property() and for database work, etc.
987 A QVariant object holds a single value of a single type() at a
988 time. (Some type()s are multi-valued, for example a string list.)
989 You can find out what type, T, the variant holds, convert it to a
990 different type using convert(), get its value using one of the
991 toT() functions (e.g., toSize()) and check whether the type can
992 be converted to a particular type using canConvert().
994 The methods named toT() (e.g., toInt(), toString()) are const. If
995 you ask for the stored type, they return a copy of the stored
996 object. If you ask for a type that can be generated from the
997 stored type, toT() copies and converts and leaves the object
998 itself unchanged. If you ask for a type that cannot be generated
999 from the stored type, the result depends on the type; see the
1000 function documentation for details.
1002 Here is some example code to demonstrate the use of QVariant:
1004 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 0
1006 You can even store QList<QVariant> and QMap<QString, QVariant>
1007 values in a variant, so you can easily construct arbitrarily
1008 complex data structures of arbitrary types. This is very powerful
1009 and versatile, but may prove less memory and speed efficient than
1010 storing specific types in standard data structures.
1012 QVariant also supports the notion of null values, where you can
1013 have a defined type with no value set. However, note that QVariant
1014 types can only be cast when they have had a value set.
1016 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 1
1018 QVariant can be extended to support other types than those
1019 mentioned in the \l Type enum. See the \l QMetaType documentation
1022 \section1 A Note on GUI Types
1024 Because QVariant is part of the QtCore library, it cannot provide
1025 conversion functions to data types defined in QtGui, such as
1026 QColor, QImage, and QPixmap. In other words, there is no \c
1027 toColor() function. Instead, you can use the QVariant::value() or
1028 the qvariant_cast() template function. For example:
1030 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 2
1032 The inverse conversion (e.g., from QColor to QVariant) is
1033 automatic for all data types supported by QVariant, including
1036 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 3
1038 \section1 Using canConvert() and convert() Consecutively
1040 When using canConvert() and convert() consecutively, it is possible for
1041 canConvert() to return true, but convert() to return false. This
1042 is typically because canConvert() only reports the general ability of
1043 QVariant to convert between types given suitable data; it is still
1044 possible to supply data which cannot actually be converted.
1046 For example, canConvert() would return true when called on a variant
1047 containing a string because, in principle, QVariant is able to convert
1048 strings of numbers to integers.
1049 However, if the string contains non-numeric characters, it cannot be
1050 converted to an integer, and any attempt to convert it will fail.
1051 Hence, it is important to have both functions return true for a
1052 successful conversion.
1058 \enum QVariant::Type
1060 This enum type defines the types of variable that a QVariant can
1063 \value Invalid no type
1064 \value BitArray a QBitArray
1065 \value Bitmap a QBitmap
1067 \value Brush a QBrush
1068 \value ByteArray a QByteArray
1070 \value Color a QColor
1071 \value Cursor a QCursor
1073 \value DateTime a QDateTime
1074 \value Double a double
1075 \value EasingCurve a QEasingCurve
1077 \value Hash a QVariantHash
1079 \value Image a QImage
1081 \value KeySequence a QKeySequence
1083 \value LineF a QLineF
1084 \value List a QVariantList
1085 \value Locale a QLocale
1086 \value LongLong a \l qlonglong
1087 \value Map a QVariantMap
1088 \value Matrix a QMatrix
1089 \value Transform a QTransform
1090 \value Matrix4x4 a QMatrix4x4
1091 \value Palette a QPalette
1093 \value Pixmap a QPixmap
1094 \value Point a QPoint
1095 \value PointF a QPointF
1096 \value Polygon a QPolygon
1097 \value PolygonF a QPolygonF
1098 \value Quaternion a QQuaternion
1100 \value RectF a QRectF
1101 \value RegExp a QRegExp
1102 \value Region a QRegion
1104 \value SizeF a QSizeF
1105 \value SizePolicy a QSizePolicy
1106 \value String a QString
1107 \value StringList a QStringList
1108 \value TextFormat a QTextFormat
1109 \value TextLength a QTextLength
1111 \value UInt a \l uint
1112 \value ULongLong a \l qulonglong
1114 \value Vector2D a QVector2D
1115 \value Vector3D a QVector3D
1116 \value Vector4D a QVector4D
1118 \value UserType Base value for user-defined types.
1121 \omitvalue ColorGroup
1123 \omitvalue LastGuiType
1124 \omitvalue LastCoreType
1129 \fn QVariant::QVariant()
1131 Constructs an invalid variant.
1136 \fn QVariant::QVariant(int typeOrUserType, const void *copy)
1138 Constructs variant of type \a typeOrUserType, and initializes with
1139 \a copy if \a copy is not 0.
1141 Note that you have to pass the address of the variable you want stored.
1143 Usually, you never have to use this constructor, use QVariant::fromValue()
1144 instead to construct variants from the pointer types represented by
1145 \c QMetaType::VoidStar, \c QMetaType::QObjectStar and
1146 \c QMetaType::QWidgetStar.
1148 \sa QVariant::fromValue(), Type
1152 \fn QVariant::QVariant(Type type)
1154 Constructs a null variant of type \a type.
1160 \fn QVariant::create(int type, const void *copy)
1164 Constructs a variant private of type \a type, and initializes with \a copy if
1168 void QVariant::create(int type, const void *copy)
1171 handlerManager[type]->construct(&d, copy);
1175 \fn QVariant::~QVariant()
1177 Destroys the QVariant and the contained object.
1179 Note that subclasses that reimplement clear() should reimplement
1180 the destructor to call clear(). This destructor calls clear(), but
1181 because it is the destructor, QVariant::clear() is called rather
1182 than a subclass's clear().
1185 QVariant::~QVariant()
1187 if ((d.is_shared && !d.data.shared->ref.deref()) || (!d.is_shared && d.type > Char))
1188 handlerManager[d.type]->clear(&d);
1192 \fn QVariant::QVariant(const QVariant &p)
1194 Constructs a copy of the variant, \a p, passed as the argument to
1198 QVariant::QVariant(const QVariant &p)
1202 d.data.shared->ref.ref();
1203 } else if (p.d.type > Char) {
1204 handlerManager[d.type]->construct(&d, p.constData());
1205 d.is_null = p.d.is_null;
1209 #ifndef QT_NO_DATASTREAM
1211 Reads the variant from the data stream, \a s.
1213 QVariant::QVariant(QDataStream &s)
1218 #endif //QT_NO_DATASTREAM
1221 \fn QVariant::QVariant(const QString &val)
1223 Constructs a new variant with a string value, \a val.
1227 \fn QVariant::QVariant(const QLatin1String &val)
1229 Constructs a new variant with a string value, \a val.
1233 \fn QVariant::QVariant(const char *val)
1235 Constructs a new variant with a string value of \a val.
1236 The variant creates a deep copy of \a val, using the encoding
1237 set by QTextCodec::setCodecForCStrings().
1239 Note that \a val is converted to a QString for storing in the
1240 variant and QVariant::type() will return QMetaType::QString for
1243 You can disable this operator by defining \c
1244 QT_NO_CAST_FROM_ASCII when you compile your applications.
1246 \sa QTextCodec::setCodecForCStrings()
1249 #ifndef QT_NO_CAST_FROM_ASCII
1250 QVariant::QVariant(const char *val)
1252 QString s = QString::fromAscii(val);
1258 \fn QVariant::QVariant(const QStringList &val)
1260 Constructs a new variant with a string list value, \a val.
1264 \fn QVariant::QVariant(const QMap<QString, QVariant> &val)
1266 Constructs a new variant with a map of QVariants, \a val.
1270 \fn QVariant::QVariant(const QHash<QString, QVariant> &val)
1272 Constructs a new variant with a hash of QVariants, \a val.
1276 \fn QVariant::QVariant(const QDate &val)
1278 Constructs a new variant with a date value, \a val.
1282 \fn QVariant::QVariant(const QTime &val)
1284 Constructs a new variant with a time value, \a val.
1288 \fn QVariant::QVariant(const QDateTime &val)
1290 Constructs a new variant with a date/time value, \a val.
1295 \fn QVariant::QVariant(const QEasingCurve &val)
1297 Constructs a new variant with an easing curve value, \a val.
1301 \fn QVariant::QVariant(const QByteArray &val)
1303 Constructs a new variant with a bytearray value, \a val.
1307 \fn QVariant::QVariant(const QBitArray &val)
1309 Constructs a new variant with a bitarray value, \a val.
1313 \fn QVariant::QVariant(const QPoint &val)
1315 Constructs a new variant with a point value of \a val.
1319 \fn QVariant::QVariant(const QPointF &val)
1321 Constructs a new variant with a point value of \a val.
1325 \fn QVariant::QVariant(const QRectF &val)
1327 Constructs a new variant with a rect value of \a val.
1331 \fn QVariant::QVariant(const QLineF &val)
1333 Constructs a new variant with a line value of \a val.
1337 \fn QVariant::QVariant(const QLine &val)
1339 Constructs a new variant with a line value of \a val.
1343 \fn QVariant::QVariant(const QRect &val)
1345 Constructs a new variant with a rect value of \a val.
1349 \fn QVariant::QVariant(const QSize &val)
1351 Constructs a new variant with a size value of \a val.
1355 \fn QVariant::QVariant(const QSizeF &val)
1357 Constructs a new variant with a size value of \a val.
1361 \fn QVariant::QVariant(const QUrl &val)
1363 Constructs a new variant with a url value of \a val.
1367 \fn QVariant::QVariant(int val)
1369 Constructs a new variant with an integer value, \a val.
1373 \fn QVariant::QVariant(uint val)
1375 Constructs a new variant with an unsigned integer value, \a val.
1379 \fn QVariant::QVariant(qlonglong val)
1381 Constructs a new variant with a long long integer value, \a val.
1385 \fn QVariant::QVariant(qulonglong val)
1387 Constructs a new variant with an unsigned long long integer value, \a val.
1392 \fn QVariant::QVariant(bool val)
1394 Constructs a new variant with a boolean value, \a val.
1398 \fn QVariant::QVariant(double val)
1400 Constructs a new variant with a floating point value, \a val.
1404 \fn QVariant::QVariant(float val)
1406 Constructs a new variant with a floating point value, \a val.
1411 \fn QVariant::QVariant(const QList<QVariant> &val)
1413 Constructs a new variant with a list value, \a val.
1417 \fn QVariant::QVariant(const QChar &c)
1419 Constructs a new variant with a char value, \a c.
1423 \fn QVariant::QVariant(const QLocale &l)
1425 Constructs a new variant with a locale value, \a l.
1429 \fn QVariant::QVariant(const QRegExp ®Exp)
1431 Constructs a new variant with the regexp value \a regExp.
1435 \fn QVariant::QVariant(Qt::GlobalColor color)
1437 Constructs a new variant of type QVariant::Color and initializes
1440 This is a convenience constructor that allows \c{QVariant(Qt::blue);}
1441 to create a valid QVariant storing a QColor.
1443 Note: This constructor will assert if the application does not link
1444 to the Qt GUI library.
1447 QVariant::QVariant(Type type)
1448 { create(type, 0); }
1449 QVariant::QVariant(int typeOrUserType, const void *copy)
1450 { create(typeOrUserType, copy); d.is_null = false; }
1453 flags is true if it is a pointer type
1455 QVariant::QVariant(int typeOrUserType, const void *copy, uint flags)
1457 if (flags) { //type is a pointer type
1458 d.type = typeOrUserType;
1459 d.data.ptr = *reinterpret_cast<void *const*>(copy);
1461 create(typeOrUserType, copy);
1466 QVariant::QVariant(int val)
1467 { d.is_null = false; d.type = Int; d.data.i = val; }
1468 QVariant::QVariant(uint val)
1469 { d.is_null = false; d.type = UInt; d.data.u = val; }
1470 QVariant::QVariant(qlonglong val)
1471 { d.is_null = false; d.type = LongLong; d.data.ll = val; }
1472 QVariant::QVariant(qulonglong val)
1473 { d.is_null = false; d.type = ULongLong; d.data.ull = val; }
1474 QVariant::QVariant(bool val)
1475 { d.is_null = false; d.type = Bool; d.data.b = val; }
1476 QVariant::QVariant(double val)
1477 { d.is_null = false; d.type = Double; d.data.d = val; }
1479 QVariant::QVariant(const QByteArray &val)
1480 { d.is_null = false; d.type = ByteArray; v_construct<QByteArray>(&d, val); }
1481 QVariant::QVariant(const QBitArray &val)
1482 { d.is_null = false; d.type = BitArray; v_construct<QBitArray>(&d, val); }
1483 QVariant::QVariant(const QString &val)
1484 { d.is_null = false; d.type = String; v_construct<QString>(&d, val); }
1485 QVariant::QVariant(const QChar &val)
1486 { d.is_null = false; d.type = Char; v_construct<QChar>(&d, val); }
1487 QVariant::QVariant(const QLatin1String &val)
1488 { QString str(val); d.is_null = false; d.type = String; v_construct<QString>(&d, str); }
1489 QVariant::QVariant(const QStringList &val)
1490 { d.is_null = false; d.type = StringList; v_construct<QStringList>(&d, val); }
1492 QVariant::QVariant(const QDate &val)
1493 { d.is_null = false; d.type = Date; v_construct<QDate>(&d, val); }
1494 QVariant::QVariant(const QTime &val)
1495 { d.is_null = false; d.type = Time; v_construct<QTime>(&d, val); }
1496 QVariant::QVariant(const QDateTime &val)
1497 { d.is_null = false; d.type = DateTime; v_construct<QDateTime>(&d, val); }
1498 #ifndef QT_BOOTSTRAPPED
1499 QVariant::QVariant(const QEasingCurve &val)
1500 { d.is_null = false; d.type = EasingCurve; v_construct<QEasingCurve>(&d, val); }
1502 QVariant::QVariant(const QList<QVariant> &list)
1503 { d.is_null = false; d.type = List; v_construct<QVariantList>(&d, list); }
1504 QVariant::QVariant(const QMap<QString, QVariant> &map)
1505 { d.is_null = false; d.type = Map; v_construct<QVariantMap>(&d, map); }
1506 QVariant::QVariant(const QHash<QString, QVariant> &hash)
1507 { d.is_null = false; d.type = Hash; v_construct<QVariantHash>(&d, hash); }
1508 #ifndef QT_NO_GEOM_VARIANT
1509 QVariant::QVariant(const QPoint &pt) { d.is_null = false; d.type = Point; v_construct<QPoint>(&d, pt); }
1510 QVariant::QVariant(const QPointF &pt) { d.is_null = false; d.type = PointF; v_construct<QPointF>(&d, pt); }
1511 QVariant::QVariant(const QRectF &r) { d.is_null = false; d.type = RectF; v_construct<QRectF>(&d, r); }
1512 QVariant::QVariant(const QLineF &l) { d.is_null = false; d.type = LineF; v_construct<QLineF>(&d, l); }
1513 QVariant::QVariant(const QLine &l) { d.is_null = false; d.type = Line; v_construct<QLine>(&d, l); }
1514 QVariant::QVariant(const QRect &r) { d.is_null = false; d.type = Rect; v_construct<QRect>(&d, r); }
1515 QVariant::QVariant(const QSize &s) { d.is_null = false; d.type = Size; v_construct<QSize>(&d, s); }
1516 QVariant::QVariant(const QSizeF &s) { d.is_null = false; d.type = SizeF; v_construct<QSizeF>(&d, s); }
1518 QVariant::QVariant(const QUrl &u) { d.is_null = false; d.type = Url; v_construct<QUrl>(&d, u); }
1519 QVariant::QVariant(const QLocale &l) { d.is_null = false; d.type = Locale; v_construct<QLocale>(&d, l); }
1520 #ifndef QT_NO_REGEXP
1521 QVariant::QVariant(const QRegExp ®Exp) { d.is_null = false; d.type = RegExp; v_construct<QRegExp>(&d, regExp); }
1523 QVariant::QVariant(Qt::GlobalColor color) { create(62, &color); }
1526 Returns the storage type of the value stored in the variant.
1527 Although this function is declared as returning QVariant::Type,
1528 the return value should be interpreted as QMetaType::Type. In
1529 particular, QVariant::UserType is returned here only if the value
1530 is equal or greater than QMetaType::User.
1532 Note that return values in the ranges QVariant::Char through
1533 QVariant::RegExp and QVariant::Font through QVariant::Transform
1534 correspond to the values in the ranges QMetaType::QChar through
1535 QMetaType::QRegExp and QMetaType::QFont through QMetaType::QQuaternion.
1537 Pay particular attention when working with char and QChar
1538 variants. Note that there is no QVariant constructor specifically
1539 for type char, but there is one for QChar. For a variant of type
1540 QChar, this function returns QVariant::Char, which is the same as
1541 QMetaType::QChar, but for a variant of type \c char, this function
1542 returns QMetaType::Char, which is \e not the same as
1545 Also note that the types \c void*, \c long, \c short, \c unsigned
1546 \c long, \c unsigned \c short, \c unsigned \c char, \c float, \c
1547 QObject*, and \c QWidget* are represented in QMetaType::Type but
1548 not in QVariant::Type, and they can be returned by this function.
1549 However, they are considered to be user defined types when tested
1550 against QVariant::Type.
1552 To test whether an instance of QVariant contains a data type that
1553 is compatible with the data type you are interested in, use
1557 QVariant::Type QVariant::type() const
1559 return d.type >= QMetaType::User ? UserType : static_cast<Type>(d.type);
1563 Returns the storage type of the value stored in the variant. For
1564 non-user types, this is the same as type().
1569 int QVariant::userType() const
1575 Assigns the value of the variant \a variant to this variant.
1577 QVariant& QVariant::operator=(const QVariant &variant)
1579 if (this == &variant)
1583 if (variant.d.is_shared) {
1584 variant.d.data.shared->ref.ref();
1586 } else if (variant.d.type > Char) {
1587 d.type = variant.d.type;
1588 handlerManager[d.type]->construct(&d, variant.constData());
1589 d.is_null = variant.d.is_null;
1598 \fn void QVariant::swap(QVariant &other)
1601 Swaps variant \a other with this variant. This operation is very
1602 fast and never fails.
1606 \fn void QVariant::detach()
1611 void QVariant::detach()
1613 if (!d.is_shared || d.data.shared->ref.load() == 1)
1618 handlerManager[d.type]->construct(&dd, constData());
1619 if (!d.data.shared->ref.deref())
1620 handlerManager[d.type]->clear(&d);
1621 d.data.shared = dd.data.shared;
1625 \fn bool QVariant::isDetached() const
1630 // ### Qt 5: change typeName()(and froends= to return a QString. Suggestion from Harald.
1632 Returns the name of the type stored in the variant. The returned
1633 strings describe the C++ datatype used to store the data: for
1634 example, "QFont", "QString", or "QVariantList". An Invalid
1637 const char *QVariant::typeName() const
1639 return typeToName(Type(d.type));
1643 Convert this variant to type Invalid and free up any resources
1646 void QVariant::clear()
1648 if ((d.is_shared && !d.data.shared->ref.deref()) || (!d.is_shared && d.type > Char))
1649 handlerManager[d.type]->clear(&d);
1652 d.is_shared = false;
1656 Converts the enum representation of the storage type, \a typ, to
1657 its string representation.
1659 Returns a null pointer if the type is QVariant::Invalid or doesn't exist.
1661 const char *QVariant::typeToName(Type typ)
1665 if (typ == UserType)
1668 return QMetaType::typeName(typ);
1673 Converts the string representation of the storage type given in \a
1674 name, to its enum representation.
1676 If the string representation cannot be converted to any enum
1677 representation, the variant is set to \c Invalid.
1679 QVariant::Type QVariant::nameToType(const char *name)
1681 if (!name || !*name)
1683 if (strcmp(name, "Q3CString") == 0)
1685 if (strcmp(name, "Q_LLONG") == 0)
1687 if (strcmp(name, "Q_ULLONG") == 0)
1689 if (strcmp(name, "QIconSet") == 0)
1691 if (strcmp(name, "UserType") == 0)
1694 int metaType = QMetaType::type(name);
1695 return metaType <= int(LastGuiType) ? QVariant::Type(metaType) : UserType;
1698 #ifndef QT_NO_DATASTREAM
1699 enum { MapFromThreeCount = 36 };
1700 static const ushort map_from_three[MapFromThreeCount] =
1706 QVariant::StringList,
1722 QVariant::ByteArray,
1727 QVariant::SizePolicy,
1731 QVariant::ByteArray,
1733 QVariant::KeySequence,
1736 QVariant::ULongLong,
1737 QVariant::EasingCurve
1741 Internal function for loading a variant from stream \a s. Use the
1742 stream operators instead.
1746 void QVariant::load(QDataStream &s)
1752 if (s.version() < QDataStream::Qt_4_0) {
1753 if (u >= MapFromThreeCount)
1755 u = map_from_three[u];
1757 qint8 is_null = false;
1758 if (s.version() >= QDataStream::Qt_4_2)
1760 if (u == QVariant::UserType) {
1763 u = QMetaType::type(name);
1765 s.setStatus(QDataStream::ReadCorruptData);
1769 create(static_cast<int>(u), 0);
1770 d.is_null = is_null;
1773 // Since we wrote something, we should read something
1780 // const cast is safe since we operate on a newly constructed variant
1781 if (!QMetaType::load(s, d.type, const_cast<void *>(constData()))) {
1782 s.setStatus(QDataStream::ReadCorruptData);
1783 qWarning("QVariant::load: unable to load type %d.", d.type);
1788 Internal function for saving a variant to the stream \a s. Use the
1789 stream operators instead.
1793 void QVariant::save(QDataStream &s) const
1795 quint32 tp = type();
1796 if (s.version() < QDataStream::Qt_4_0) {
1798 for (i = MapFromThreeCount - 1; i >= 0; i--) {
1799 if (map_from_three[i] == tp) {
1810 if (s.version() >= QDataStream::Qt_4_2)
1811 s << qint8(d.is_null);
1812 if (tp == QVariant::UserType) {
1813 s << QMetaType::typeName(userType());
1821 if (!QMetaType::save(s, d.type, constData())) {
1822 Q_ASSERT_X(false, "QVariant::save", "Invalid type to save");
1823 qWarning("QVariant::save: unable to save type %d.", d.type);
1830 Reads a variant \a p from the stream \a s.
1832 \sa \link datastreamformat.html Format of the QDataStream
1835 QDataStream& operator>>(QDataStream &s, QVariant &p)
1842 Writes a variant \a p to the stream \a s.
1844 \sa \link datastreamformat.html Format of the QDataStream
1847 QDataStream& operator<<(QDataStream &s, const QVariant &p)
1854 Reads a variant type \a p in enum representation from the stream \a s.
1856 QDataStream& operator>>(QDataStream &s, QVariant::Type &p)
1860 p = (QVariant::Type)u;
1866 Writes a variant type \a p to the stream \a s.
1868 QDataStream& operator<<(QDataStream &s, const QVariant::Type p)
1870 s << static_cast<quint32>(p);
1875 #endif //QT_NO_DATASTREAM
1878 \fn bool QVariant::isValid() const
1880 Returns true if the storage type of this variant is not
1881 QVariant::Invalid; otherwise returns false.
1884 template <typename T>
1885 inline T qVariantToHelper(const QVariant::Private &d, QVariant::Type t, const HandlersManager &handler)
1888 return *v_cast<T>(&d);
1891 handler[d.type]->convert(&d, t, &ret, 0);
1896 \fn QStringList QVariant::toStringList() const
1898 Returns the variant as a QStringList if the variant has type()
1899 StringList, \l String, or \l List of a type that can be converted
1900 to QString; otherwise returns an empty list.
1902 \sa canConvert(), convert()
1904 QStringList QVariant::toStringList() const
1906 return qVariantToHelper<QStringList>(d, StringList, handlerManager);
1910 Returns the variant as a QString if the variant has type() \l
1911 String, \l Bool, \l ByteArray, \l Char, \l Date, \l DateTime, \l
1912 Double, \l Int, \l LongLong, \l StringList, \l Time, \l UInt, or
1913 \l ULongLong; otherwise returns an empty string.
1915 \sa canConvert(), convert()
1917 QString QVariant::toString() const
1919 return qVariantToHelper<QString>(d, String, handlerManager);
1923 Returns the variant as a QMap<QString, QVariant> if the variant
1924 has type() \l Map; otherwise returns an empty map.
1926 \sa canConvert(), convert()
1928 QVariantMap QVariant::toMap() const
1930 return qVariantToHelper<QVariantMap>(d, Map, handlerManager);
1934 Returns the variant as a QHash<QString, QVariant> if the variant
1935 has type() \l Hash; otherwise returns an empty map.
1937 \sa canConvert(), convert()
1939 QVariantHash QVariant::toHash() const
1941 return qVariantToHelper<QVariantHash>(d, Hash, handlerManager);
1945 \fn QDate QVariant::toDate() const
1947 Returns the variant as a QDate if the variant has type() \l Date,
1948 \l DateTime, or \l String; otherwise returns an invalid date.
1950 If the type() is \l String, an invalid date will be returned if the
1951 string cannot be parsed as a Qt::ISODate format date.
1953 \sa canConvert(), convert()
1955 QDate QVariant::toDate() const
1957 return qVariantToHelper<QDate>(d, Date, handlerManager);
1961 \fn QTime QVariant::toTime() const
1963 Returns the variant as a QTime if the variant has type() \l Time,
1964 \l DateTime, or \l String; otherwise returns an invalid time.
1966 If the type() is \l String, an invalid time will be returned if
1967 the string cannot be parsed as a Qt::ISODate format time.
1969 \sa canConvert(), convert()
1971 QTime QVariant::toTime() const
1973 return qVariantToHelper<QTime>(d, Time, handlerManager);
1977 \fn QDateTime QVariant::toDateTime() const
1979 Returns the variant as a QDateTime if the variant has type() \l
1980 DateTime, \l Date, or \l String; otherwise returns an invalid
1983 If the type() is \l String, an invalid date/time will be returned
1984 if the string cannot be parsed as a Qt::ISODate format date/time.
1986 \sa canConvert(), convert()
1988 QDateTime QVariant::toDateTime() const
1990 return qVariantToHelper<QDateTime>(d, DateTime, handlerManager);
1995 \fn QEasingCurve QVariant::toEasingCurve() const
1997 Returns the variant as a QEasingCurve if the variant has type() \l
1998 EasingCurve; otherwise returns a default easing curve.
2000 \sa canConvert(), convert()
2002 #ifndef QT_BOOTSTRAPPED
2003 QEasingCurve QVariant::toEasingCurve() const
2005 return qVariantToHelper<QEasingCurve>(d, EasingCurve, handlerManager);
2010 \fn QByteArray QVariant::toByteArray() const
2012 Returns the variant as a QByteArray if the variant has type() \l
2013 ByteArray or \l String (converted using QString::fromAscii());
2014 otherwise returns an empty byte array.
2016 \sa canConvert(), convert()
2018 QByteArray QVariant::toByteArray() const
2020 return qVariantToHelper<QByteArray>(d, ByteArray, handlerManager);
2023 #ifndef QT_NO_GEOM_VARIANT
2025 \fn QPoint QVariant::toPoint() const
2027 Returns the variant as a QPoint if the variant has type()
2028 \l Point or \l PointF; otherwise returns a null QPoint.
2030 \sa canConvert(), convert()
2032 QPoint QVariant::toPoint() const
2034 return qVariantToHelper<QPoint>(d, Point, handlerManager);
2038 \fn QRect QVariant::toRect() const
2040 Returns the variant as a QRect if the variant has type() \l Rect;
2041 otherwise returns an invalid QRect.
2043 \sa canConvert(), convert()
2045 QRect QVariant::toRect() const
2047 return qVariantToHelper<QRect>(d, Rect, handlerManager);
2051 \fn QSize QVariant::toSize() const
2053 Returns the variant as a QSize if the variant has type() \l Size;
2054 otherwise returns an invalid QSize.
2056 \sa canConvert(), convert()
2058 QSize QVariant::toSize() const
2060 return qVariantToHelper<QSize>(d, Size, handlerManager);
2064 \fn QSizeF QVariant::toSizeF() const
2066 Returns the variant as a QSizeF if the variant has type() \l
2067 SizeF; otherwise returns an invalid QSizeF.
2069 \sa canConvert(), convert()
2071 QSizeF QVariant::toSizeF() const
2073 return qVariantToHelper<QSizeF>(d, SizeF, handlerManager);
2077 \fn QRectF QVariant::toRectF() const
2079 Returns the variant as a QRectF if the variant has type() \l Rect
2080 or \l RectF; otherwise returns an invalid QRectF.
2082 \sa canConvert(), convert()
2084 QRectF QVariant::toRectF() const
2086 return qVariantToHelper<QRectF>(d, RectF, handlerManager);
2090 \fn QLineF QVariant::toLineF() const
2092 Returns the variant as a QLineF if the variant has type() \l
2093 LineF; otherwise returns an invalid QLineF.
2095 \sa canConvert(), convert()
2097 QLineF QVariant::toLineF() const
2099 return qVariantToHelper<QLineF>(d, LineF, handlerManager);
2103 \fn QLine QVariant::toLine() const
2105 Returns the variant as a QLine if the variant has type() \l Line;
2106 otherwise returns an invalid QLine.
2108 \sa canConvert(), convert()
2110 QLine QVariant::toLine() const
2112 return qVariantToHelper<QLine>(d, Line, handlerManager);
2116 \fn QPointF QVariant::toPointF() const
2118 Returns the variant as a QPointF if the variant has type() \l
2119 Point or \l PointF; otherwise returns a null QPointF.
2121 \sa canConvert(), convert()
2123 QPointF QVariant::toPointF() const
2125 return qVariantToHelper<QPointF>(d, PointF, handlerManager);
2128 #endif // QT_NO_GEOM_VARIANT
2131 \fn QUrl QVariant::toUrl() const
2133 Returns the variant as a QUrl if the variant has type()
2134 \l Url; otherwise returns an invalid QUrl.
2136 \sa canConvert(), convert()
2138 QUrl QVariant::toUrl() const
2140 return qVariantToHelper<QUrl>(d, Url, handlerManager);
2144 \fn QLocale QVariant::toLocale() const
2146 Returns the variant as a QLocale if the variant has type()
2147 \l Locale; otherwise returns an invalid QLocale.
2149 \sa canConvert(), convert()
2151 QLocale QVariant::toLocale() const
2153 return qVariantToHelper<QLocale>(d, Locale, handlerManager);
2157 \fn QRegExp QVariant::toRegExp() const
2160 Returns the variant as a QRegExp if the variant has type() \l
2161 RegExp; otherwise returns an empty QRegExp.
2163 \sa canConvert(), convert()
2165 #ifndef QT_NO_REGEXP
2166 QRegExp QVariant::toRegExp() const
2168 return qVariantToHelper<QRegExp>(d, RegExp, handlerManager);
2173 \fn QChar QVariant::toChar() const
2175 Returns the variant as a QChar if the variant has type() \l Char,
2176 \l Int, or \l UInt; otherwise returns an invalid QChar.
2178 \sa canConvert(), convert()
2180 QChar QVariant::toChar() const
2182 return qVariantToHelper<QChar>(d, Char, handlerManager);
2186 Returns the variant as a QBitArray if the variant has type()
2187 \l BitArray; otherwise returns an empty bit array.
2189 \sa canConvert(), convert()
2191 QBitArray QVariant::toBitArray() const
2193 return qVariantToHelper<QBitArray>(d, BitArray, handlerManager);
2196 template <typename T>
2197 inline T qNumVariantToHelper(const QVariant::Private &d,
2198 const HandlersManager &handler, bool *ok, const T& val)
2200 uint t = qMetaTypeId<T>();
2207 if (!handler[d.type]->convert(&d, QVariant::Type(t), &ret, ok) && ok)
2213 Returns the variant as an int if the variant has type() \l Int,
2214 \l Bool, \l ByteArray, \l Char, \l Double, \l LongLong, \l
2215 String, \l UInt, or \l ULongLong; otherwise returns 0.
2217 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2218 converted to an int; otherwise \c{*}\a{ok} is set to false.
2220 \bold{Warning:} If the value is convertible to a \l LongLong but is too
2221 large to be represented in an int, the resulting arithmetic overflow will
2222 not be reflected in \a ok. A simple workaround is to use QString::toInt().
2223 Fixing this bug has been postponed to Qt 5 in order to avoid breaking existing code.
2225 \sa canConvert(), convert()
2227 int QVariant::toInt(bool *ok) const
2229 return qNumVariantToHelper<int>(d, handlerManager, ok, d.data.i);
2233 Returns the variant as an unsigned int if the variant has type()
2234 \l UInt, \l Bool, \l ByteArray, \l Char, \l Double, \l Int, \l
2235 LongLong, \l String, or \l ULongLong; otherwise returns 0.
2237 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2238 converted to an unsigned int; otherwise \c{*}\a{ok} is set to false.
2240 \bold{Warning:} If the value is convertible to a \l ULongLong but is too
2241 large to be represented in an unsigned int, the resulting arithmetic overflow will
2242 not be reflected in \a ok. A simple workaround is to use QString::toUInt().
2243 Fixing this bug has been postponed to Qt 5 in order to avoid breaking existing code.
2245 \sa canConvert(), convert()
2247 uint QVariant::toUInt(bool *ok) const
2249 return qNumVariantToHelper<uint>(d, handlerManager, ok, d.data.u);
2253 Returns the variant as a long long int if the variant has type()
2254 \l LongLong, \l Bool, \l ByteArray, \l Char, \l Double, \l Int,
2255 \l String, \l UInt, or \l ULongLong; otherwise returns 0.
2257 If \a ok is non-null: \c{*}\c{ok} is set to true if the value could be
2258 converted to an int; otherwise \c{*}\c{ok} is set to false.
2260 \sa canConvert(), convert()
2262 qlonglong QVariant::toLongLong(bool *ok) const
2264 return qNumVariantToHelper<qlonglong>(d, handlerManager, ok, d.data.ll);
2268 Returns the variant as as an unsigned long long int if the
2269 variant has type() \l ULongLong, \l Bool, \l ByteArray, \l Char,
2270 \l Double, \l Int, \l LongLong, \l String, or \l UInt; otherwise
2273 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2274 converted to an int; otherwise \c{*}\a{ok} is set to false.
2276 \sa canConvert(), convert()
2278 qulonglong QVariant::toULongLong(bool *ok) const
2280 return qNumVariantToHelper<qulonglong>(d, handlerManager, ok, d.data.ull);
2284 Returns the variant as a bool if the variant has type() Bool.
2286 Returns true if the variant has type() \l Bool, \l Char, \l Double,
2287 \l Int, \l LongLong, \l UInt, or \l ULongLong and the value is
2288 non-zero, or if the variant has type \l String or \l ByteArray and
2289 its lower-case content is not empty, "0" or "false"; otherwise
2292 \sa canConvert(), convert()
2294 bool QVariant::toBool() const
2300 handlerManager[d.type]->convert(&d, Bool, &res, 0);
2306 Returns the variant as a double if the variant has type() \l
2307 Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
2308 UInt, or \l ULongLong; otherwise returns 0.0.
2310 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2311 converted to a double; otherwise \c{*}\a{ok} is set to false.
2313 \sa canConvert(), convert()
2315 double QVariant::toDouble(bool *ok) const
2317 return qNumVariantToHelper<double>(d, handlerManager, ok, d.data.d);
2321 Returns the variant as a float if the variant has type() \l
2322 Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
2323 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 float QVariant::toFloat(bool *ok) const
2334 return qNumVariantToHelper<float>(d, handlerManager, ok, d.data.f);
2338 Returns the variant as a qreal 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 qreal QVariant::toReal(bool *ok) const
2351 return qNumVariantToHelper<qreal>(d, handlerManager, ok, d.data.real);
2355 Returns the variant as a QVariantList if the variant has type()
2356 \l List or \l StringList; otherwise returns an empty list.
2358 \sa canConvert(), convert()
2360 QVariantList QVariant::toList() const
2362 return qVariantToHelper<QVariantList>(d, List, handlerManager);
2366 static const quint32 qCanConvertMatrix[QVariant::LastCoreType + 1] =
2370 /*Bool*/ 1 << QVariant::Double | 1 << QVariant::Int | 1 << QVariant::UInt
2371 | 1 << QVariant::LongLong | 1 << QVariant::ULongLong | 1 << QVariant::ByteArray
2372 | 1 << QVariant::String | 1 << QVariant::Char,
2374 /*Int*/ 1 << QVariant::UInt | 1 << QVariant::String | 1 << QVariant::Double
2375 | 1 << QVariant::Bool | 1 << QVariant::LongLong | 1 << QVariant::ULongLong
2376 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2378 /*UInt*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::Double
2379 | 1 << QVariant::Bool | 1 << QVariant::LongLong | 1 << QVariant::ULongLong
2380 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2382 /*LLong*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::Double
2383 | 1 << QVariant::Bool | 1 << QVariant::UInt | 1 << QVariant::ULongLong
2384 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2386 /*ULlong*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::Double
2387 | 1 << QVariant::Bool | 1 << QVariant::UInt | 1 << QVariant::LongLong
2388 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2390 /*double*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::ULongLong
2391 | 1 << QVariant::Bool | 1 << QVariant::UInt | 1 << QVariant::LongLong
2392 | 1 << QVariant::ByteArray,
2394 /*QChar*/ 1 << QVariant::Int | 1 << QVariant::UInt | 1 << QVariant::LongLong
2395 | 1 << QVariant::ULongLong,
2399 /*QList*/ 1 << QVariant::StringList,
2401 /*QString*/ 1 << QVariant::StringList | 1 << QVariant::ByteArray | 1 << QVariant::Int
2402 | 1 << QVariant::UInt | 1 << QVariant::Bool | 1 << QVariant::Double
2403 | 1 << QVariant::Date | 1 << QVariant::Time | 1 << QVariant::DateTime
2404 | 1 << QVariant::LongLong | 1 << QVariant::ULongLong | 1 << QVariant::Char
2405 | 1 << QVariant::Url,
2407 /*QStringList*/ 1 << QVariant::List | 1 << QVariant::String,
2409 /*QByteArray*/ 1 << QVariant::String | 1 << QVariant::Int | 1 << QVariant::UInt | 1 << QVariant::Bool
2410 | 1 << QVariant::Double | 1 << QVariant::LongLong | 1 << QVariant::ULongLong,
2414 /*QDate*/ 1 << QVariant::String | 1 << QVariant::DateTime,
2416 /*QTime*/ 1 << QVariant::String | 1 << QVariant::DateTime,
2418 /*QDateTime*/ 1 << QVariant::String | 1 << QVariant::Date,
2420 /*QUrl*/ 1 << QVariant::String,
2424 /*QRect*/ 1 << QVariant::RectF,
2426 /*QRectF*/ 1 << QVariant::Rect,
2428 /*QSize*/ 1 << QVariant::SizeF,
2430 /*QSizeF*/ 1 << QVariant::Size,
2432 /*QLine*/ 1 << QVariant::LineF,
2434 /*QLineF*/ 1 << QVariant::Line,
2436 /*QPoint*/ 1 << QVariant::PointF,
2438 /*QPointF*/ 1 << QVariant::Point,
2448 Returns true if the variant's type can be cast to the requested
2449 type, \a t. Such casting is done automatically when calling the
2450 toInt(), toBool(), ... methods.
2452 The following casts are done automatically:
2455 \header \o Type \o Automatically Cast To
2456 \row \o \l Bool \o \l Char, \l Double, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2457 \row \o \l ByteArray \o \l Double, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2458 \row \o \l Char \o \l Bool, \l Int, \l UInt, \l LongLong, \l ULongLong
2459 \row \o \l Color \o \l String
2460 \row \o \l Date \o \l DateTime, \l String
2461 \row \o \l DateTime \o \l Date, \l String, \l Time
2462 \row \o \l Double \o \l Bool, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2463 \row \o \l Font \o \l String
2464 \row \o \l Int \o \l Bool, \l Char, \l Double, \l LongLong, \l String, \l UInt, \l ULongLong
2465 \row \o \l KeySequence \o \l Int, \l String
2466 \row \o \l List \o \l StringList (if the list's items can be converted to strings)
2467 \row \o \l LongLong \o \l Bool, \l ByteArray, \l Char, \l Double, \l Int, \l String, \l UInt, \l ULongLong
2468 \row \o \l Point \o PointF
2469 \row \o \l Rect \o RectF
2470 \row \o \l String \o \l Bool, \l ByteArray, \l Char, \l Color, \l Date, \l DateTime, \l Double,
2471 \l Font, \l Int, \l KeySequence, \l LongLong, \l StringList, \l Time, \l UInt,
2473 \row \o \l StringList \o \l List, \l String (if the list contains exactly one item)
2474 \row \o \l Time \o \l String
2475 \row \o \l UInt \o \l Bool, \l Char, \l Double, \l Int, \l LongLong, \l String, \l ULongLong
2476 \row \o \l ULongLong \o \l Bool, \l Char, \l Double, \l Int, \l LongLong, \l String, \l UInt
2481 bool QVariant::canConvert(Type t) const
2483 //we can treat floats as double
2484 //the reason for not doing it the "proper" way is that QMetaType::Float's value is 135,
2485 //which can't be handled by qCanConvertMatrix
2486 //In addition QVariant::Type doesn't have a Float value, so we're using QMetaType::Float
2487 const uint currentType = ((d.type == QMetaType::Float) ? QVariant::Double : d.type);
2488 if (uint(t) == uint(QMetaType::Float)) t = QVariant::Double;
2490 if (currentType == uint(t))
2493 if (currentType > QVariant::LastCoreType || t > QVariant::LastCoreType) {
2496 return currentType == QVariant::KeySequence
2497 || currentType == QMetaType::ULong
2498 || currentType == QMetaType::Long
2499 || currentType == QMetaType::UShort
2500 || currentType == QMetaType::UChar
2501 || currentType == QMetaType::Char
2502 || currentType == QMetaType::Short;
2503 case QVariant::Image:
2504 return currentType == QVariant::Pixmap || currentType == QVariant::Bitmap;
2505 case QVariant::Pixmap:
2506 return currentType == QVariant::Image || currentType == QVariant::Bitmap
2507 || currentType == QVariant::Brush;
2508 case QVariant::Bitmap:
2509 return currentType == QVariant::Pixmap || currentType == QVariant::Image;
2510 case QVariant::ByteArray:
2511 return currentType == QVariant::Color;
2512 case QVariant::String:
2513 return currentType == QVariant::KeySequence || currentType == QVariant::Font
2514 || currentType == QVariant::Color;
2515 case QVariant::KeySequence:
2516 return currentType == QVariant::String || currentType == QVariant::Int;
2517 case QVariant::Font:
2518 return currentType == QVariant::String;
2519 case QVariant::Color:
2520 return currentType == QVariant::String || currentType == QVariant::ByteArray
2521 || currentType == QVariant::Brush;
2522 case QVariant::Brush:
2523 return currentType == QVariant::Color || currentType == QVariant::Pixmap;
2524 case QMetaType::Long:
2525 case QMetaType::Char:
2526 case QMetaType::UChar:
2527 case QMetaType::ULong:
2528 case QMetaType::Short:
2529 case QMetaType::UShort:
2530 return qCanConvertMatrix[QVariant::Int] & (1 << currentType) || currentType == QVariant::Int;
2536 if(t == String && currentType == StringList)
2537 return v_cast<QStringList>(&d)->count() == 1;
2539 return qCanConvertMatrix[t] & (1 << currentType);
2543 Casts the variant to the requested type, \a t. If the cast cannot be
2544 done, the variant is cleared. Returns true if the current type of
2545 the variant was successfully cast; otherwise returns false.
2547 \warning For historical reasons, converting a null QVariant results
2548 in a null value of the desired type (e.g., an empty string for
2549 QString) and a result of false.
2551 \sa canConvert(), clear()
2554 bool QVariant::convert(Type t)
2556 if (d.type == uint(t))
2559 QVariant oldValue = *this;
2562 if (!oldValue.canConvert(t))
2566 if (oldValue.isNull())
2570 if (!handlerManager[d.type]->convert(&oldValue.d, t, data(), &isOk))
2577 \fn convert(const int type, void *ptr) const
2579 Created for qvariant_cast() usage
2581 bool QVariant::convert(const int type, void *ptr) const
2583 Q_ASSERT(type < int(QMetaType::User));
2584 return handlerManager[type]->convert(&d, QVariant::Type(type), ptr, 0);
2589 \fn bool operator==(const QVariant &v1, const QVariant &v2)
2593 Returns true if \a v1 and \a v2 are equal; otherwise returns false.
2595 \warning This function doesn't support custom types registered
2596 with qRegisterMetaType().
2599 \fn bool operator!=(const QVariant &v1, const QVariant &v2)
2603 Returns false if \a v1 and \a v2 are equal; otherwise returns true.
2605 \warning This function doesn't support custom types registered
2606 with qRegisterMetaType().
2609 /*! \fn bool QVariant::operator==(const QVariant &v) const
2611 Compares this QVariant with \a v and returns true if they are
2612 equal; otherwise returns false.
2614 In the case of custom types, their equalness operators are not called.
2615 Instead the values' addresses are compared.
2619 \fn bool QVariant::operator!=(const QVariant &v) const
2621 Compares this QVariant with \a v and returns true if they are not
2622 equal; otherwise returns false.
2624 \warning This function doesn't support custom types registered
2625 with qRegisterMetaType().
2628 static bool qIsNumericType(uint tp)
2630 return (tp >= QVariant::Bool && tp <= QVariant::Double)
2631 || (tp >= QMetaType::Long && tp <= QMetaType::Float);
2634 static bool qIsFloatingPoint(uint tp)
2636 return tp == QVariant::Double || tp == QMetaType::Float;
2641 bool QVariant::cmp(const QVariant &v) const
2644 if (d.type != v2.d.type) {
2645 if (qIsNumericType(d.type) && qIsNumericType(v.d.type)) {
2646 if (qIsFloatingPoint(d.type) || qIsFloatingPoint(v.d.type))
2647 return qFuzzyCompare(toReal(), v.toReal());
2649 return toLongLong() == v.toLongLong();
2651 if (!v2.canConvert(Type(d.type)) || !v2.convert(Type(d.type)))
2654 return handlerManager[d.type]->compare(&d, &v2.d);
2660 const void *QVariant::constData() const
2662 return d.is_shared ? d.data.shared->ptr : reinterpret_cast<const void *>(&d.data.ptr);
2666 \fn const void* QVariant::data() const
2672 void* QVariant::data()
2675 return const_cast<void *>(constData());
2680 Returns true if this is a NULL variant, false otherwise.
2682 bool QVariant::isNull() const
2684 return handlerManager[d.type]->isNull(&d);
2687 #ifndef QT_NO_DEBUG_STREAM
2688 QDebug operator<<(QDebug dbg, const QVariant &v)
2690 #ifndef Q_BROKEN_DEBUG_STREAM
2691 dbg.nospace() << "QVariant(" << v.typeName() << ", ";
2692 handlerManager[v.d.type]->debugStream(dbg, v);
2693 dbg.nospace() << ')';
2696 qWarning("This compiler doesn't support streaming QVariant to QDebug");
2702 QDebug operator<<(QDebug dbg, const QVariant::Type p)
2704 #ifndef Q_BROKEN_DEBUG_STREAM
2705 dbg.nospace() << "QVariant::" << QVariant::typeToName(p);
2708 qWarning("This compiler doesn't support streaming QVariant::Type to QDebug");
2716 /*! \fn void QVariant::setValue(const T &value)
2718 Stores a copy of \a value. If \c{T} is a type that QVariant
2719 doesn't support, QMetaType is used to store the value. A compile
2720 error will occur if QMetaType doesn't handle the type.
2724 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 4
2726 \sa value(), fromValue(), canConvert()
2729 /*! \fn T QVariant::value() const
2731 Returns the stored value converted to the template type \c{T}.
2732 Call canConvert() to find out whether a type can be converted.
2733 If the value cannot be converted, \l{default-constructed value}
2736 If the type \c{T} is supported by QVariant, this function behaves
2737 exactly as toString(), toInt() etc.
2741 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 5
2743 \sa setValue(), fromValue(), canConvert()
2746 /*! \fn bool QVariant::canConvert() const
2748 Returns true if the variant can be converted to the template type \c{T},
2753 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 6
2758 /*! \fn static QVariant QVariant::fromValue(const T &value)
2760 Returns a QVariant containing a copy of \a value. Behaves
2761 exactly like setValue() otherwise.
2765 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 7
2767 \note If you are working with custom types, you should use
2768 the Q_DECLARE_METATYPE() macro to register your custom type.
2770 \sa setValue(), value()
2774 \fn QVariant qVariantFromValue(const T &value)
2778 Returns a variant containing a copy of the given \a value
2779 with template type \c{T}.
2781 This function is equivalent to QVariant::fromValue(\a value).
2783 \note This function was provided as a workaround for MSVC 6
2784 which did not support member template functions. It is advised
2785 to use the other form in new code.
2787 For example, a QObject pointer can be stored in a variant with the
2790 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 8
2792 \sa QVariant::fromValue()
2795 /*! \fn void qVariantSetValue(QVariant &variant, const T &value)
2799 Sets the contents of the given \a variant to a copy of the
2800 \a value with the specified template type \c{T}.
2802 This function is equivalent to QVariant::setValue(\a value).
2804 \note This function was provided as a workaround for MSVC 6
2805 which did not support member template functions. It is advised
2806 to use the other form in new code.
2808 \sa QVariant::setValue()
2812 \fn T qvariant_cast(const QVariant &value)
2815 Returns the given \a value converted to the template type \c{T}.
2817 This function is equivalent to QVariant::value().
2819 \sa QVariant::value()
2822 /*! \fn T qVariantValue(const QVariant &value)
2826 Returns the given \a value converted to the template type \c{T}.
2828 This function is equivalent to
2829 \l{QVariant::value()}{QVariant::value}<T>(\a value).
2831 \note This function was provided as a workaround for MSVC 6
2832 which did not support member template functions. It is advised
2833 to use the other form in new code.
2835 \sa QVariant::value(), qvariant_cast()
2838 /*! \fn bool qVariantCanConvert(const QVariant &value)
2842 Returns true if the given \a value can be converted to the
2843 template type specified; otherwise returns false.
2845 This function is equivalent to QVariant::canConvert(\a value).
2847 \note This function was provided as a workaround for MSVC 6
2848 which did not support member template functions. It is advised
2849 to use the other form in new code.
2851 \sa QVariant::canConvert()
2855 \typedef QVariantList
2858 Synonym for QList<QVariant>.
2862 \typedef QVariantMap
2865 Synonym for QMap<QString, QVariant>.
2869 \typedef QVariantHash
2873 Synonym for QHash<QString, QVariant>.
2877 \typedef QVariant::DataPtr
2882 \fn DataPtr &QVariant::data_ptr()