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
77 struct TypeDefiniton {
78 static const bool IsAvailable = true;
81 // Ignore these types, as incomplete
82 #ifdef QT_BOOTSTRAPPED
83 template<> struct TypeDefiniton<QEasingCurve> { static const bool IsAvailable = false; };
85 #ifdef QT_NO_GEOM_VARIANT
86 template<> struct TypeDefiniton<QRect> { static const bool IsAvailable = false; };
87 template<> struct TypeDefiniton<QRectF> { static const bool IsAvailable = false; };
88 template<> struct TypeDefiniton<QSize> { static const bool IsAvailable = false; };
89 template<> struct TypeDefiniton<QSizeF> { static const bool IsAvailable = false; };
90 template<> struct TypeDefiniton<QLine> { static const bool IsAvailable = false; };
91 template<> struct TypeDefiniton<QLineF> { static const bool IsAvailable = false; };
92 template<> struct TypeDefiniton<QPoint> { static const bool IsAvailable = false; };
93 template<> struct TypeDefiniton<QPointF> { static const bool IsAvailable = false; };
96 struct CoreTypesFilter {
99 static const bool IsAccepted = QTypeModuleInfo<T>::IsCore && TypeDefiniton<T>::IsAvailable;
103 static void construct(QVariant::Private *x, const void *copy)
105 QVariantConstructor<CoreTypesFilter> constructor(x, copy);
106 QMetaTypeSwitcher::switcher<void>(constructor, x->type, 0);
109 static void clear(QVariant::Private *d)
111 QVariantDestructor<CoreTypesFilter> cleaner(d);
112 QMetaTypeSwitcher::switcher<void>(cleaner, d->type, 0);
115 static bool isNull(const QVariant::Private *d)
117 QVariantIsNull<CoreTypesFilter> isNull(d);
118 return QMetaTypeSwitcher::switcher<bool>(isNull, d->type, 0);
124 Compares \a a to \a b. The caller guarantees that \a a and \a b
125 are of the same type.
127 static bool compare(const QVariant::Private *a, const QVariant::Private *b)
129 QVariantComparator<CoreTypesFilter> comparator(a, b);
130 return QMetaTypeSwitcher::switcher<bool>(comparator, a->type, 0);
136 static qlonglong qMetaTypeNumber(const QVariant::Private *d)
141 case QMetaType::LongLong:
143 case QMetaType::Char:
144 return qlonglong(d->data.c);
145 case QMetaType::Short:
146 return qlonglong(d->data.s);
147 case QMetaType::Long:
148 return qlonglong(d->data.l);
149 case QMetaType::Float:
150 return qRound64(d->data.f);
151 case QVariant::Double:
152 return qRound64(d->data.d);
158 static qulonglong qMetaTypeUNumber(const QVariant::Private *d)
163 case QVariant::ULongLong:
165 case QMetaType::UChar:
167 case QMetaType::UShort:
169 case QMetaType::ULong:
176 static qlonglong qConvertToNumber(const QVariant::Private *d, bool *ok)
180 switch (uint(d->type)) {
181 case QVariant::String:
182 return v_cast<QString>(d)->toLongLong(ok);
184 return v_cast<QChar>(d)->unicode();
185 case QVariant::ByteArray:
186 return v_cast<QByteArray>(d)->toLongLong(ok);
188 return qlonglong(d->data.b);
189 case QVariant::Double:
191 case QMetaType::Char:
192 case QMetaType::Short:
193 case QMetaType::Long:
194 case QMetaType::Float:
195 case QMetaType::LongLong:
196 return qMetaTypeNumber(d);
197 case QVariant::ULongLong:
199 case QMetaType::UChar:
200 case QMetaType::UShort:
201 case QMetaType::ULong:
202 return qlonglong(qMetaTypeUNumber(d));
209 static qulonglong qConvertToUnsignedNumber(const QVariant::Private *d, bool *ok)
213 switch (uint(d->type)) {
214 case QVariant::String:
215 return v_cast<QString>(d)->toULongLong(ok);
217 return v_cast<QChar>(d)->unicode();
218 case QVariant::ByteArray:
219 return v_cast<QByteArray>(d)->toULongLong(ok);
221 return qulonglong(d->data.b);
222 case QVariant::Double:
224 case QMetaType::Char:
225 case QMetaType::Short:
226 case QMetaType::Long:
227 case QMetaType::Float:
228 case QMetaType::LongLong:
229 return qulonglong(qMetaTypeNumber(d));
230 case QVariant::ULongLong:
232 case QMetaType::UChar:
233 case QMetaType::UShort:
234 case QMetaType::ULong:
235 return qMetaTypeUNumber(d);
239 return Q_UINT64_C(0);
242 template<typename TInput, typename LiteralWrapper>
243 inline bool qt_convertToBool(const QVariant::Private *const d)
245 TInput str = v_cast<TInput>(d)->toLower();
246 return !(str == LiteralWrapper("0") || str == LiteralWrapper("false") || str.isEmpty());
252 Converts \a d to type \a t, which is placed in \a result.
254 static bool convert(const QVariant::Private *d, QVariant::Type t, void *result, bool *ok)
256 Q_ASSERT(d->type != uint(t));
266 case QVariant::String:
267 *static_cast<QUrl *>(result) = QUrl(*v_cast<QString>(d));
273 case QVariant::String: {
274 QString *str = static_cast<QString *>(result);
277 *str = QString(*v_cast<QChar>(d));
279 case QMetaType::Char:
280 case QMetaType::UChar:
281 *str = QChar::fromAscii(d->data.c);
283 case QMetaType::Short:
284 case QMetaType::Long:
286 case QVariant::LongLong:
287 *str = QString::number(qMetaTypeNumber(d));
290 case QVariant::ULongLong:
291 case QMetaType::UShort:
292 case QMetaType::ULong:
293 *str = QString::number(qMetaTypeUNumber(d));
295 case QMetaType::Float:
296 *str = QString::number(d->data.f, 'g', FLT_DIG);
298 case QVariant::Double:
299 *str = QString::number(d->data.d, 'g', DBL_DIG);
301 #if !defined(QT_NO_DATESTRING)
303 *str = v_cast<QDate>(d)->toString(Qt::ISODate);
306 *str = v_cast<QTime>(d)->toString(Qt::ISODate);
308 case QVariant::DateTime:
309 *str = v_cast<QDateTime>(d)->toString(Qt::ISODate);
313 *str = QLatin1String(d->data.b ? "true" : "false");
315 case QVariant::ByteArray:
316 *str = QString::fromAscii(v_cast<QByteArray>(d)->constData());
318 case QVariant::StringList:
319 if (v_cast<QStringList>(d)->count() == 1)
320 *str = v_cast<QStringList>(d)->at(0);
323 *str = v_cast<QUrl>(d)->toString();
330 case QVariant::Char: {
331 QChar *c = static_cast<QChar *>(result);
334 case QVariant::LongLong:
335 case QMetaType::Char:
336 case QMetaType::Short:
337 case QMetaType::Long:
338 case QMetaType::Float:
339 *c = QChar(ushort(qMetaTypeNumber(d)));
342 case QVariant::ULongLong:
343 case QMetaType::UChar:
344 case QMetaType::UShort:
345 case QMetaType::ULong:
346 *c = QChar(ushort(qMetaTypeUNumber(d)));
353 #ifndef QT_NO_GEOM_VARIANT
354 case QVariant::Size: {
355 QSize *s = static_cast<QSize *>(result);
357 case QVariant::SizeF:
358 *s = v_cast<QSizeF>(d)->toSize();
366 case QVariant::SizeF: {
367 QSizeF *s = static_cast<QSizeF *>(result);
370 *s = QSizeF(*(v_cast<QSize>(d)));
378 case QVariant::Line: {
379 QLine *s = static_cast<QLine *>(result);
381 case QVariant::LineF:
382 *s = v_cast<QLineF>(d)->toLine();
390 case QVariant::LineF: {
391 QLineF *s = static_cast<QLineF *>(result);
394 *s = QLineF(*(v_cast<QLine>(d)));
402 case QVariant::StringList:
403 if (d->type == QVariant::List) {
404 QStringList *slst = static_cast<QStringList *>(result);
405 const QVariantList *list = v_cast<QVariantList >(d);
406 for (int i = 0; i < list->size(); ++i)
407 slst->append(list->at(i).toString());
408 } else if (d->type == QVariant::String) {
409 QStringList *slst = static_cast<QStringList *>(result);
410 *slst = QStringList(*v_cast<QString>(d));
415 case QVariant::Date: {
416 QDate *dt = static_cast<QDate *>(result);
417 if (d->type == QVariant::DateTime)
418 *dt = v_cast<QDateTime>(d)->date();
419 #ifndef QT_NO_DATESTRING
420 else if (d->type == QVariant::String)
421 *dt = QDate::fromString(*v_cast<QString>(d), Qt::ISODate);
426 return dt->isValid();
428 case QVariant::Time: {
429 QTime *t = static_cast<QTime *>(result);
431 case QVariant::DateTime:
432 *t = v_cast<QDateTime>(d)->time();
434 #ifndef QT_NO_DATESTRING
435 case QVariant::String:
436 *t = QTime::fromString(*v_cast<QString>(d), Qt::ISODate);
444 case QVariant::DateTime: {
445 QDateTime *dt = static_cast<QDateTime *>(result);
447 #ifndef QT_NO_DATESTRING
448 case QVariant::String:
449 *dt = QDateTime::fromString(*v_cast<QString>(d), Qt::ISODate);
453 *dt = QDateTime(*v_cast<QDate>(d));
458 return dt->isValid();
460 case QVariant::ByteArray: {
461 QByteArray *ba = static_cast<QByteArray *>(result);
463 case QVariant::String:
464 *ba = v_cast<QString>(d)->toAscii();
466 case QVariant::Double:
467 *ba = QByteArray::number(d->data.d, 'g', DBL_DIG);
469 case QMetaType::Float:
470 *ba = QByteArray::number(d->data.f, 'g', FLT_DIG);
472 case QMetaType::Char:
473 case QMetaType::UChar:
474 *ba = QByteArray(1, d->data.c);
477 case QVariant::LongLong:
478 case QMetaType::Short:
479 case QMetaType::Long:
480 *ba = QByteArray::number(qMetaTypeNumber(d));
483 case QVariant::ULongLong:
484 case QMetaType::UShort:
485 case QMetaType::ULong:
486 *ba = QByteArray::number(qMetaTypeUNumber(d));
489 *ba = QByteArray(d->data.b ? "true" : "false");
496 case QMetaType::Short:
497 *static_cast<short *>(result) = short(qConvertToNumber(d, ok));
499 case QMetaType::Long:
500 *static_cast<long *>(result) = long(qConvertToNumber(d, ok));
502 case QMetaType::UShort:
503 *static_cast<ushort *>(result) = ushort(qConvertToUnsignedNumber(d, ok));
505 case QMetaType::ULong:
506 *static_cast<ulong *>(result) = ulong(qConvertToUnsignedNumber(d, ok));
509 *static_cast<int *>(result) = int(qConvertToNumber(d, ok));
512 *static_cast<uint *>(result) = uint(qConvertToUnsignedNumber(d, ok));
514 case QVariant::LongLong:
515 *static_cast<qlonglong *>(result) = qConvertToNumber(d, ok);
517 case QVariant::ULongLong: {
518 *static_cast<qulonglong *>(result) = qConvertToUnsignedNumber(d, ok);
521 case QMetaType::UChar: {
522 *static_cast<uchar *>(result) = qConvertToUnsignedNumber(d, ok);
525 case QVariant::Bool: {
526 bool *b = static_cast<bool *>(result);
528 case QVariant::ByteArray:
529 *b = qt_convertToBool<QByteArray, QByteArray>(d);
531 case QVariant::String:
532 *b = qt_convertToBool<QString, QLatin1String>(d);
535 *b = !v_cast<QChar>(d)->isNull();
537 case QVariant::Double:
539 case QVariant::LongLong:
540 case QMetaType::Char:
541 case QMetaType::Short:
542 case QMetaType::Long:
543 case QMetaType::Float:
544 *b = qMetaTypeNumber(d) != Q_INT64_C(0);
547 case QVariant::ULongLong:
548 case QMetaType::UChar:
549 case QMetaType::UShort:
550 case QMetaType::ULong:
551 *b = qMetaTypeUNumber(d) != Q_UINT64_C(0);
559 case QVariant::Double: {
560 double *f = static_cast<double *>(result);
562 case QVariant::String:
563 *f = v_cast<QString>(d)->toDouble(ok);
565 case QVariant::ByteArray:
566 *f = v_cast<QByteArray>(d)->toDouble(ok);
569 *f = double(d->data.b);
571 case QMetaType::Float:
572 *f = double(d->data.f);
574 case QVariant::LongLong:
576 case QMetaType::Char:
577 case QMetaType::Short:
578 case QMetaType::Long:
579 *f = double(qMetaTypeNumber(d));
582 case QVariant::ULongLong:
583 case QMetaType::UChar:
584 case QMetaType::UShort:
585 case QMetaType::ULong:
586 *f = double(qMetaTypeUNumber(d));
594 case QMetaType::Float: {
595 float *f = static_cast<float *>(result);
597 case QVariant::String:
598 *f = v_cast<QString>(d)->toFloat(ok);
600 case QVariant::ByteArray:
601 *f = v_cast<QByteArray>(d)->toFloat(ok);
604 *f = float(d->data.b);
606 case QVariant::Double:
607 *f = float(d->data.d);
609 case QVariant::LongLong:
611 case QMetaType::Char:
612 case QMetaType::Short:
613 case QMetaType::Long:
614 *f = float(qMetaTypeNumber(d));
617 case QVariant::ULongLong:
618 case QMetaType::UChar:
619 case QMetaType::UShort:
620 case QMetaType::ULong:
621 *f = float(qMetaTypeUNumber(d));
630 if (d->type == QVariant::StringList) {
631 QVariantList *lst = static_cast<QVariantList *>(result);
632 const QStringList *slist = v_cast<QStringList>(d);
633 for (int i = 0; i < slist->size(); ++i)
634 lst->append(QVariant(slist->at(i)));
635 } else if (qstrcmp(QMetaType::typeName(d->type), "QList<QVariant>") == 0) {
636 *static_cast<QVariantList *>(result) =
637 *static_cast<QList<QVariant> *>(d->data.shared->ptr);
643 if (qstrcmp(QMetaType::typeName(d->type), "QMap<QString, QVariant>") == 0) {
644 *static_cast<QVariantMap *>(result) =
645 *static_cast<QMap<QString, QVariant> *>(d->data.shared->ptr);
651 if (qstrcmp(QMetaType::typeName(d->type), "QHash<QString, QVariant>") == 0) {
652 *static_cast<QVariantHash *>(result) =
653 *static_cast<QHash<QString, QVariant> *>(d->data.shared->ptr);
658 #ifndef QT_NO_GEOM_VARIANT
660 if (d->type == QVariant::RectF)
661 *static_cast<QRect *>(result) = (v_cast<QRectF>(d))->toRect();
665 case QVariant::RectF:
666 if (d->type == QVariant::Rect)
667 *static_cast<QRectF *>(result) = *v_cast<QRect>(d);
671 case QVariant::PointF:
672 if (d->type == QVariant::Point)
673 *static_cast<QPointF *>(result) = *v_cast<QPoint>(d);
677 case QVariant::Point:
678 if (d->type == QVariant::PointF)
679 *static_cast<QPoint *>(result) = (v_cast<QPointF>(d))->toPoint();
683 case QMetaType::Char:
685 *static_cast<qint8 *>(result) = qint8(qConvertToNumber(d, ok));
695 #if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)
696 static void streamDebug(QDebug dbg, const QVariant &v)
698 switch (v.userType()) {
700 dbg.nospace() << v.toInt();
703 dbg.nospace() << v.toUInt();
705 case QVariant::LongLong:
706 dbg.nospace() << v.toLongLong();
708 case QVariant::ULongLong:
709 dbg.nospace() << v.toULongLong();
711 case QMetaType::Float:
712 dbg.nospace() << v.toFloat();
714 case QMetaType::QObjectStar:
715 dbg.nospace() << qvariant_cast<QObject *>(v);
717 case QVariant::Double:
718 dbg.nospace() << v.toDouble();
721 dbg.nospace() << v.toBool();
723 case QVariant::String:
724 dbg.nospace() << v.toString();
727 dbg.nospace() << v.toChar();
729 case QVariant::StringList:
730 dbg.nospace() << v.toStringList();
733 dbg.nospace() << v.toMap();
736 dbg.nospace() << v.toHash();
739 dbg.nospace() << v.toList();
742 dbg.nospace() << v.toDate();
745 dbg.nospace() << v.toTime();
747 case QVariant::DateTime:
748 dbg.nospace() << v.toDateTime();
750 #ifndef QT_BOOTSTRAPPED
751 case QVariant::EasingCurve:
752 dbg.nospace() << v.toEasingCurve();
755 case QVariant::ByteArray:
756 dbg.nospace() << v.toByteArray();
759 dbg.nospace() << v.toUrl();
761 #ifndef QT_NO_GEOM_VARIANT
762 case QVariant::Point:
763 dbg.nospace() << v.toPoint();
765 case QVariant::PointF:
766 dbg.nospace() << v.toPointF();
769 dbg.nospace() << v.toRect();
772 dbg.nospace() << v.toSize();
774 case QVariant::SizeF:
775 dbg.nospace() << v.toSizeF();
778 dbg.nospace() << v.toLine();
780 case QVariant::LineF:
781 dbg.nospace() << v.toLineF();
783 case QVariant::RectF:
784 dbg.nospace() << v.toRectF();
787 case QVariant::BitArray:
788 //dbg.nospace() << v.toBitArray();
796 const QVariant::Handler qt_kernel_variant_handler = {
800 #ifndef QT_NO_DATASTREAM
807 #if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)
814 Q_CORE_EXPORT const QVariant::Handler *qcoreVariantHandler()
816 return &qt_kernel_variant_handler;
820 const QVariant::Handler *QVariant::handler = &qt_kernel_variant_handler;
824 \brief The QVariant class acts like a union for the most common Qt data types.
830 Because C++ forbids unions from including types that have
831 non-default constructors or destructors, most interesting Qt
832 classes cannot be used in unions. Without QVariant, this would be
833 a problem for QObject::property() and for database work, etc.
835 A QVariant object holds a single value of a single type() at a
836 time. (Some type()s are multi-valued, for example a string list.)
837 You can find out what type, T, the variant holds, convert it to a
838 different type using convert(), get its value using one of the
839 toT() functions (e.g., toSize()) and check whether the type can
840 be converted to a particular type using canConvert().
842 The methods named toT() (e.g., toInt(), toString()) are const. If
843 you ask for the stored type, they return a copy of the stored
844 object. If you ask for a type that can be generated from the
845 stored type, toT() copies and converts and leaves the object
846 itself unchanged. If you ask for a type that cannot be generated
847 from the stored type, the result depends on the type; see the
848 function documentation for details.
850 Here is some example code to demonstrate the use of QVariant:
852 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 0
854 You can even store QList<QVariant> and QMap<QString, QVariant>
855 values in a variant, so you can easily construct arbitrarily
856 complex data structures of arbitrary types. This is very powerful
857 and versatile, but may prove less memory and speed efficient than
858 storing specific types in standard data structures.
860 QVariant also supports the notion of null values, where you can
861 have a defined type with no value set. However, note that QVariant
862 types can only be cast when they have had a value set.
864 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 1
866 QVariant can be extended to support other types than those
867 mentioned in the \l Type enum. See the \l QMetaType documentation
870 \section1 A Note on GUI Types
872 Because QVariant is part of the QtCore library, it cannot provide
873 conversion functions to data types defined in QtGui, such as
874 QColor, QImage, and QPixmap. In other words, there is no \c
875 toColor() function. Instead, you can use the QVariant::value() or
876 the qvariant_cast() template function. For example:
878 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 2
880 The inverse conversion (e.g., from QColor to QVariant) is
881 automatic for all data types supported by QVariant, including
884 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 3
886 \section1 Using canConvert() and convert() Consecutively
888 When using canConvert() and convert() consecutively, it is possible for
889 canConvert() to return true, but convert() to return false. This
890 is typically because canConvert() only reports the general ability of
891 QVariant to convert between types given suitable data; it is still
892 possible to supply data which cannot actually be converted.
894 For example, canConvert() would return true when called on a variant
895 containing a string because, in principle, QVariant is able to convert
896 strings of numbers to integers.
897 However, if the string contains non-numeric characters, it cannot be
898 converted to an integer, and any attempt to convert it will fail.
899 Hence, it is important to have both functions return true for a
900 successful conversion.
908 This enum type defines the types of variable that a QVariant can
911 \value Invalid no type
912 \value BitArray a QBitArray
913 \value Bitmap a QBitmap
915 \value Brush a QBrush
916 \value ByteArray a QByteArray
918 \value Color a QColor
919 \value Cursor a QCursor
921 \value DateTime a QDateTime
922 \value Double a double
923 \value EasingCurve a QEasingCurve
925 \value Hash a QVariantHash
927 \value Image a QImage
929 \value KeySequence a QKeySequence
931 \value LineF a QLineF
932 \value List a QVariantList
933 \value Locale a QLocale
934 \value LongLong a \l qlonglong
935 \value Map a QVariantMap
936 \value Matrix a QMatrix
937 \value Transform a QTransform
938 \value Matrix4x4 a QMatrix4x4
939 \value Palette a QPalette
941 \value Pixmap a QPixmap
942 \value Point a QPoint
943 \value PointArray a QPointArray
944 \value PointF a QPointF
945 \value Polygon a QPolygon
946 \value Quaternion a QQuaternion
948 \value RectF a QRectF
949 \value RegExp a QRegExp
950 \value Region a QRegion
952 \value SizeF a QSizeF
953 \value SizePolicy a QSizePolicy
954 \value String a QString
955 \value StringList a QStringList
956 \value TextFormat a QTextFormat
957 \value TextLength a QTextLength
959 \value UInt a \l uint
960 \value ULongLong a \l qulonglong
962 \value Vector2D a QVector2D
963 \value Vector3D a QVector3D
964 \value Vector4D a QVector4D
966 \value UserType Base value for user-defined types.
969 \omitvalue ColorGroup
971 \omitvalue LastGuiType
972 \omitvalue LastCoreType
977 \fn QVariant::QVariant()
979 Constructs an invalid variant.
984 \fn QVariant::QVariant(int typeOrUserType, const void *copy)
986 Constructs variant of type \a typeOrUserType, and initializes with
987 \a copy if \a copy is not 0.
989 Note that you have to pass the address of the variable you want stored.
991 Usually, you never have to use this constructor, use QVariant::fromValue()
992 instead to construct variants from the pointer types represented by
993 \c QMetaType::VoidStar, \c QMetaType::QObjectStar and
994 \c QMetaType::QWidgetStar.
996 \sa QVariant::fromValue(), Type
1000 \fn QVariant::QVariant(Type type)
1002 Constructs a null variant of type \a type.
1008 \fn QVariant::create(int type, const void *copy)
1012 Constructs a variant private of type \a type, and initializes with \a copy if
1016 void QVariant::create(int type, const void *copy)
1019 handler->construct(&d, copy);
1023 \fn QVariant::~QVariant()
1025 Destroys the QVariant and the contained object.
1027 Note that subclasses that reimplement clear() should reimplement
1028 the destructor to call clear(). This destructor calls clear(), but
1029 because it is the destructor, QVariant::clear() is called rather
1030 than a subclass's clear().
1033 QVariant::~QVariant()
1035 if ((d.is_shared && !d.data.shared->ref.deref()) || (!d.is_shared && d.type > Char))
1040 \fn QVariant::QVariant(const QVariant &p)
1042 Constructs a copy of the variant, \a p, passed as the argument to
1046 QVariant::QVariant(const QVariant &p)
1050 d.data.shared->ref.ref();
1051 } else if (p.d.type > Char) {
1052 handler->construct(&d, p.constData());
1053 d.is_null = p.d.is_null;
1057 #ifndef QT_NO_DATASTREAM
1059 Reads the variant from the data stream, \a s.
1061 QVariant::QVariant(QDataStream &s)
1066 #endif //QT_NO_DATASTREAM
1069 \fn QVariant::QVariant(const QString &val)
1071 Constructs a new variant with a string value, \a val.
1075 \fn QVariant::QVariant(const QLatin1String &val)
1077 Constructs a new variant with a string value, \a val.
1081 \fn QVariant::QVariant(const char *val)
1083 Constructs a new variant with a string value of \a val.
1084 The variant creates a deep copy of \a val, using the encoding
1085 set by QTextCodec::setCodecForCStrings().
1087 Note that \a val is converted to a QString for storing in the
1088 variant and QVariant::type() will return QMetaType::QString for
1091 You can disable this operator by defining \c
1092 QT_NO_CAST_FROM_ASCII when you compile your applications.
1094 \sa QTextCodec::setCodecForCStrings()
1097 #ifndef QT_NO_CAST_FROM_ASCII
1098 QVariant::QVariant(const char *val)
1100 QString s = QString::fromAscii(val);
1106 \fn QVariant::QVariant(const QStringList &val)
1108 Constructs a new variant with a string list value, \a val.
1112 \fn QVariant::QVariant(const QMap<QString, QVariant> &val)
1114 Constructs a new variant with a map of QVariants, \a val.
1118 \fn QVariant::QVariant(const QHash<QString, QVariant> &val)
1120 Constructs a new variant with a hash of QVariants, \a val.
1124 \fn QVariant::QVariant(const QDate &val)
1126 Constructs a new variant with a date value, \a val.
1130 \fn QVariant::QVariant(const QTime &val)
1132 Constructs a new variant with a time value, \a val.
1136 \fn QVariant::QVariant(const QDateTime &val)
1138 Constructs a new variant with a date/time value, \a val.
1143 \fn QVariant::QVariant(const QEasingCurve &val)
1145 Constructs a new variant with an easing curve value, \a val.
1149 \fn QVariant::QVariant(const QByteArray &val)
1151 Constructs a new variant with a bytearray value, \a val.
1155 \fn QVariant::QVariant(const QBitArray &val)
1157 Constructs a new variant with a bitarray value, \a val.
1161 \fn QVariant::QVariant(const QPoint &val)
1163 Constructs a new variant with a point value of \a val.
1167 \fn QVariant::QVariant(const QPointF &val)
1169 Constructs a new variant with a point value of \a val.
1173 \fn QVariant::QVariant(const QRectF &val)
1175 Constructs a new variant with a rect value of \a val.
1179 \fn QVariant::QVariant(const QLineF &val)
1181 Constructs a new variant with a line value of \a val.
1185 \fn QVariant::QVariant(const QLine &val)
1187 Constructs a new variant with a line value of \a val.
1191 \fn QVariant::QVariant(const QRect &val)
1193 Constructs a new variant with a rect value of \a val.
1197 \fn QVariant::QVariant(const QSize &val)
1199 Constructs a new variant with a size value of \a val.
1203 \fn QVariant::QVariant(const QSizeF &val)
1205 Constructs a new variant with a size value of \a val.
1209 \fn QVariant::QVariant(const QUrl &val)
1211 Constructs a new variant with a url value of \a val.
1215 \fn QVariant::QVariant(int val)
1217 Constructs a new variant with an integer value, \a val.
1221 \fn QVariant::QVariant(uint val)
1223 Constructs a new variant with an unsigned integer value, \a val.
1227 \fn QVariant::QVariant(qlonglong val)
1229 Constructs a new variant with a long long integer value, \a val.
1233 \fn QVariant::QVariant(qulonglong val)
1235 Constructs a new variant with an unsigned long long integer value, \a val.
1240 \fn QVariant::QVariant(bool val)
1242 Constructs a new variant with a boolean value, \a val.
1246 \fn QVariant::QVariant(double val)
1248 Constructs a new variant with a floating point value, \a val.
1252 \fn QVariant::QVariant(float val)
1254 Constructs a new variant with a floating point value, \a val.
1259 \fn QVariant::QVariant(const QList<QVariant> &val)
1261 Constructs a new variant with a list value, \a val.
1265 \fn QVariant::QVariant(const QChar &c)
1267 Constructs a new variant with a char value, \a c.
1271 \fn QVariant::QVariant(const QLocale &l)
1273 Constructs a new variant with a locale value, \a l.
1277 \fn QVariant::QVariant(const QRegExp ®Exp)
1279 Constructs a new variant with the regexp value \a regExp.
1283 \fn QVariant::QVariant(Qt::GlobalColor color)
1285 Constructs a new variant of type QVariant::Color and initializes
1288 This is a convenience constructor that allows \c{QVariant(Qt::blue);}
1289 to create a valid QVariant storing a QColor.
1291 Note: This constructor will assert if the application does not link
1292 to the Qt GUI library.
1295 QVariant::QVariant(Type type)
1296 { create(type, 0); }
1297 QVariant::QVariant(int typeOrUserType, const void *copy)
1298 { create(typeOrUserType, copy); d.is_null = false; }
1301 flags is true if it is a pointer type
1303 QVariant::QVariant(int typeOrUserType, const void *copy, uint flags)
1305 if (flags) { //type is a pointer type
1306 d.type = typeOrUserType;
1307 d.data.ptr = *reinterpret_cast<void *const*>(copy);
1309 create(typeOrUserType, copy);
1314 QVariant::QVariant(int val)
1315 { d.is_null = false; d.type = Int; d.data.i = val; }
1316 QVariant::QVariant(uint val)
1317 { d.is_null = false; d.type = UInt; d.data.u = val; }
1318 QVariant::QVariant(qlonglong val)
1319 { d.is_null = false; d.type = LongLong; d.data.ll = val; }
1320 QVariant::QVariant(qulonglong val)
1321 { d.is_null = false; d.type = ULongLong; d.data.ull = val; }
1322 QVariant::QVariant(bool val)
1323 { d.is_null = false; d.type = Bool; d.data.b = val; }
1324 QVariant::QVariant(double val)
1325 { d.is_null = false; d.type = Double; d.data.d = val; }
1327 QVariant::QVariant(const QByteArray &val)
1328 { d.is_null = false; d.type = ByteArray; v_construct<QByteArray>(&d, val); }
1329 QVariant::QVariant(const QBitArray &val)
1330 { d.is_null = false; d.type = BitArray; v_construct<QBitArray>(&d, val); }
1331 QVariant::QVariant(const QString &val)
1332 { d.is_null = false; d.type = String; v_construct<QString>(&d, val); }
1333 QVariant::QVariant(const QChar &val)
1334 { d.is_null = false; d.type = Char; v_construct<QChar>(&d, val); }
1335 QVariant::QVariant(const QLatin1String &val)
1336 { QString str(val); d.is_null = false; d.type = String; v_construct<QString>(&d, str); }
1337 QVariant::QVariant(const QStringList &val)
1338 { d.is_null = false; d.type = StringList; v_construct<QStringList>(&d, val); }
1340 QVariant::QVariant(const QDate &val)
1341 { d.is_null = false; d.type = Date; v_construct<QDate>(&d, val); }
1342 QVariant::QVariant(const QTime &val)
1343 { d.is_null = false; d.type = Time; v_construct<QTime>(&d, val); }
1344 QVariant::QVariant(const QDateTime &val)
1345 { d.is_null = false; d.type = DateTime; v_construct<QDateTime>(&d, val); }
1346 #ifndef QT_BOOTSTRAPPED
1347 QVariant::QVariant(const QEasingCurve &val)
1348 { d.is_null = false; d.type = EasingCurve; v_construct<QEasingCurve>(&d, val); }
1350 QVariant::QVariant(const QList<QVariant> &list)
1351 { d.is_null = false; d.type = List; v_construct<QVariantList>(&d, list); }
1352 QVariant::QVariant(const QMap<QString, QVariant> &map)
1353 { d.is_null = false; d.type = Map; v_construct<QVariantMap>(&d, map); }
1354 QVariant::QVariant(const QHash<QString, QVariant> &hash)
1355 { d.is_null = false; d.type = Hash; v_construct<QVariantHash>(&d, hash); }
1356 #ifndef QT_NO_GEOM_VARIANT
1357 QVariant::QVariant(const QPoint &pt) { d.is_null = false; d.type = Point; v_construct<QPoint>(&d, pt); }
1358 QVariant::QVariant(const QPointF &pt) { d.is_null = false; d.type = PointF; v_construct<QPointF>(&d, pt); }
1359 QVariant::QVariant(const QRectF &r) { d.is_null = false; d.type = RectF; v_construct<QRectF>(&d, r); }
1360 QVariant::QVariant(const QLineF &l) { d.is_null = false; d.type = LineF; v_construct<QLineF>(&d, l); }
1361 QVariant::QVariant(const QLine &l) { d.is_null = false; d.type = Line; v_construct<QLine>(&d, l); }
1362 QVariant::QVariant(const QRect &r) { d.is_null = false; d.type = Rect; v_construct<QRect>(&d, r); }
1363 QVariant::QVariant(const QSize &s) { d.is_null = false; d.type = Size; v_construct<QSize>(&d, s); }
1364 QVariant::QVariant(const QSizeF &s) { d.is_null = false; d.type = SizeF; v_construct<QSizeF>(&d, s); }
1366 QVariant::QVariant(const QUrl &u) { d.is_null = false; d.type = Url; v_construct<QUrl>(&d, u); }
1367 QVariant::QVariant(const QLocale &l) { d.is_null = false; d.type = Locale; v_construct<QLocale>(&d, l); }
1368 #ifndef QT_NO_REGEXP
1369 QVariant::QVariant(const QRegExp ®Exp) { d.is_null = false; d.type = RegExp; v_construct<QRegExp>(&d, regExp); }
1371 QVariant::QVariant(Qt::GlobalColor color) { create(62, &color); }
1374 Returns the storage type of the value stored in the variant.
1375 Although this function is declared as returning QVariant::Type,
1376 the return value should be interpreted as QMetaType::Type. In
1377 particular, QVariant::UserType is returned here only if the value
1378 is equal or greater than QMetaType::User.
1380 Note that return values in the ranges QVariant::Char through
1381 QVariant::RegExp and QVariant::Font through QVariant::Transform
1382 correspond to the values in the ranges QMetaType::QChar through
1383 QMetaType::QRegExp and QMetaType::QFont through QMetaType::QQuaternion.
1385 Pay particular attention when working with char and QChar
1386 variants. Note that there is no QVariant constructor specifically
1387 for type char, but there is one for QChar. For a variant of type
1388 QChar, this function returns QVariant::Char, which is the same as
1389 QMetaType::QChar, but for a variant of type \c char, this function
1390 returns QMetaType::Char, which is \e not the same as
1393 Also note that the types \c void*, \c long, \c short, \c unsigned
1394 \c long, \c unsigned \c short, \c unsigned \c char, \c float, \c
1395 QObject*, and \c QWidget* are represented in QMetaType::Type but
1396 not in QVariant::Type, and they can be returned by this function.
1397 However, they are considered to be user defined types when tested
1398 against QVariant::Type.
1400 To test whether an instance of QVariant contains a data type that
1401 is compatible with the data type you are interested in, use
1405 QVariant::Type QVariant::type() const
1407 return d.type >= QMetaType::User ? UserType : static_cast<Type>(d.type);
1411 Returns the storage type of the value stored in the variant. For
1412 non-user types, this is the same as type().
1417 int QVariant::userType() const
1423 Assigns the value of the variant \a variant to this variant.
1425 QVariant& QVariant::operator=(const QVariant &variant)
1427 if (this == &variant)
1431 if (variant.d.is_shared) {
1432 variant.d.data.shared->ref.ref();
1434 } else if (variant.d.type > Char) {
1435 d.type = variant.d.type;
1436 handler->construct(&d, variant.constData());
1437 d.is_null = variant.d.is_null;
1446 \fn void QVariant::swap(QVariant &other)
1449 Swaps variant \a other with this variant. This operation is very
1450 fast and never fails.
1454 \fn void QVariant::detach()
1459 void QVariant::detach()
1461 if (!d.is_shared || d.data.shared->ref.load() == 1)
1466 handler->construct(&dd, constData());
1467 if (!d.data.shared->ref.deref())
1469 d.data.shared = dd.data.shared;
1473 \fn bool QVariant::isDetached() const
1478 // ### Qt 5: change typeName()(and froends= to return a QString. Suggestion from Harald.
1480 Returns the name of the type stored in the variant. The returned
1481 strings describe the C++ datatype used to store the data: for
1482 example, "QFont", "QString", or "QVariantList". An Invalid
1485 const char *QVariant::typeName() const
1487 return typeToName(Type(d.type));
1491 Convert this variant to type Invalid and free up any resources
1494 void QVariant::clear()
1496 if ((d.is_shared && !d.data.shared->ref.deref()) || (!d.is_shared && d.type > Char))
1500 d.is_shared = false;
1504 Converts the enum representation of the storage type, \a typ, to
1505 its string representation.
1507 Returns a null pointer if the type is QVariant::Invalid or doesn't exist.
1509 const char *QVariant::typeToName(Type typ)
1513 if (typ == UserType)
1516 return QMetaType::typeName(typ);
1521 Converts the string representation of the storage type given in \a
1522 name, to its enum representation.
1524 If the string representation cannot be converted to any enum
1525 representation, the variant is set to \c Invalid.
1527 QVariant::Type QVariant::nameToType(const char *name)
1529 if (!name || !*name)
1531 if (strcmp(name, "Q3CString") == 0)
1533 if (strcmp(name, "Q_LLONG") == 0)
1535 if (strcmp(name, "Q_ULLONG") == 0)
1537 if (strcmp(name, "QIconSet") == 0)
1539 if (strcmp(name, "UserType") == 0)
1542 int metaType = QMetaType::type(name);
1543 return metaType <= int(LastGuiType) ? QVariant::Type(metaType) : UserType;
1546 #ifndef QT_NO_DATASTREAM
1547 enum { MapFromThreeCount = 36 };
1548 static const ushort map_from_three[MapFromThreeCount] =
1554 QVariant::StringList,
1570 QVariant::ByteArray,
1575 QVariant::SizePolicy,
1579 QVariant::ByteArray,
1581 QVariant::KeySequence,
1584 QVariant::ULongLong,
1585 QVariant::EasingCurve
1589 Internal function for loading a variant from stream \a s. Use the
1590 stream operators instead.
1594 void QVariant::load(QDataStream &s)
1600 if (s.version() < QDataStream::Qt_4_0) {
1601 if (u >= MapFromThreeCount)
1603 u = map_from_three[u];
1605 qint8 is_null = false;
1606 if (s.version() >= QDataStream::Qt_4_2)
1608 if (u == QVariant::UserType) {
1611 u = QMetaType::type(name);
1613 s.setStatus(QDataStream::ReadCorruptData);
1617 create(static_cast<int>(u), 0);
1618 d.is_null = is_null;
1621 // Since we wrote something, we should read something
1628 // const cast is safe since we operate on a newly constructed variant
1629 if (!QMetaType::load(s, d.type, const_cast<void *>(constData()))) {
1630 s.setStatus(QDataStream::ReadCorruptData);
1631 qWarning("QVariant::load: unable to load type %d.", d.type);
1636 Internal function for saving a variant to the stream \a s. Use the
1637 stream operators instead.
1641 void QVariant::save(QDataStream &s) const
1643 quint32 tp = type();
1644 if (s.version() < QDataStream::Qt_4_0) {
1646 for (i = MapFromThreeCount - 1; i >= 0; i--) {
1647 if (map_from_three[i] == tp) {
1658 if (s.version() >= QDataStream::Qt_4_2)
1659 s << qint8(d.is_null);
1660 if (tp == QVariant::UserType) {
1661 s << QMetaType::typeName(userType());
1669 if (!QMetaType::save(s, d.type, constData())) {
1670 Q_ASSERT_X(false, "QVariant::save", "Invalid type to save");
1671 qWarning("QVariant::save: unable to save type %d.", d.type);
1678 Reads a variant \a p from the stream \a s.
1680 \sa \link datastreamformat.html Format of the QDataStream
1683 QDataStream& operator>>(QDataStream &s, QVariant &p)
1690 Writes a variant \a p to the stream \a s.
1692 \sa \link datastreamformat.html Format of the QDataStream
1695 QDataStream& operator<<(QDataStream &s, const QVariant &p)
1702 Reads a variant type \a p in enum representation from the stream \a s.
1704 QDataStream& operator>>(QDataStream &s, QVariant::Type &p)
1708 p = (QVariant::Type)u;
1714 Writes a variant type \a p to the stream \a s.
1716 QDataStream& operator<<(QDataStream &s, const QVariant::Type p)
1718 s << static_cast<quint32>(p);
1723 #endif //QT_NO_DATASTREAM
1726 \fn bool QVariant::isValid() const
1728 Returns true if the storage type of this variant is not
1729 QVariant::Invalid; otherwise returns false.
1732 template <typename T>
1733 inline T qVariantToHelper(const QVariant::Private &d, QVariant::Type t,
1734 const QVariant::Handler *handler, T * = 0)
1737 return *v_cast<T>(&d);
1740 handler->convert(&d, t, &ret, 0);
1745 \fn QStringList QVariant::toStringList() const
1747 Returns the variant as a QStringList if the variant has type()
1748 StringList, \l String, or \l List of a type that can be converted
1749 to QString; otherwise returns an empty list.
1751 \sa canConvert(), convert()
1753 QStringList QVariant::toStringList() const
1755 return qVariantToHelper<QStringList>(d, StringList, handler);
1759 Returns the variant as a QString if the variant has type() \l
1760 String, \l Bool, \l ByteArray, \l Char, \l Date, \l DateTime, \l
1761 Double, \l Int, \l LongLong, \l StringList, \l Time, \l UInt, or
1762 \l ULongLong; otherwise returns an empty string.
1764 \sa canConvert(), convert()
1766 QString QVariant::toString() const
1768 return qVariantToHelper<QString>(d, String, handler);
1772 Returns the variant as a QMap<QString, QVariant> if the variant
1773 has type() \l Map; otherwise returns an empty map.
1775 \sa canConvert(), convert()
1777 QVariantMap QVariant::toMap() const
1779 return qVariantToHelper<QVariantMap>(d, Map, handler);
1783 Returns the variant as a QHash<QString, QVariant> if the variant
1784 has type() \l Hash; otherwise returns an empty map.
1786 \sa canConvert(), convert()
1788 QVariantHash QVariant::toHash() const
1790 return qVariantToHelper<QVariantHash>(d, Hash, handler);
1794 \fn QDate QVariant::toDate() const
1796 Returns the variant as a QDate if the variant has type() \l Date,
1797 \l DateTime, or \l String; otherwise returns an invalid date.
1799 If the type() is \l String, an invalid date will be returned if the
1800 string cannot be parsed as a Qt::ISODate format date.
1802 \sa canConvert(), convert()
1804 QDate QVariant::toDate() const
1806 return qVariantToHelper<QDate>(d, Date, handler);
1810 \fn QTime QVariant::toTime() const
1812 Returns the variant as a QTime if the variant has type() \l Time,
1813 \l DateTime, or \l String; otherwise returns an invalid time.
1815 If the type() is \l String, an invalid time will be returned if
1816 the string cannot be parsed as a Qt::ISODate format time.
1818 \sa canConvert(), convert()
1820 QTime QVariant::toTime() const
1822 return qVariantToHelper<QTime>(d, Time, handler);
1826 \fn QDateTime QVariant::toDateTime() const
1828 Returns the variant as a QDateTime if the variant has type() \l
1829 DateTime, \l Date, or \l String; otherwise returns an invalid
1832 If the type() is \l String, an invalid date/time will be returned
1833 if the string cannot be parsed as a Qt::ISODate format date/time.
1835 \sa canConvert(), convert()
1837 QDateTime QVariant::toDateTime() const
1839 return qVariantToHelper<QDateTime>(d, DateTime, handler);
1844 \fn QEasingCurve QVariant::toEasingCurve() const
1846 Returns the variant as a QEasingCurve if the variant has type() \l
1847 EasingCurve; otherwise returns a default easing curve.
1849 \sa canConvert(), convert()
1851 #ifndef QT_BOOTSTRAPPED
1852 QEasingCurve QVariant::toEasingCurve() const
1854 return qVariantToHelper<QEasingCurve>(d, EasingCurve, handler);
1859 \fn QByteArray QVariant::toByteArray() const
1861 Returns the variant as a QByteArray if the variant has type() \l
1862 ByteArray or \l String (converted using QString::fromAscii());
1863 otherwise returns an empty byte array.
1865 \sa canConvert(), convert()
1867 QByteArray QVariant::toByteArray() const
1869 return qVariantToHelper<QByteArray>(d, ByteArray, handler);
1872 #ifndef QT_NO_GEOM_VARIANT
1874 \fn QPoint QVariant::toPoint() const
1876 Returns the variant as a QPoint if the variant has type()
1877 \l Point or \l PointF; otherwise returns a null QPoint.
1879 \sa canConvert(), convert()
1881 QPoint QVariant::toPoint() const
1883 return qVariantToHelper<QPoint>(d, Point, handler);
1887 \fn QRect QVariant::toRect() const
1889 Returns the variant as a QRect if the variant has type() \l Rect;
1890 otherwise returns an invalid QRect.
1892 \sa canConvert(), convert()
1894 QRect QVariant::toRect() const
1896 return qVariantToHelper<QRect>(d, Rect, handler);
1900 \fn QSize QVariant::toSize() const
1902 Returns the variant as a QSize if the variant has type() \l Size;
1903 otherwise returns an invalid QSize.
1905 \sa canConvert(), convert()
1907 QSize QVariant::toSize() const
1909 return qVariantToHelper<QSize>(d, Size, handler);
1913 \fn QSizeF QVariant::toSizeF() const
1915 Returns the variant as a QSizeF if the variant has type() \l
1916 SizeF; otherwise returns an invalid QSizeF.
1918 \sa canConvert(), convert()
1920 QSizeF QVariant::toSizeF() const
1922 return qVariantToHelper<QSizeF>(d, SizeF, handler);
1926 \fn QRectF QVariant::toRectF() const
1928 Returns the variant as a QRectF if the variant has type() \l Rect
1929 or \l RectF; otherwise returns an invalid QRectF.
1931 \sa canConvert(), convert()
1933 QRectF QVariant::toRectF() const
1935 return qVariantToHelper<QRectF>(d, RectF, handler);
1939 \fn QLineF QVariant::toLineF() const
1941 Returns the variant as a QLineF if the variant has type() \l
1942 LineF; otherwise returns an invalid QLineF.
1944 \sa canConvert(), convert()
1946 QLineF QVariant::toLineF() const
1948 return qVariantToHelper<QLineF>(d, LineF, handler);
1952 \fn QLine QVariant::toLine() const
1954 Returns the variant as a QLine if the variant has type() \l Line;
1955 otherwise returns an invalid QLine.
1957 \sa canConvert(), convert()
1959 QLine QVariant::toLine() const
1961 return qVariantToHelper<QLine>(d, Line, handler);
1965 \fn QPointF QVariant::toPointF() const
1967 Returns the variant as a QPointF if the variant has type() \l
1968 Point or \l PointF; otherwise returns a null QPointF.
1970 \sa canConvert(), convert()
1972 QPointF QVariant::toPointF() const
1974 return qVariantToHelper<QPointF>(d, PointF, handler);
1977 #endif // QT_NO_GEOM_VARIANT
1980 \fn QUrl QVariant::toUrl() const
1982 Returns the variant as a QUrl if the variant has type()
1983 \l Url; otherwise returns an invalid QUrl.
1985 \sa canConvert(), convert()
1987 QUrl QVariant::toUrl() const
1989 return qVariantToHelper<QUrl>(d, Url, handler);
1993 \fn QLocale QVariant::toLocale() const
1995 Returns the variant as a QLocale if the variant has type()
1996 \l Locale; otherwise returns an invalid QLocale.
1998 \sa canConvert(), convert()
2000 QLocale QVariant::toLocale() const
2002 return qVariantToHelper<QLocale>(d, Locale, handler);
2006 \fn QRegExp QVariant::toRegExp() const
2009 Returns the variant as a QRegExp if the variant has type() \l
2010 RegExp; otherwise returns an empty QRegExp.
2012 \sa canConvert(), convert()
2014 #ifndef QT_NO_REGEXP
2015 QRegExp QVariant::toRegExp() const
2017 return qVariantToHelper<QRegExp>(d, RegExp, handler);
2022 \fn QChar QVariant::toChar() const
2024 Returns the variant as a QChar if the variant has type() \l Char,
2025 \l Int, or \l UInt; otherwise returns an invalid QChar.
2027 \sa canConvert(), convert()
2029 QChar QVariant::toChar() const
2031 return qVariantToHelper<QChar>(d, Char, handler);
2035 Returns the variant as a QBitArray if the variant has type()
2036 \l BitArray; otherwise returns an empty bit array.
2038 \sa canConvert(), convert()
2040 QBitArray QVariant::toBitArray() const
2042 return qVariantToHelper<QBitArray>(d, BitArray, handler);
2045 template <typename T>
2046 inline T qNumVariantToHelper(const QVariant::Private &d,
2047 const QVariant::Handler *handler, bool *ok, const T& val)
2049 uint t = qMetaTypeId<T>();
2056 if (!handler->convert(&d, QVariant::Type(t), &ret, ok) && ok)
2062 Returns the variant as an int if the variant has type() \l Int,
2063 \l Bool, \l ByteArray, \l Char, \l Double, \l LongLong, \l
2064 String, \l UInt, or \l ULongLong; otherwise returns 0.
2066 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2067 converted to an int; otherwise \c{*}\a{ok} is set to false.
2069 \bold{Warning:} If the value is convertible to a \l LongLong but is too
2070 large to be represented in an int, the resulting arithmetic overflow will
2071 not be reflected in \a ok. A simple workaround is to use QString::toInt().
2072 Fixing this bug has been postponed to Qt 5 in order to avoid breaking existing code.
2074 \sa canConvert(), convert()
2076 int QVariant::toInt(bool *ok) const
2078 return qNumVariantToHelper<int>(d, handler, ok, d.data.i);
2082 Returns the variant as an unsigned int if the variant has type()
2083 \l UInt, \l Bool, \l ByteArray, \l Char, \l Double, \l Int, \l
2084 LongLong, \l String, or \l ULongLong; otherwise returns 0.
2086 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2087 converted to an unsigned int; otherwise \c{*}\a{ok} is set to false.
2089 \bold{Warning:} If the value is convertible to a \l ULongLong but is too
2090 large to be represented in an unsigned int, the resulting arithmetic overflow will
2091 not be reflected in \a ok. A simple workaround is to use QString::toUInt().
2092 Fixing this bug has been postponed to Qt 5 in order to avoid breaking existing code.
2094 \sa canConvert(), convert()
2096 uint QVariant::toUInt(bool *ok) const
2098 return qNumVariantToHelper<uint>(d, handler, ok, d.data.u);
2102 Returns the variant as a long long int if the variant has type()
2103 \l LongLong, \l Bool, \l ByteArray, \l Char, \l Double, \l Int,
2104 \l String, \l UInt, or \l ULongLong; otherwise returns 0.
2106 If \a ok is non-null: \c{*}\c{ok} is set to true if the value could be
2107 converted to an int; otherwise \c{*}\c{ok} is set to false.
2109 \sa canConvert(), convert()
2111 qlonglong QVariant::toLongLong(bool *ok) const
2113 return qNumVariantToHelper<qlonglong>(d, handler, ok, d.data.ll);
2117 Returns the variant as as an unsigned long long int if the
2118 variant has type() \l ULongLong, \l Bool, \l ByteArray, \l Char,
2119 \l Double, \l Int, \l LongLong, \l String, or \l UInt; otherwise
2122 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2123 converted to an int; otherwise \c{*}\a{ok} is set to false.
2125 \sa canConvert(), convert()
2127 qulonglong QVariant::toULongLong(bool *ok) const
2129 return qNumVariantToHelper<qulonglong>(d, handler, ok, d.data.ull);
2133 Returns the variant as a bool if the variant has type() Bool.
2135 Returns true if the variant has type() \l Bool, \l Char, \l Double,
2136 \l Int, \l LongLong, \l UInt, or \l ULongLong and the value is
2137 non-zero, or if the variant has type \l String or \l ByteArray and
2138 its lower-case content is not empty, "0" or "false"; otherwise
2141 \sa canConvert(), convert()
2143 bool QVariant::toBool() const
2149 handler->convert(&d, Bool, &res, 0);
2155 Returns the variant as a double if the variant has type() \l
2156 Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
2157 UInt, or \l ULongLong; otherwise returns 0.0.
2159 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2160 converted to a double; otherwise \c{*}\a{ok} is set to false.
2162 \sa canConvert(), convert()
2164 double QVariant::toDouble(bool *ok) const
2166 return qNumVariantToHelper<double>(d, handler, ok, d.data.d);
2170 Returns the variant as a float if the variant has type() \l
2171 Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
2172 UInt, or \l ULongLong; otherwise returns 0.0.
2176 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2177 converted to a double; otherwise \c{*}\a{ok} is set to false.
2179 \sa canConvert(), convert()
2181 float QVariant::toFloat(bool *ok) const
2183 return qNumVariantToHelper<float>(d, handler, ok, d.data.f);
2187 Returns the variant as a qreal if the variant has type() \l
2188 Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
2189 UInt, or \l ULongLong; otherwise returns 0.0.
2193 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2194 converted to a double; otherwise \c{*}\a{ok} is set to false.
2196 \sa canConvert(), convert()
2198 qreal QVariant::toReal(bool *ok) const
2200 return qNumVariantToHelper<qreal>(d, handler, ok, d.data.real);
2204 Returns the variant as a QVariantList if the variant has type()
2205 \l List or \l StringList; otherwise returns an empty list.
2207 \sa canConvert(), convert()
2209 QVariantList QVariant::toList() const
2211 return qVariantToHelper<QVariantList>(d, List, handler);
2215 static const quint32 qCanConvertMatrix[QVariant::LastCoreType + 1] =
2219 /*Bool*/ 1 << QVariant::Double | 1 << QVariant::Int | 1 << QVariant::UInt
2220 | 1 << QVariant::LongLong | 1 << QVariant::ULongLong | 1 << QVariant::ByteArray
2221 | 1 << QVariant::String | 1 << QVariant::Char,
2223 /*Int*/ 1 << QVariant::UInt | 1 << QVariant::String | 1 << QVariant::Double
2224 | 1 << QVariant::Bool | 1 << QVariant::LongLong | 1 << QVariant::ULongLong
2225 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2227 /*UInt*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::Double
2228 | 1 << QVariant::Bool | 1 << QVariant::LongLong | 1 << QVariant::ULongLong
2229 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2231 /*LLong*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::Double
2232 | 1 << QVariant::Bool | 1 << QVariant::UInt | 1 << QVariant::ULongLong
2233 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2235 /*ULlong*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::Double
2236 | 1 << QVariant::Bool | 1 << QVariant::UInt | 1 << QVariant::LongLong
2237 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2239 /*double*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::ULongLong
2240 | 1 << QVariant::Bool | 1 << QVariant::UInt | 1 << QVariant::LongLong
2241 | 1 << QVariant::ByteArray,
2243 /*QChar*/ 1 << QVariant::Int | 1 << QVariant::UInt | 1 << QVariant::LongLong
2244 | 1 << QVariant::ULongLong,
2248 /*QList*/ 1 << QVariant::StringList,
2250 /*QString*/ 1 << QVariant::StringList | 1 << QVariant::ByteArray | 1 << QVariant::Int
2251 | 1 << QVariant::UInt | 1 << QVariant::Bool | 1 << QVariant::Double
2252 | 1 << QVariant::Date | 1 << QVariant::Time | 1 << QVariant::DateTime
2253 | 1 << QVariant::LongLong | 1 << QVariant::ULongLong | 1 << QVariant::Char
2254 | 1 << QVariant::Url,
2256 /*QStringList*/ 1 << QVariant::List | 1 << QVariant::String,
2258 /*QByteArray*/ 1 << QVariant::String | 1 << QVariant::Int | 1 << QVariant::UInt | 1 << QVariant::Bool
2259 | 1 << QVariant::Double | 1 << QVariant::LongLong | 1 << QVariant::ULongLong,
2263 /*QDate*/ 1 << QVariant::String | 1 << QVariant::DateTime,
2265 /*QTime*/ 1 << QVariant::String | 1 << QVariant::DateTime,
2267 /*QDateTime*/ 1 << QVariant::String | 1 << QVariant::Date,
2269 /*QUrl*/ 1 << QVariant::String,
2273 /*QRect*/ 1 << QVariant::RectF,
2275 /*QRectF*/ 1 << QVariant::Rect,
2277 /*QSize*/ 1 << QVariant::SizeF,
2279 /*QSizeF*/ 1 << QVariant::Size,
2281 /*QLine*/ 1 << QVariant::LineF,
2283 /*QLineF*/ 1 << QVariant::Line,
2285 /*QPoint*/ 1 << QVariant::PointF,
2287 /*QPointF*/ 1 << QVariant::Point,
2297 Returns true if the variant's type can be cast to the requested
2298 type, \a t. Such casting is done automatically when calling the
2299 toInt(), toBool(), ... methods.
2301 The following casts are done automatically:
2304 \header \o Type \o Automatically Cast To
2305 \row \o \l Bool \o \l Char, \l Double, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2306 \row \o \l ByteArray \o \l Double, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2307 \row \o \l Char \o \l Bool, \l Int, \l UInt, \l LongLong, \l ULongLong
2308 \row \o \l Color \o \l String
2309 \row \o \l Date \o \l DateTime, \l String
2310 \row \o \l DateTime \o \l Date, \l String, \l Time
2311 \row \o \l Double \o \l Bool, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2312 \row \o \l Font \o \l String
2313 \row \o \l Int \o \l Bool, \l Char, \l Double, \l LongLong, \l String, \l UInt, \l ULongLong
2314 \row \o \l KeySequence \o \l Int, \l String
2315 \row \o \l List \o \l StringList (if the list's items can be converted to strings)
2316 \row \o \l LongLong \o \l Bool, \l ByteArray, \l Char, \l Double, \l Int, \l String, \l UInt, \l ULongLong
2317 \row \o \l Point \o PointF
2318 \row \o \l Rect \o RectF
2319 \row \o \l String \o \l Bool, \l ByteArray, \l Char, \l Color, \l Date, \l DateTime, \l Double,
2320 \l Font, \l Int, \l KeySequence, \l LongLong, \l StringList, \l Time, \l UInt,
2322 \row \o \l StringList \o \l List, \l String (if the list contains exactly one item)
2323 \row \o \l Time \o \l String
2324 \row \o \l UInt \o \l Bool, \l Char, \l Double, \l Int, \l LongLong, \l String, \l ULongLong
2325 \row \o \l ULongLong \o \l Bool, \l Char, \l Double, \l Int, \l LongLong, \l String, \l UInt
2330 bool QVariant::canConvert(Type t) const
2332 //we can treat floats as double
2333 //the reason for not doing it the "proper" way is that QMetaType::Float's value is 135,
2334 //which can't be handled by qCanConvertMatrix
2335 //In addition QVariant::Type doesn't have a Float value, so we're using QMetaType::Float
2336 const uint currentType = ((d.type == QMetaType::Float) ? QVariant::Double : d.type);
2337 if (uint(t) == uint(QMetaType::Float)) t = QVariant::Double;
2339 if (currentType == uint(t))
2342 if (currentType > QVariant::LastCoreType || t > QVariant::LastCoreType) {
2345 return currentType == QVariant::KeySequence
2346 || currentType == QMetaType::ULong
2347 || currentType == QMetaType::Long
2348 || currentType == QMetaType::UShort
2349 || currentType == QMetaType::UChar
2350 || currentType == QMetaType::Char
2351 || currentType == QMetaType::Short;
2352 case QVariant::Image:
2353 return currentType == QVariant::Pixmap || currentType == QVariant::Bitmap;
2354 case QVariant::Pixmap:
2355 return currentType == QVariant::Image || currentType == QVariant::Bitmap
2356 || currentType == QVariant::Brush;
2357 case QVariant::Bitmap:
2358 return currentType == QVariant::Pixmap || currentType == QVariant::Image;
2359 case QVariant::ByteArray:
2360 return currentType == QVariant::Color;
2361 case QVariant::String:
2362 return currentType == QVariant::KeySequence || currentType == QVariant::Font
2363 || currentType == QVariant::Color;
2364 case QVariant::KeySequence:
2365 return currentType == QVariant::String || currentType == QVariant::Int;
2366 case QVariant::Font:
2367 return currentType == QVariant::String;
2368 case QVariant::Color:
2369 return currentType == QVariant::String || currentType == QVariant::ByteArray
2370 || currentType == QVariant::Brush;
2371 case QVariant::Brush:
2372 return currentType == QVariant::Color || currentType == QVariant::Pixmap;
2373 case QMetaType::Long:
2374 case QMetaType::Char:
2375 case QMetaType::UChar:
2376 case QMetaType::ULong:
2377 case QMetaType::Short:
2378 case QMetaType::UShort:
2379 return qCanConvertMatrix[QVariant::Int] & (1 << currentType) || currentType == QVariant::Int;
2385 if(t == String && currentType == StringList)
2386 return v_cast<QStringList>(&d)->count() == 1;
2388 return qCanConvertMatrix[t] & (1 << currentType);
2392 Casts the variant to the requested type, \a t. If the cast cannot be
2393 done, the variant is cleared. Returns true if the current type of
2394 the variant was successfully cast; otherwise returns false.
2396 \warning For historical reasons, converting a null QVariant results
2397 in a null value of the desired type (e.g., an empty string for
2398 QString) and a result of false.
2400 \sa canConvert(), clear()
2403 bool QVariant::convert(Type t)
2405 if (d.type == uint(t))
2408 QVariant oldValue = *this;
2411 if (!oldValue.canConvert(t))
2415 if (oldValue.isNull())
2419 if (!handler->convert(&oldValue.d, t, data(), &isOk))
2426 \fn bool operator==(const QVariant &v1, const QVariant &v2)
2430 Returns true if \a v1 and \a v2 are equal; otherwise returns false.
2432 \warning This function doesn't support custom types registered
2433 with qRegisterMetaType().
2436 \fn bool operator!=(const QVariant &v1, const QVariant &v2)
2440 Returns false if \a v1 and \a v2 are equal; otherwise returns true.
2442 \warning This function doesn't support custom types registered
2443 with qRegisterMetaType().
2446 /*! \fn bool QVariant::operator==(const QVariant &v) const
2448 Compares this QVariant with \a v and returns true if they are
2449 equal; otherwise returns false.
2451 In the case of custom types, their equalness operators are not called.
2452 Instead the values' addresses are compared.
2456 \fn bool QVariant::operator!=(const QVariant &v) const
2458 Compares this QVariant with \a v and returns true if they are not
2459 equal; otherwise returns false.
2461 \warning This function doesn't support custom types registered
2462 with qRegisterMetaType().
2465 static bool qIsNumericType(uint tp)
2467 return (tp >= QVariant::Bool && tp <= QVariant::Double)
2468 || (tp >= QMetaType::Long && tp <= QMetaType::Float);
2471 static bool qIsFloatingPoint(uint tp)
2473 return tp == QVariant::Double || tp == QMetaType::Float;
2478 bool QVariant::cmp(const QVariant &v) const
2481 if (d.type != v2.d.type) {
2482 if (qIsNumericType(d.type) && qIsNumericType(v.d.type)) {
2483 if (qIsFloatingPoint(d.type) || qIsFloatingPoint(v.d.type))
2484 return qFuzzyCompare(toReal(), v.toReal());
2486 return toLongLong() == v.toLongLong();
2488 if (!v2.canConvert(Type(d.type)) || !v2.convert(Type(d.type)))
2491 return handler->compare(&d, &v2.d);
2497 const void *QVariant::constData() const
2499 return d.is_shared ? d.data.shared->ptr : reinterpret_cast<const void *>(&d.data.ptr);
2503 \fn const void* QVariant::data() const
2509 void* QVariant::data()
2512 return const_cast<void *>(constData());
2517 Returns true if this is a NULL variant, false otherwise.
2519 bool QVariant::isNull() const
2521 return handler->isNull(&d);
2524 #ifndef QT_NO_DEBUG_STREAM
2525 QDebug operator<<(QDebug dbg, const QVariant &v)
2527 #ifndef Q_BROKEN_DEBUG_STREAM
2528 dbg.nospace() << "QVariant(" << v.typeName() << ", ";
2529 QVariant::handler->debugStream(dbg, v);
2530 dbg.nospace() << ')';
2533 qWarning("This compiler doesn't support streaming QVariant to QDebug");
2539 QDebug operator<<(QDebug dbg, const QVariant::Type p)
2541 #ifndef Q_BROKEN_DEBUG_STREAM
2542 dbg.nospace() << "QVariant::" << QVariant::typeToName(p);
2545 qWarning("This compiler doesn't support streaming QVariant::Type to QDebug");
2553 /*! \fn void QVariant::setValue(const T &value)
2555 Stores a copy of \a value. If \c{T} is a type that QVariant
2556 doesn't support, QMetaType is used to store the value. A compile
2557 error will occur if QMetaType doesn't handle the type.
2561 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 4
2563 \sa value(), fromValue(), canConvert()
2566 /*! \fn T QVariant::value() const
2568 Returns the stored value converted to the template type \c{T}.
2569 Call canConvert() to find out whether a type can be converted.
2570 If the value cannot be converted, \l{default-constructed value}
2573 If the type \c{T} is supported by QVariant, this function behaves
2574 exactly as toString(), toInt() etc.
2578 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 5
2580 \sa setValue(), fromValue(), canConvert()
2583 /*! \fn bool QVariant::canConvert() const
2585 Returns true if the variant can be converted to the template type \c{T},
2590 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 6
2595 /*! \fn static QVariant QVariant::fromValue(const T &value)
2597 Returns a QVariant containing a copy of \a value. Behaves
2598 exactly like setValue() otherwise.
2602 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 7
2604 \note If you are working with custom types, you should use
2605 the Q_DECLARE_METATYPE() macro to register your custom type.
2607 \sa setValue(), value()
2611 \fn QVariant qVariantFromValue(const T &value)
2615 Returns a variant containing a copy of the given \a value
2616 with template type \c{T}.
2618 This function is equivalent to QVariant::fromValue(\a value).
2620 \note This function was provided as a workaround for MSVC 6
2621 which did not support member template functions. It is advised
2622 to use the other form in new code.
2624 For example, a QObject pointer can be stored in a variant with the
2627 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 8
2629 \sa QVariant::fromValue()
2632 /*! \fn void qVariantSetValue(QVariant &variant, const T &value)
2636 Sets the contents of the given \a variant to a copy of the
2637 \a value with the specified template type \c{T}.
2639 This function is equivalent to QVariant::setValue(\a value).
2641 \note This function was provided as a workaround for MSVC 6
2642 which did not support member template functions. It is advised
2643 to use the other form in new code.
2645 \sa QVariant::setValue()
2649 \fn T qvariant_cast(const QVariant &value)
2652 Returns the given \a value converted to the template type \c{T}.
2654 This function is equivalent to QVariant::value().
2656 \sa QVariant::value()
2659 /*! \fn T qVariantValue(const QVariant &value)
2663 Returns the given \a value converted to the template type \c{T}.
2665 This function is equivalent to
2666 \l{QVariant::value()}{QVariant::value}<T>(\a value).
2668 \note This function was provided as a workaround for MSVC 6
2669 which did not support member template functions. It is advised
2670 to use the other form in new code.
2672 \sa QVariant::value(), qvariant_cast()
2675 /*! \fn bool qVariantCanConvert(const QVariant &value)
2679 Returns true if the given \a value can be converted to the
2680 template type specified; otherwise returns false.
2682 This function is equivalent to QVariant::canConvert(\a value).
2684 \note This function was provided as a workaround for MSVC 6
2685 which did not support member template functions. It is advised
2686 to use the other form in new code.
2688 \sa QVariant::canConvert()
2692 \typedef QVariantList
2695 Synonym for QList<QVariant>.
2699 \typedef QVariantMap
2702 Synonym for QMap<QString, QVariant>.
2706 \typedef QVariantHash
2710 Synonym for QHash<QString, QVariant>.
2714 \typedef QVariant::DataPtr
2719 \fn DataPtr &QVariant::data_ptr()