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
78 struct TypeDefiniton {
79 static const bool IsAvailable = true;
82 // Ignore these types, as incomplete
83 #ifdef QT_BOOTSTRAPPED
84 template<> struct TypeDefiniton<QEasingCurve> { static const bool IsAvailable = false; };
86 #ifdef QT_NO_GEOM_VARIANT
87 template<> struct TypeDefiniton<QRect> { static const bool IsAvailable = false; };
88 template<> struct TypeDefiniton<QRectF> { static const bool IsAvailable = false; };
89 template<> struct TypeDefiniton<QSize> { static const bool IsAvailable = false; };
90 template<> struct TypeDefiniton<QSizeF> { static const bool IsAvailable = false; };
91 template<> struct TypeDefiniton<QLine> { static const bool IsAvailable = false; };
92 template<> struct TypeDefiniton<QLineF> { static const bool IsAvailable = false; };
93 template<> struct TypeDefiniton<QPoint> { static const bool IsAvailable = false; };
94 template<> struct TypeDefiniton<QPointF> { static const bool IsAvailable = false; };
97 struct CoreTypesFilter {
100 static const bool IsAccepted = QTypeModuleInfo<T>::IsCore && TypeDefiniton<T>::IsAvailable;
105 static void construct(QVariant::Private *x, const void *copy)
107 QVariantConstructor<CoreTypesFilter> constructor(x, copy);
108 QMetaTypeSwitcher::switcher<void>(constructor, x->type, 0);
111 static void clear(QVariant::Private *d)
113 QVariantDestructor<CoreTypesFilter> cleaner(d);
114 QMetaTypeSwitcher::switcher<void>(cleaner, d->type, 0);
117 static bool isNull(const QVariant::Private *d)
119 QVariantIsNull<CoreTypesFilter> isNull(d);
120 return QMetaTypeSwitcher::switcher<bool>(isNull, d->type, 0);
126 Compares \a a to \a b. The caller guarantees that \a a and \a b
127 are of the same type.
129 static bool compare(const QVariant::Private *a, const QVariant::Private *b)
131 QVariantComparator<CoreTypesFilter> comparator(a, b);
132 return QMetaTypeSwitcher::switcher<bool>(comparator, a->type, 0);
138 static qlonglong qMetaTypeNumber(const QVariant::Private *d)
143 case QMetaType::LongLong:
145 case QMetaType::Char:
146 return qlonglong(d->data.c);
147 case QMetaType::Short:
148 return qlonglong(d->data.s);
149 case QMetaType::Long:
150 return qlonglong(d->data.l);
151 case QMetaType::Float:
152 return qRound64(d->data.f);
153 case QVariant::Double:
154 return qRound64(d->data.d);
160 static qulonglong qMetaTypeUNumber(const QVariant::Private *d)
165 case QVariant::ULongLong:
167 case QMetaType::UChar:
169 case QMetaType::UShort:
171 case QMetaType::ULong:
178 static qlonglong qConvertToNumber(const QVariant::Private *d, bool *ok)
182 switch (uint(d->type)) {
183 case QVariant::String:
184 return v_cast<QString>(d)->toLongLong(ok);
186 return v_cast<QChar>(d)->unicode();
187 case QVariant::ByteArray:
188 return v_cast<QByteArray>(d)->toLongLong(ok);
190 return qlonglong(d->data.b);
191 case QVariant::Double:
193 case QMetaType::Char:
194 case QMetaType::Short:
195 case QMetaType::Long:
196 case QMetaType::Float:
197 case QMetaType::LongLong:
198 return qMetaTypeNumber(d);
199 case QVariant::ULongLong:
201 case QMetaType::UChar:
202 case QMetaType::UShort:
203 case QMetaType::ULong:
204 return qlonglong(qMetaTypeUNumber(d));
211 static qulonglong qConvertToUnsignedNumber(const QVariant::Private *d, bool *ok)
215 switch (uint(d->type)) {
216 case QVariant::String:
217 return v_cast<QString>(d)->toULongLong(ok);
219 return v_cast<QChar>(d)->unicode();
220 case QVariant::ByteArray:
221 return v_cast<QByteArray>(d)->toULongLong(ok);
223 return qulonglong(d->data.b);
224 case QVariant::Double:
226 case QMetaType::Char:
227 case QMetaType::Short:
228 case QMetaType::Long:
229 case QMetaType::Float:
230 case QMetaType::LongLong:
231 return qulonglong(qMetaTypeNumber(d));
232 case QVariant::ULongLong:
234 case QMetaType::UChar:
235 case QMetaType::UShort:
236 case QMetaType::ULong:
237 return qMetaTypeUNumber(d);
241 return Q_UINT64_C(0);
244 template<typename TInput, typename LiteralWrapper>
245 inline bool qt_convertToBool(const QVariant::Private *const d)
247 TInput str = v_cast<TInput>(d)->toLower();
248 return !(str == LiteralWrapper("0") || str == LiteralWrapper("false") || str.isEmpty());
254 Converts \a d to type \a t, which is placed in \a result.
256 static bool convert(const QVariant::Private *d, QVariant::Type t, void *result, bool *ok)
258 Q_ASSERT(d->type != uint(t));
268 case QVariant::String:
269 *static_cast<QUrl *>(result) = QUrl(*v_cast<QString>(d));
275 case QVariant::String: {
276 QString *str = static_cast<QString *>(result);
279 *str = QString(*v_cast<QChar>(d));
281 case QMetaType::Char:
282 case QMetaType::UChar:
283 *str = QChar::fromAscii(d->data.c);
285 case QMetaType::Short:
286 case QMetaType::Long:
288 case QVariant::LongLong:
289 *str = QString::number(qMetaTypeNumber(d));
292 case QVariant::ULongLong:
293 case QMetaType::UShort:
294 case QMetaType::ULong:
295 *str = QString::number(qMetaTypeUNumber(d));
297 case QMetaType::Float:
298 *str = QString::number(d->data.f, 'g', FLT_DIG);
300 case QVariant::Double:
301 *str = QString::number(d->data.d, 'g', DBL_DIG);
303 #if !defined(QT_NO_DATESTRING)
305 *str = v_cast<QDate>(d)->toString(Qt::ISODate);
308 *str = v_cast<QTime>(d)->toString(Qt::ISODate);
310 case QVariant::DateTime:
311 *str = v_cast<QDateTime>(d)->toString(Qt::ISODate);
315 *str = QLatin1String(d->data.b ? "true" : "false");
317 case QVariant::ByteArray:
318 *str = QString::fromAscii(v_cast<QByteArray>(d)->constData());
320 case QVariant::StringList:
321 if (v_cast<QStringList>(d)->count() == 1)
322 *str = v_cast<QStringList>(d)->at(0);
325 *str = v_cast<QUrl>(d)->toString();
332 case QVariant::Char: {
333 QChar *c = static_cast<QChar *>(result);
336 case QVariant::LongLong:
337 case QMetaType::Char:
338 case QMetaType::Short:
339 case QMetaType::Long:
340 case QMetaType::Float:
341 *c = QChar(ushort(qMetaTypeNumber(d)));
344 case QVariant::ULongLong:
345 case QMetaType::UChar:
346 case QMetaType::UShort:
347 case QMetaType::ULong:
348 *c = QChar(ushort(qMetaTypeUNumber(d)));
355 #ifndef QT_NO_GEOM_VARIANT
356 case QVariant::Size: {
357 QSize *s = static_cast<QSize *>(result);
359 case QVariant::SizeF:
360 *s = v_cast<QSizeF>(d)->toSize();
368 case QVariant::SizeF: {
369 QSizeF *s = static_cast<QSizeF *>(result);
372 *s = QSizeF(*(v_cast<QSize>(d)));
380 case QVariant::Line: {
381 QLine *s = static_cast<QLine *>(result);
383 case QVariant::LineF:
384 *s = v_cast<QLineF>(d)->toLine();
392 case QVariant::LineF: {
393 QLineF *s = static_cast<QLineF *>(result);
396 *s = QLineF(*(v_cast<QLine>(d)));
404 case QVariant::StringList:
405 if (d->type == QVariant::List) {
406 QStringList *slst = static_cast<QStringList *>(result);
407 const QVariantList *list = v_cast<QVariantList >(d);
408 for (int i = 0; i < list->size(); ++i)
409 slst->append(list->at(i).toString());
410 } else if (d->type == QVariant::String) {
411 QStringList *slst = static_cast<QStringList *>(result);
412 *slst = QStringList(*v_cast<QString>(d));
417 case QVariant::Date: {
418 QDate *dt = static_cast<QDate *>(result);
419 if (d->type == QVariant::DateTime)
420 *dt = v_cast<QDateTime>(d)->date();
421 #ifndef QT_NO_DATESTRING
422 else if (d->type == QVariant::String)
423 *dt = QDate::fromString(*v_cast<QString>(d), Qt::ISODate);
428 return dt->isValid();
430 case QVariant::Time: {
431 QTime *t = static_cast<QTime *>(result);
433 case QVariant::DateTime:
434 *t = v_cast<QDateTime>(d)->time();
436 #ifndef QT_NO_DATESTRING
437 case QVariant::String:
438 *t = QTime::fromString(*v_cast<QString>(d), Qt::ISODate);
446 case QVariant::DateTime: {
447 QDateTime *dt = static_cast<QDateTime *>(result);
449 #ifndef QT_NO_DATESTRING
450 case QVariant::String:
451 *dt = QDateTime::fromString(*v_cast<QString>(d), Qt::ISODate);
455 *dt = QDateTime(*v_cast<QDate>(d));
460 return dt->isValid();
462 case QVariant::ByteArray: {
463 QByteArray *ba = static_cast<QByteArray *>(result);
465 case QVariant::String:
466 *ba = v_cast<QString>(d)->toAscii();
468 case QVariant::Double:
469 *ba = QByteArray::number(d->data.d, 'g', DBL_DIG);
471 case QMetaType::Float:
472 *ba = QByteArray::number(d->data.f, 'g', FLT_DIG);
474 case QMetaType::Char:
475 case QMetaType::UChar:
476 *ba = QByteArray(1, d->data.c);
479 case QVariant::LongLong:
480 case QMetaType::Short:
481 case QMetaType::Long:
482 *ba = QByteArray::number(qMetaTypeNumber(d));
485 case QVariant::ULongLong:
486 case QMetaType::UShort:
487 case QMetaType::ULong:
488 *ba = QByteArray::number(qMetaTypeUNumber(d));
491 *ba = QByteArray(d->data.b ? "true" : "false");
498 case QMetaType::Short:
499 *static_cast<short *>(result) = short(qConvertToNumber(d, ok));
501 case QMetaType::Long:
502 *static_cast<long *>(result) = long(qConvertToNumber(d, ok));
504 case QMetaType::UShort:
505 *static_cast<ushort *>(result) = ushort(qConvertToUnsignedNumber(d, ok));
507 case QMetaType::ULong:
508 *static_cast<ulong *>(result) = ulong(qConvertToUnsignedNumber(d, ok));
511 *static_cast<int *>(result) = int(qConvertToNumber(d, ok));
514 *static_cast<uint *>(result) = uint(qConvertToUnsignedNumber(d, ok));
516 case QVariant::LongLong:
517 *static_cast<qlonglong *>(result) = qConvertToNumber(d, ok);
519 case QVariant::ULongLong: {
520 *static_cast<qulonglong *>(result) = qConvertToUnsignedNumber(d, ok);
523 case QMetaType::UChar: {
524 *static_cast<uchar *>(result) = qConvertToUnsignedNumber(d, ok);
527 case QVariant::Bool: {
528 bool *b = static_cast<bool *>(result);
530 case QVariant::ByteArray:
531 *b = qt_convertToBool<QByteArray, QByteArray>(d);
533 case QVariant::String:
534 *b = qt_convertToBool<QString, QLatin1String>(d);
537 *b = !v_cast<QChar>(d)->isNull();
539 case QVariant::Double:
541 case QVariant::LongLong:
542 case QMetaType::Char:
543 case QMetaType::Short:
544 case QMetaType::Long:
545 case QMetaType::Float:
546 *b = qMetaTypeNumber(d) != Q_INT64_C(0);
549 case QVariant::ULongLong:
550 case QMetaType::UChar:
551 case QMetaType::UShort:
552 case QMetaType::ULong:
553 *b = qMetaTypeUNumber(d) != Q_UINT64_C(0);
561 case QVariant::Double: {
562 double *f = static_cast<double *>(result);
564 case QVariant::String:
565 *f = v_cast<QString>(d)->toDouble(ok);
567 case QVariant::ByteArray:
568 *f = v_cast<QByteArray>(d)->toDouble(ok);
571 *f = double(d->data.b);
573 case QMetaType::Float:
574 *f = double(d->data.f);
576 case QVariant::LongLong:
578 case QMetaType::Char:
579 case QMetaType::Short:
580 case QMetaType::Long:
581 *f = double(qMetaTypeNumber(d));
584 case QVariant::ULongLong:
585 case QMetaType::UChar:
586 case QMetaType::UShort:
587 case QMetaType::ULong:
588 *f = double(qMetaTypeUNumber(d));
596 case QMetaType::Float: {
597 float *f = static_cast<float *>(result);
599 case QVariant::String:
600 *f = v_cast<QString>(d)->toFloat(ok);
602 case QVariant::ByteArray:
603 *f = v_cast<QByteArray>(d)->toFloat(ok);
606 *f = float(d->data.b);
608 case QVariant::Double:
609 *f = float(d->data.d);
611 case QVariant::LongLong:
613 case QMetaType::Char:
614 case QMetaType::Short:
615 case QMetaType::Long:
616 *f = float(qMetaTypeNumber(d));
619 case QVariant::ULongLong:
620 case QMetaType::UChar:
621 case QMetaType::UShort:
622 case QMetaType::ULong:
623 *f = float(qMetaTypeUNumber(d));
632 if (d->type == QVariant::StringList) {
633 QVariantList *lst = static_cast<QVariantList *>(result);
634 const QStringList *slist = v_cast<QStringList>(d);
635 for (int i = 0; i < slist->size(); ++i)
636 lst->append(QVariant(slist->at(i)));
637 } else if (qstrcmp(QMetaType::typeName(d->type), "QList<QVariant>") == 0) {
638 *static_cast<QVariantList *>(result) =
639 *static_cast<QList<QVariant> *>(d->data.shared->ptr);
645 if (qstrcmp(QMetaType::typeName(d->type), "QMap<QString, QVariant>") == 0) {
646 *static_cast<QVariantMap *>(result) =
647 *static_cast<QMap<QString, QVariant> *>(d->data.shared->ptr);
653 if (qstrcmp(QMetaType::typeName(d->type), "QHash<QString, QVariant>") == 0) {
654 *static_cast<QVariantHash *>(result) =
655 *static_cast<QHash<QString, QVariant> *>(d->data.shared->ptr);
660 #ifndef QT_NO_GEOM_VARIANT
662 if (d->type == QVariant::RectF)
663 *static_cast<QRect *>(result) = (v_cast<QRectF>(d))->toRect();
667 case QVariant::RectF:
668 if (d->type == QVariant::Rect)
669 *static_cast<QRectF *>(result) = *v_cast<QRect>(d);
673 case QVariant::PointF:
674 if (d->type == QVariant::Point)
675 *static_cast<QPointF *>(result) = *v_cast<QPoint>(d);
679 case QVariant::Point:
680 if (d->type == QVariant::PointF)
681 *static_cast<QPoint *>(result) = (v_cast<QPointF>(d))->toPoint();
685 case QMetaType::Char:
687 *static_cast<qint8 *>(result) = qint8(qConvertToNumber(d, ok));
697 #if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)
698 static void streamDebug(QDebug dbg, const QVariant &v)
700 switch (v.userType()) {
702 dbg.nospace() << v.toInt();
705 dbg.nospace() << v.toUInt();
707 case QVariant::LongLong:
708 dbg.nospace() << v.toLongLong();
710 case QVariant::ULongLong:
711 dbg.nospace() << v.toULongLong();
713 case QMetaType::Float:
714 dbg.nospace() << v.toFloat();
716 case QMetaType::QObjectStar:
717 dbg.nospace() << qvariant_cast<QObject *>(v);
719 case QVariant::Double:
720 dbg.nospace() << v.toDouble();
723 dbg.nospace() << v.toBool();
725 case QVariant::String:
726 dbg.nospace() << v.toString();
729 dbg.nospace() << v.toChar();
731 case QVariant::StringList:
732 dbg.nospace() << v.toStringList();
735 dbg.nospace() << v.toMap();
738 dbg.nospace() << v.toHash();
741 dbg.nospace() << v.toList();
744 dbg.nospace() << v.toDate();
747 dbg.nospace() << v.toTime();
749 case QVariant::DateTime:
750 dbg.nospace() << v.toDateTime();
752 #ifndef QT_BOOTSTRAPPED
753 case QVariant::EasingCurve:
754 dbg.nospace() << v.toEasingCurve();
757 case QVariant::ByteArray:
758 dbg.nospace() << v.toByteArray();
761 dbg.nospace() << v.toUrl();
763 #ifndef QT_NO_GEOM_VARIANT
764 case QVariant::Point:
765 dbg.nospace() << v.toPoint();
767 case QVariant::PointF:
768 dbg.nospace() << v.toPointF();
771 dbg.nospace() << v.toRect();
774 dbg.nospace() << v.toSize();
776 case QVariant::SizeF:
777 dbg.nospace() << v.toSizeF();
780 dbg.nospace() << v.toLine();
782 case QVariant::LineF:
783 dbg.nospace() << v.toLineF();
785 case QVariant::RectF:
786 dbg.nospace() << v.toRectF();
789 case QVariant::BitArray:
790 //dbg.nospace() << v.toBitArray();
798 const QVariant::Handler qt_kernel_variant_handler = {
802 #ifndef QT_NO_DATASTREAM
809 #if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)
816 Q_CORE_EXPORT const QVariant::Handler *qcoreVariantHandler()
818 return &qt_kernel_variant_handler;
822 const QVariant::Handler *QVariant::handler = &qt_kernel_variant_handler;
826 \brief The QVariant class acts like a union for the most common Qt data types.
832 Because C++ forbids unions from including types that have
833 non-default constructors or destructors, most interesting Qt
834 classes cannot be used in unions. Without QVariant, this would be
835 a problem for QObject::property() and for database work, etc.
837 A QVariant object holds a single value of a single type() at a
838 time. (Some type()s are multi-valued, for example a string list.)
839 You can find out what type, T, the variant holds, convert it to a
840 different type using convert(), get its value using one of the
841 toT() functions (e.g., toSize()) and check whether the type can
842 be converted to a particular type using canConvert().
844 The methods named toT() (e.g., toInt(), toString()) are const. If
845 you ask for the stored type, they return a copy of the stored
846 object. If you ask for a type that can be generated from the
847 stored type, toT() copies and converts and leaves the object
848 itself unchanged. If you ask for a type that cannot be generated
849 from the stored type, the result depends on the type; see the
850 function documentation for details.
852 Here is some example code to demonstrate the use of QVariant:
854 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 0
856 You can even store QList<QVariant> and QMap<QString, QVariant>
857 values in a variant, so you can easily construct arbitrarily
858 complex data structures of arbitrary types. This is very powerful
859 and versatile, but may prove less memory and speed efficient than
860 storing specific types in standard data structures.
862 QVariant also supports the notion of null values, where you can
863 have a defined type with no value set. However, note that QVariant
864 types can only be cast when they have had a value set.
866 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 1
868 QVariant can be extended to support other types than those
869 mentioned in the \l Type enum. See the \l QMetaType documentation
872 \section1 A Note on GUI Types
874 Because QVariant is part of the QtCore library, it cannot provide
875 conversion functions to data types defined in QtGui, such as
876 QColor, QImage, and QPixmap. In other words, there is no \c
877 toColor() function. Instead, you can use the QVariant::value() or
878 the qvariant_cast() template function. For example:
880 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 2
882 The inverse conversion (e.g., from QColor to QVariant) is
883 automatic for all data types supported by QVariant, including
886 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 3
888 \section1 Using canConvert() and convert() Consecutively
890 When using canConvert() and convert() consecutively, it is possible for
891 canConvert() to return true, but convert() to return false. This
892 is typically because canConvert() only reports the general ability of
893 QVariant to convert between types given suitable data; it is still
894 possible to supply data which cannot actually be converted.
896 For example, canConvert() would return true when called on a variant
897 containing a string because, in principle, QVariant is able to convert
898 strings of numbers to integers.
899 However, if the string contains non-numeric characters, it cannot be
900 converted to an integer, and any attempt to convert it will fail.
901 Hence, it is important to have both functions return true for a
902 successful conversion.
910 This enum type defines the types of variable that a QVariant can
913 \value Invalid no type
914 \value BitArray a QBitArray
915 \value Bitmap a QBitmap
917 \value Brush a QBrush
918 \value ByteArray a QByteArray
920 \value Color a QColor
921 \value Cursor a QCursor
923 \value DateTime a QDateTime
924 \value Double a double
925 \value EasingCurve a QEasingCurve
927 \value Hash a QVariantHash
929 \value Image a QImage
931 \value KeySequence a QKeySequence
933 \value LineF a QLineF
934 \value List a QVariantList
935 \value Locale a QLocale
936 \value LongLong a \l qlonglong
937 \value Map a QVariantMap
938 \value Matrix a QMatrix
939 \value Transform a QTransform
940 \value Matrix4x4 a QMatrix4x4
941 \value Palette a QPalette
943 \value Pixmap a QPixmap
944 \value Point a QPoint
945 \value PointF a QPointF
946 \value Polygon a QPolygon
947 \value PolygonF a QPolygonF
948 \value Quaternion a QQuaternion
950 \value RectF a QRectF
951 \value RegExp a QRegExp
952 \value Region a QRegion
954 \value SizeF a QSizeF
955 \value SizePolicy a QSizePolicy
956 \value String a QString
957 \value StringList a QStringList
958 \value TextFormat a QTextFormat
959 \value TextLength a QTextLength
961 \value UInt a \l uint
962 \value ULongLong a \l qulonglong
964 \value Vector2D a QVector2D
965 \value Vector3D a QVector3D
966 \value Vector4D a QVector4D
968 \value UserType Base value for user-defined types.
971 \omitvalue ColorGroup
973 \omitvalue LastGuiType
974 \omitvalue LastCoreType
979 \fn QVariant::QVariant()
981 Constructs an invalid variant.
986 \fn QVariant::QVariant(int typeOrUserType, const void *copy)
988 Constructs variant of type \a typeOrUserType, and initializes with
989 \a copy if \a copy is not 0.
991 Note that you have to pass the address of the variable you want stored.
993 Usually, you never have to use this constructor, use QVariant::fromValue()
994 instead to construct variants from the pointer types represented by
995 \c QMetaType::VoidStar, \c QMetaType::QObjectStar and
996 \c QMetaType::QWidgetStar.
998 \sa QVariant::fromValue(), Type
1002 \fn QVariant::QVariant(Type type)
1004 Constructs a null variant of type \a type.
1010 \fn QVariant::create(int type, const void *copy)
1014 Constructs a variant private of type \a type, and initializes with \a copy if
1018 void QVariant::create(int type, const void *copy)
1021 handler->construct(&d, copy);
1025 \fn QVariant::~QVariant()
1027 Destroys the QVariant and the contained object.
1029 Note that subclasses that reimplement clear() should reimplement
1030 the destructor to call clear(). This destructor calls clear(), but
1031 because it is the destructor, QVariant::clear() is called rather
1032 than a subclass's clear().
1035 QVariant::~QVariant()
1037 if ((d.is_shared && !d.data.shared->ref.deref()) || (!d.is_shared && d.type > Char))
1042 \fn QVariant::QVariant(const QVariant &p)
1044 Constructs a copy of the variant, \a p, passed as the argument to
1048 QVariant::QVariant(const QVariant &p)
1052 d.data.shared->ref.ref();
1053 } else if (p.d.type > Char) {
1054 handler->construct(&d, p.constData());
1055 d.is_null = p.d.is_null;
1059 #ifndef QT_NO_DATASTREAM
1061 Reads the variant from the data stream, \a s.
1063 QVariant::QVariant(QDataStream &s)
1068 #endif //QT_NO_DATASTREAM
1071 \fn QVariant::QVariant(const QString &val)
1073 Constructs a new variant with a string value, \a val.
1077 \fn QVariant::QVariant(const QLatin1String &val)
1079 Constructs a new variant with a string value, \a val.
1083 \fn QVariant::QVariant(const char *val)
1085 Constructs a new variant with a string value of \a val.
1086 The variant creates a deep copy of \a val, using the encoding
1087 set by QTextCodec::setCodecForCStrings().
1089 Note that \a val is converted to a QString for storing in the
1090 variant and QVariant::type() will return QMetaType::QString for
1093 You can disable this operator by defining \c
1094 QT_NO_CAST_FROM_ASCII when you compile your applications.
1096 \sa QTextCodec::setCodecForCStrings()
1099 #ifndef QT_NO_CAST_FROM_ASCII
1100 QVariant::QVariant(const char *val)
1102 QString s = QString::fromAscii(val);
1108 \fn QVariant::QVariant(const QStringList &val)
1110 Constructs a new variant with a string list value, \a val.
1114 \fn QVariant::QVariant(const QMap<QString, QVariant> &val)
1116 Constructs a new variant with a map of QVariants, \a val.
1120 \fn QVariant::QVariant(const QHash<QString, QVariant> &val)
1122 Constructs a new variant with a hash of QVariants, \a val.
1126 \fn QVariant::QVariant(const QDate &val)
1128 Constructs a new variant with a date value, \a val.
1132 \fn QVariant::QVariant(const QTime &val)
1134 Constructs a new variant with a time value, \a val.
1138 \fn QVariant::QVariant(const QDateTime &val)
1140 Constructs a new variant with a date/time value, \a val.
1145 \fn QVariant::QVariant(const QEasingCurve &val)
1147 Constructs a new variant with an easing curve value, \a val.
1151 \fn QVariant::QVariant(const QByteArray &val)
1153 Constructs a new variant with a bytearray value, \a val.
1157 \fn QVariant::QVariant(const QBitArray &val)
1159 Constructs a new variant with a bitarray value, \a val.
1163 \fn QVariant::QVariant(const QPoint &val)
1165 Constructs a new variant with a point value of \a val.
1169 \fn QVariant::QVariant(const QPointF &val)
1171 Constructs a new variant with a point value of \a val.
1175 \fn QVariant::QVariant(const QRectF &val)
1177 Constructs a new variant with a rect value of \a val.
1181 \fn QVariant::QVariant(const QLineF &val)
1183 Constructs a new variant with a line value of \a val.
1187 \fn QVariant::QVariant(const QLine &val)
1189 Constructs a new variant with a line value of \a val.
1193 \fn QVariant::QVariant(const QRect &val)
1195 Constructs a new variant with a rect value of \a val.
1199 \fn QVariant::QVariant(const QSize &val)
1201 Constructs a new variant with a size value of \a val.
1205 \fn QVariant::QVariant(const QSizeF &val)
1207 Constructs a new variant with a size value of \a val.
1211 \fn QVariant::QVariant(const QUrl &val)
1213 Constructs a new variant with a url value of \a val.
1217 \fn QVariant::QVariant(int val)
1219 Constructs a new variant with an integer value, \a val.
1223 \fn QVariant::QVariant(uint val)
1225 Constructs a new variant with an unsigned integer value, \a val.
1229 \fn QVariant::QVariant(qlonglong val)
1231 Constructs a new variant with a long long integer value, \a val.
1235 \fn QVariant::QVariant(qulonglong val)
1237 Constructs a new variant with an unsigned long long integer value, \a val.
1242 \fn QVariant::QVariant(bool val)
1244 Constructs a new variant with a boolean value, \a val.
1248 \fn QVariant::QVariant(double val)
1250 Constructs a new variant with a floating point value, \a val.
1254 \fn QVariant::QVariant(float val)
1256 Constructs a new variant with a floating point value, \a val.
1261 \fn QVariant::QVariant(const QList<QVariant> &val)
1263 Constructs a new variant with a list value, \a val.
1267 \fn QVariant::QVariant(const QChar &c)
1269 Constructs a new variant with a char value, \a c.
1273 \fn QVariant::QVariant(const QLocale &l)
1275 Constructs a new variant with a locale value, \a l.
1279 \fn QVariant::QVariant(const QRegExp ®Exp)
1281 Constructs a new variant with the regexp value \a regExp.
1285 \fn QVariant::QVariant(Qt::GlobalColor color)
1287 Constructs a new variant of type QVariant::Color and initializes
1290 This is a convenience constructor that allows \c{QVariant(Qt::blue);}
1291 to create a valid QVariant storing a QColor.
1293 Note: This constructor will assert if the application does not link
1294 to the Qt GUI library.
1297 QVariant::QVariant(Type type)
1298 { create(type, 0); }
1299 QVariant::QVariant(int typeOrUserType, const void *copy)
1300 { create(typeOrUserType, copy); d.is_null = false; }
1303 flags is true if it is a pointer type
1305 QVariant::QVariant(int typeOrUserType, const void *copy, uint flags)
1307 if (flags) { //type is a pointer type
1308 d.type = typeOrUserType;
1309 d.data.ptr = *reinterpret_cast<void *const*>(copy);
1311 create(typeOrUserType, copy);
1316 QVariant::QVariant(int val)
1317 { d.is_null = false; d.type = Int; d.data.i = val; }
1318 QVariant::QVariant(uint val)
1319 { d.is_null = false; d.type = UInt; d.data.u = val; }
1320 QVariant::QVariant(qlonglong val)
1321 { d.is_null = false; d.type = LongLong; d.data.ll = val; }
1322 QVariant::QVariant(qulonglong val)
1323 { d.is_null = false; d.type = ULongLong; d.data.ull = val; }
1324 QVariant::QVariant(bool val)
1325 { d.is_null = false; d.type = Bool; d.data.b = val; }
1326 QVariant::QVariant(double val)
1327 { d.is_null = false; d.type = Double; d.data.d = val; }
1329 QVariant::QVariant(const QByteArray &val)
1330 { d.is_null = false; d.type = ByteArray; v_construct<QByteArray>(&d, val); }
1331 QVariant::QVariant(const QBitArray &val)
1332 { d.is_null = false; d.type = BitArray; v_construct<QBitArray>(&d, val); }
1333 QVariant::QVariant(const QString &val)
1334 { d.is_null = false; d.type = String; v_construct<QString>(&d, val); }
1335 QVariant::QVariant(const QChar &val)
1336 { d.is_null = false; d.type = Char; v_construct<QChar>(&d, val); }
1337 QVariant::QVariant(const QLatin1String &val)
1338 { QString str(val); d.is_null = false; d.type = String; v_construct<QString>(&d, str); }
1339 QVariant::QVariant(const QStringList &val)
1340 { d.is_null = false; d.type = StringList; v_construct<QStringList>(&d, val); }
1342 QVariant::QVariant(const QDate &val)
1343 { d.is_null = false; d.type = Date; v_construct<QDate>(&d, val); }
1344 QVariant::QVariant(const QTime &val)
1345 { d.is_null = false; d.type = Time; v_construct<QTime>(&d, val); }
1346 QVariant::QVariant(const QDateTime &val)
1347 { d.is_null = false; d.type = DateTime; v_construct<QDateTime>(&d, val); }
1348 #ifndef QT_BOOTSTRAPPED
1349 QVariant::QVariant(const QEasingCurve &val)
1350 { d.is_null = false; d.type = EasingCurve; v_construct<QEasingCurve>(&d, val); }
1352 QVariant::QVariant(const QList<QVariant> &list)
1353 { d.is_null = false; d.type = List; v_construct<QVariantList>(&d, list); }
1354 QVariant::QVariant(const QMap<QString, QVariant> &map)
1355 { d.is_null = false; d.type = Map; v_construct<QVariantMap>(&d, map); }
1356 QVariant::QVariant(const QHash<QString, QVariant> &hash)
1357 { d.is_null = false; d.type = Hash; v_construct<QVariantHash>(&d, hash); }
1358 #ifndef QT_NO_GEOM_VARIANT
1359 QVariant::QVariant(const QPoint &pt) { d.is_null = false; d.type = Point; v_construct<QPoint>(&d, pt); }
1360 QVariant::QVariant(const QPointF &pt) { d.is_null = false; d.type = PointF; v_construct<QPointF>(&d, pt); }
1361 QVariant::QVariant(const QRectF &r) { d.is_null = false; d.type = RectF; v_construct<QRectF>(&d, r); }
1362 QVariant::QVariant(const QLineF &l) { d.is_null = false; d.type = LineF; v_construct<QLineF>(&d, l); }
1363 QVariant::QVariant(const QLine &l) { d.is_null = false; d.type = Line; v_construct<QLine>(&d, l); }
1364 QVariant::QVariant(const QRect &r) { d.is_null = false; d.type = Rect; v_construct<QRect>(&d, r); }
1365 QVariant::QVariant(const QSize &s) { d.is_null = false; d.type = Size; v_construct<QSize>(&d, s); }
1366 QVariant::QVariant(const QSizeF &s) { d.is_null = false; d.type = SizeF; v_construct<QSizeF>(&d, s); }
1368 QVariant::QVariant(const QUrl &u) { d.is_null = false; d.type = Url; v_construct<QUrl>(&d, u); }
1369 QVariant::QVariant(const QLocale &l) { d.is_null = false; d.type = Locale; v_construct<QLocale>(&d, l); }
1370 #ifndef QT_NO_REGEXP
1371 QVariant::QVariant(const QRegExp ®Exp) { d.is_null = false; d.type = RegExp; v_construct<QRegExp>(&d, regExp); }
1373 QVariant::QVariant(Qt::GlobalColor color) { create(62, &color); }
1376 Returns the storage type of the value stored in the variant.
1377 Although this function is declared as returning QVariant::Type,
1378 the return value should be interpreted as QMetaType::Type. In
1379 particular, QVariant::UserType is returned here only if the value
1380 is equal or greater than QMetaType::User.
1382 Note that return values in the ranges QVariant::Char through
1383 QVariant::RegExp and QVariant::Font through QVariant::Transform
1384 correspond to the values in the ranges QMetaType::QChar through
1385 QMetaType::QRegExp and QMetaType::QFont through QMetaType::QQuaternion.
1387 Pay particular attention when working with char and QChar
1388 variants. Note that there is no QVariant constructor specifically
1389 for type char, but there is one for QChar. For a variant of type
1390 QChar, this function returns QVariant::Char, which is the same as
1391 QMetaType::QChar, but for a variant of type \c char, this function
1392 returns QMetaType::Char, which is \e not the same as
1395 Also note that the types \c void*, \c long, \c short, \c unsigned
1396 \c long, \c unsigned \c short, \c unsigned \c char, \c float, \c
1397 QObject*, and \c QWidget* are represented in QMetaType::Type but
1398 not in QVariant::Type, and they can be returned by this function.
1399 However, they are considered to be user defined types when tested
1400 against QVariant::Type.
1402 To test whether an instance of QVariant contains a data type that
1403 is compatible with the data type you are interested in, use
1407 QVariant::Type QVariant::type() const
1409 return d.type >= QMetaType::User ? UserType : static_cast<Type>(d.type);
1413 Returns the storage type of the value stored in the variant. For
1414 non-user types, this is the same as type().
1419 int QVariant::userType() const
1425 Assigns the value of the variant \a variant to this variant.
1427 QVariant& QVariant::operator=(const QVariant &variant)
1429 if (this == &variant)
1433 if (variant.d.is_shared) {
1434 variant.d.data.shared->ref.ref();
1436 } else if (variant.d.type > Char) {
1437 d.type = variant.d.type;
1438 handler->construct(&d, variant.constData());
1439 d.is_null = variant.d.is_null;
1448 \fn void QVariant::swap(QVariant &other)
1451 Swaps variant \a other with this variant. This operation is very
1452 fast and never fails.
1456 \fn void QVariant::detach()
1461 void QVariant::detach()
1463 if (!d.is_shared || d.data.shared->ref.load() == 1)
1468 handler->construct(&dd, constData());
1469 if (!d.data.shared->ref.deref())
1471 d.data.shared = dd.data.shared;
1475 \fn bool QVariant::isDetached() const
1480 // ### Qt 5: change typeName()(and froends= to return a QString. Suggestion from Harald.
1482 Returns the name of the type stored in the variant. The returned
1483 strings describe the C++ datatype used to store the data: for
1484 example, "QFont", "QString", or "QVariantList". An Invalid
1487 const char *QVariant::typeName() const
1489 return typeToName(Type(d.type));
1493 Convert this variant to type Invalid and free up any resources
1496 void QVariant::clear()
1498 if ((d.is_shared && !d.data.shared->ref.deref()) || (!d.is_shared && d.type > Char))
1502 d.is_shared = false;
1506 Converts the enum representation of the storage type, \a typ, to
1507 its string representation.
1509 Returns a null pointer if the type is QVariant::Invalid or doesn't exist.
1511 const char *QVariant::typeToName(Type typ)
1515 if (typ == UserType)
1518 return QMetaType::typeName(typ);
1523 Converts the string representation of the storage type given in \a
1524 name, to its enum representation.
1526 If the string representation cannot be converted to any enum
1527 representation, the variant is set to \c Invalid.
1529 QVariant::Type QVariant::nameToType(const char *name)
1531 if (!name || !*name)
1533 if (strcmp(name, "Q3CString") == 0)
1535 if (strcmp(name, "Q_LLONG") == 0)
1537 if (strcmp(name, "Q_ULLONG") == 0)
1539 if (strcmp(name, "QIconSet") == 0)
1541 if (strcmp(name, "UserType") == 0)
1544 int metaType = QMetaType::type(name);
1545 return metaType <= int(LastGuiType) ? QVariant::Type(metaType) : UserType;
1548 #ifndef QT_NO_DATASTREAM
1549 enum { MapFromThreeCount = 36 };
1550 static const ushort map_from_three[MapFromThreeCount] =
1556 QVariant::StringList,
1572 QVariant::ByteArray,
1577 QVariant::SizePolicy,
1581 QVariant::ByteArray,
1583 QVariant::KeySequence,
1586 QVariant::ULongLong,
1587 QVariant::EasingCurve
1591 Internal function for loading a variant from stream \a s. Use the
1592 stream operators instead.
1596 void QVariant::load(QDataStream &s)
1602 if (s.version() < QDataStream::Qt_4_0) {
1603 if (u >= MapFromThreeCount)
1605 u = map_from_three[u];
1607 qint8 is_null = false;
1608 if (s.version() >= QDataStream::Qt_4_2)
1610 if (u == QVariant::UserType) {
1613 u = QMetaType::type(name);
1615 s.setStatus(QDataStream::ReadCorruptData);
1619 create(static_cast<int>(u), 0);
1620 d.is_null = is_null;
1623 // Since we wrote something, we should read something
1630 // const cast is safe since we operate on a newly constructed variant
1631 if (!QMetaType::load(s, d.type, const_cast<void *>(constData()))) {
1632 s.setStatus(QDataStream::ReadCorruptData);
1633 qWarning("QVariant::load: unable to load type %d.", d.type);
1638 Internal function for saving a variant to the stream \a s. Use the
1639 stream operators instead.
1643 void QVariant::save(QDataStream &s) const
1645 quint32 tp = type();
1646 if (s.version() < QDataStream::Qt_4_0) {
1648 for (i = MapFromThreeCount - 1; i >= 0; i--) {
1649 if (map_from_three[i] == tp) {
1660 if (s.version() >= QDataStream::Qt_4_2)
1661 s << qint8(d.is_null);
1662 if (tp == QVariant::UserType) {
1663 s << QMetaType::typeName(userType());
1671 if (!QMetaType::save(s, d.type, constData())) {
1672 Q_ASSERT_X(false, "QVariant::save", "Invalid type to save");
1673 qWarning("QVariant::save: unable to save type %d.", d.type);
1680 Reads a variant \a p from the stream \a s.
1682 \sa \link datastreamformat.html Format of the QDataStream
1685 QDataStream& operator>>(QDataStream &s, QVariant &p)
1692 Writes a variant \a p to the stream \a s.
1694 \sa \link datastreamformat.html Format of the QDataStream
1697 QDataStream& operator<<(QDataStream &s, const QVariant &p)
1704 Reads a variant type \a p in enum representation from the stream \a s.
1706 QDataStream& operator>>(QDataStream &s, QVariant::Type &p)
1710 p = (QVariant::Type)u;
1716 Writes a variant type \a p to the stream \a s.
1718 QDataStream& operator<<(QDataStream &s, const QVariant::Type p)
1720 s << static_cast<quint32>(p);
1725 #endif //QT_NO_DATASTREAM
1728 \fn bool QVariant::isValid() const
1730 Returns true if the storage type of this variant is not
1731 QVariant::Invalid; otherwise returns false.
1734 template <typename T>
1735 inline T qVariantToHelper(const QVariant::Private &d, QVariant::Type t,
1736 const QVariant::Handler *handler, T * = 0)
1739 return *v_cast<T>(&d);
1742 handler->convert(&d, t, &ret, 0);
1747 \fn QStringList QVariant::toStringList() const
1749 Returns the variant as a QStringList if the variant has type()
1750 StringList, \l String, or \l List of a type that can be converted
1751 to QString; otherwise returns an empty list.
1753 \sa canConvert(), convert()
1755 QStringList QVariant::toStringList() const
1757 return qVariantToHelper<QStringList>(d, StringList, handler);
1761 Returns the variant as a QString if the variant has type() \l
1762 String, \l Bool, \l ByteArray, \l Char, \l Date, \l DateTime, \l
1763 Double, \l Int, \l LongLong, \l StringList, \l Time, \l UInt, or
1764 \l ULongLong; otherwise returns an empty string.
1766 \sa canConvert(), convert()
1768 QString QVariant::toString() const
1770 return qVariantToHelper<QString>(d, String, handler);
1774 Returns the variant as a QMap<QString, QVariant> if the variant
1775 has type() \l Map; otherwise returns an empty map.
1777 \sa canConvert(), convert()
1779 QVariantMap QVariant::toMap() const
1781 return qVariantToHelper<QVariantMap>(d, Map, handler);
1785 Returns the variant as a QHash<QString, QVariant> if the variant
1786 has type() \l Hash; otherwise returns an empty map.
1788 \sa canConvert(), convert()
1790 QVariantHash QVariant::toHash() const
1792 return qVariantToHelper<QVariantHash>(d, Hash, handler);
1796 \fn QDate QVariant::toDate() const
1798 Returns the variant as a QDate if the variant has type() \l Date,
1799 \l DateTime, or \l String; otherwise returns an invalid date.
1801 If the type() is \l String, an invalid date will be returned if the
1802 string cannot be parsed as a Qt::ISODate format date.
1804 \sa canConvert(), convert()
1806 QDate QVariant::toDate() const
1808 return qVariantToHelper<QDate>(d, Date, handler);
1812 \fn QTime QVariant::toTime() const
1814 Returns the variant as a QTime if the variant has type() \l Time,
1815 \l DateTime, or \l String; otherwise returns an invalid time.
1817 If the type() is \l String, an invalid time will be returned if
1818 the string cannot be parsed as a Qt::ISODate format time.
1820 \sa canConvert(), convert()
1822 QTime QVariant::toTime() const
1824 return qVariantToHelper<QTime>(d, Time, handler);
1828 \fn QDateTime QVariant::toDateTime() const
1830 Returns the variant as a QDateTime if the variant has type() \l
1831 DateTime, \l Date, or \l String; otherwise returns an invalid
1834 If the type() is \l String, an invalid date/time will be returned
1835 if the string cannot be parsed as a Qt::ISODate format date/time.
1837 \sa canConvert(), convert()
1839 QDateTime QVariant::toDateTime() const
1841 return qVariantToHelper<QDateTime>(d, DateTime, handler);
1846 \fn QEasingCurve QVariant::toEasingCurve() const
1848 Returns the variant as a QEasingCurve if the variant has type() \l
1849 EasingCurve; otherwise returns a default easing curve.
1851 \sa canConvert(), convert()
1853 #ifndef QT_BOOTSTRAPPED
1854 QEasingCurve QVariant::toEasingCurve() const
1856 return qVariantToHelper<QEasingCurve>(d, EasingCurve, handler);
1861 \fn QByteArray QVariant::toByteArray() const
1863 Returns the variant as a QByteArray if the variant has type() \l
1864 ByteArray or \l String (converted using QString::fromAscii());
1865 otherwise returns an empty byte array.
1867 \sa canConvert(), convert()
1869 QByteArray QVariant::toByteArray() const
1871 return qVariantToHelper<QByteArray>(d, ByteArray, handler);
1874 #ifndef QT_NO_GEOM_VARIANT
1876 \fn QPoint QVariant::toPoint() const
1878 Returns the variant as a QPoint if the variant has type()
1879 \l Point or \l PointF; otherwise returns a null QPoint.
1881 \sa canConvert(), convert()
1883 QPoint QVariant::toPoint() const
1885 return qVariantToHelper<QPoint>(d, Point, handler);
1889 \fn QRect QVariant::toRect() const
1891 Returns the variant as a QRect if the variant has type() \l Rect;
1892 otherwise returns an invalid QRect.
1894 \sa canConvert(), convert()
1896 QRect QVariant::toRect() const
1898 return qVariantToHelper<QRect>(d, Rect, handler);
1902 \fn QSize QVariant::toSize() const
1904 Returns the variant as a QSize if the variant has type() \l Size;
1905 otherwise returns an invalid QSize.
1907 \sa canConvert(), convert()
1909 QSize QVariant::toSize() const
1911 return qVariantToHelper<QSize>(d, Size, handler);
1915 \fn QSizeF QVariant::toSizeF() const
1917 Returns the variant as a QSizeF if the variant has type() \l
1918 SizeF; otherwise returns an invalid QSizeF.
1920 \sa canConvert(), convert()
1922 QSizeF QVariant::toSizeF() const
1924 return qVariantToHelper<QSizeF>(d, SizeF, handler);
1928 \fn QRectF QVariant::toRectF() const
1930 Returns the variant as a QRectF if the variant has type() \l Rect
1931 or \l RectF; otherwise returns an invalid QRectF.
1933 \sa canConvert(), convert()
1935 QRectF QVariant::toRectF() const
1937 return qVariantToHelper<QRectF>(d, RectF, handler);
1941 \fn QLineF QVariant::toLineF() const
1943 Returns the variant as a QLineF if the variant has type() \l
1944 LineF; otherwise returns an invalid QLineF.
1946 \sa canConvert(), convert()
1948 QLineF QVariant::toLineF() const
1950 return qVariantToHelper<QLineF>(d, LineF, handler);
1954 \fn QLine QVariant::toLine() const
1956 Returns the variant as a QLine if the variant has type() \l Line;
1957 otherwise returns an invalid QLine.
1959 \sa canConvert(), convert()
1961 QLine QVariant::toLine() const
1963 return qVariantToHelper<QLine>(d, Line, handler);
1967 \fn QPointF QVariant::toPointF() const
1969 Returns the variant as a QPointF if the variant has type() \l
1970 Point or \l PointF; otherwise returns a null QPointF.
1972 \sa canConvert(), convert()
1974 QPointF QVariant::toPointF() const
1976 return qVariantToHelper<QPointF>(d, PointF, handler);
1979 #endif // QT_NO_GEOM_VARIANT
1982 \fn QUrl QVariant::toUrl() const
1984 Returns the variant as a QUrl if the variant has type()
1985 \l Url; otherwise returns an invalid QUrl.
1987 \sa canConvert(), convert()
1989 QUrl QVariant::toUrl() const
1991 return qVariantToHelper<QUrl>(d, Url, handler);
1995 \fn QLocale QVariant::toLocale() const
1997 Returns the variant as a QLocale if the variant has type()
1998 \l Locale; otherwise returns an invalid QLocale.
2000 \sa canConvert(), convert()
2002 QLocale QVariant::toLocale() const
2004 return qVariantToHelper<QLocale>(d, Locale, handler);
2008 \fn QRegExp QVariant::toRegExp() const
2011 Returns the variant as a QRegExp if the variant has type() \l
2012 RegExp; otherwise returns an empty QRegExp.
2014 \sa canConvert(), convert()
2016 #ifndef QT_NO_REGEXP
2017 QRegExp QVariant::toRegExp() const
2019 return qVariantToHelper<QRegExp>(d, RegExp, handler);
2024 \fn QChar QVariant::toChar() const
2026 Returns the variant as a QChar if the variant has type() \l Char,
2027 \l Int, or \l UInt; otherwise returns an invalid QChar.
2029 \sa canConvert(), convert()
2031 QChar QVariant::toChar() const
2033 return qVariantToHelper<QChar>(d, Char, handler);
2037 Returns the variant as a QBitArray if the variant has type()
2038 \l BitArray; otherwise returns an empty bit array.
2040 \sa canConvert(), convert()
2042 QBitArray QVariant::toBitArray() const
2044 return qVariantToHelper<QBitArray>(d, BitArray, handler);
2047 template <typename T>
2048 inline T qNumVariantToHelper(const QVariant::Private &d,
2049 const QVariant::Handler *handler, bool *ok, const T& val)
2051 uint t = qMetaTypeId<T>();
2058 if (!handler->convert(&d, QVariant::Type(t), &ret, ok) && ok)
2064 Returns the variant as an int if the variant has type() \l Int,
2065 \l Bool, \l ByteArray, \l Char, \l Double, \l LongLong, \l
2066 String, \l UInt, or \l ULongLong; otherwise returns 0.
2068 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2069 converted to an int; otherwise \c{*}\a{ok} is set to false.
2071 \bold{Warning:} If the value is convertible to a \l LongLong but is too
2072 large to be represented in an int, the resulting arithmetic overflow will
2073 not be reflected in \a ok. A simple workaround is to use QString::toInt().
2074 Fixing this bug has been postponed to Qt 5 in order to avoid breaking existing code.
2076 \sa canConvert(), convert()
2078 int QVariant::toInt(bool *ok) const
2080 return qNumVariantToHelper<int>(d, handler, ok, d.data.i);
2084 Returns the variant as an unsigned int if the variant has type()
2085 \l UInt, \l Bool, \l ByteArray, \l Char, \l Double, \l Int, \l
2086 LongLong, \l String, or \l ULongLong; otherwise returns 0.
2088 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2089 converted to an unsigned int; otherwise \c{*}\a{ok} is set to false.
2091 \bold{Warning:} If the value is convertible to a \l ULongLong but is too
2092 large to be represented in an unsigned int, the resulting arithmetic overflow will
2093 not be reflected in \a ok. A simple workaround is to use QString::toUInt().
2094 Fixing this bug has been postponed to Qt 5 in order to avoid breaking existing code.
2096 \sa canConvert(), convert()
2098 uint QVariant::toUInt(bool *ok) const
2100 return qNumVariantToHelper<uint>(d, handler, ok, d.data.u);
2104 Returns the variant as a long long int if the variant has type()
2105 \l LongLong, \l Bool, \l ByteArray, \l Char, \l Double, \l Int,
2106 \l String, \l UInt, or \l ULongLong; otherwise returns 0.
2108 If \a ok is non-null: \c{*}\c{ok} is set to true if the value could be
2109 converted to an int; otherwise \c{*}\c{ok} is set to false.
2111 \sa canConvert(), convert()
2113 qlonglong QVariant::toLongLong(bool *ok) const
2115 return qNumVariantToHelper<qlonglong>(d, handler, ok, d.data.ll);
2119 Returns the variant as as an unsigned long long int if the
2120 variant has type() \l ULongLong, \l Bool, \l ByteArray, \l Char,
2121 \l Double, \l Int, \l LongLong, \l String, or \l UInt; otherwise
2124 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2125 converted to an int; otherwise \c{*}\a{ok} is set to false.
2127 \sa canConvert(), convert()
2129 qulonglong QVariant::toULongLong(bool *ok) const
2131 return qNumVariantToHelper<qulonglong>(d, handler, ok, d.data.ull);
2135 Returns the variant as a bool if the variant has type() Bool.
2137 Returns true if the variant has type() \l Bool, \l Char, \l Double,
2138 \l Int, \l LongLong, \l UInt, or \l ULongLong and the value is
2139 non-zero, or if the variant has type \l String or \l ByteArray and
2140 its lower-case content is not empty, "0" or "false"; otherwise
2143 \sa canConvert(), convert()
2145 bool QVariant::toBool() const
2151 handler->convert(&d, Bool, &res, 0);
2157 Returns the variant as a double if the variant has type() \l
2158 Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
2159 UInt, or \l ULongLong; otherwise returns 0.0.
2161 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2162 converted to a double; otherwise \c{*}\a{ok} is set to false.
2164 \sa canConvert(), convert()
2166 double QVariant::toDouble(bool *ok) const
2168 return qNumVariantToHelper<double>(d, handler, ok, d.data.d);
2172 Returns the variant as a float if the variant has type() \l
2173 Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
2174 UInt, or \l ULongLong; otherwise returns 0.0.
2178 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2179 converted to a double; otherwise \c{*}\a{ok} is set to false.
2181 \sa canConvert(), convert()
2183 float QVariant::toFloat(bool *ok) const
2185 return qNumVariantToHelper<float>(d, handler, ok, d.data.f);
2189 Returns the variant as a qreal if the variant has type() \l
2190 Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
2191 UInt, or \l ULongLong; otherwise returns 0.0.
2195 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2196 converted to a double; otherwise \c{*}\a{ok} is set to false.
2198 \sa canConvert(), convert()
2200 qreal QVariant::toReal(bool *ok) const
2202 return qNumVariantToHelper<qreal>(d, handler, ok, d.data.real);
2206 Returns the variant as a QVariantList if the variant has type()
2207 \l List or \l StringList; otherwise returns an empty list.
2209 \sa canConvert(), convert()
2211 QVariantList QVariant::toList() const
2213 return qVariantToHelper<QVariantList>(d, List, handler);
2217 static const quint32 qCanConvertMatrix[QVariant::LastCoreType + 1] =
2221 /*Bool*/ 1 << QVariant::Double | 1 << QVariant::Int | 1 << QVariant::UInt
2222 | 1 << QVariant::LongLong | 1 << QVariant::ULongLong | 1 << QVariant::ByteArray
2223 | 1 << QVariant::String | 1 << QVariant::Char,
2225 /*Int*/ 1 << QVariant::UInt | 1 << QVariant::String | 1 << QVariant::Double
2226 | 1 << QVariant::Bool | 1 << QVariant::LongLong | 1 << QVariant::ULongLong
2227 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2229 /*UInt*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::Double
2230 | 1 << QVariant::Bool | 1 << QVariant::LongLong | 1 << QVariant::ULongLong
2231 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2233 /*LLong*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::Double
2234 | 1 << QVariant::Bool | 1 << QVariant::UInt | 1 << QVariant::ULongLong
2235 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2237 /*ULlong*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::Double
2238 | 1 << QVariant::Bool | 1 << QVariant::UInt | 1 << QVariant::LongLong
2239 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2241 /*double*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::ULongLong
2242 | 1 << QVariant::Bool | 1 << QVariant::UInt | 1 << QVariant::LongLong
2243 | 1 << QVariant::ByteArray,
2245 /*QChar*/ 1 << QVariant::Int | 1 << QVariant::UInt | 1 << QVariant::LongLong
2246 | 1 << QVariant::ULongLong,
2250 /*QList*/ 1 << QVariant::StringList,
2252 /*QString*/ 1 << QVariant::StringList | 1 << QVariant::ByteArray | 1 << QVariant::Int
2253 | 1 << QVariant::UInt | 1 << QVariant::Bool | 1 << QVariant::Double
2254 | 1 << QVariant::Date | 1 << QVariant::Time | 1 << QVariant::DateTime
2255 | 1 << QVariant::LongLong | 1 << QVariant::ULongLong | 1 << QVariant::Char
2256 | 1 << QVariant::Url,
2258 /*QStringList*/ 1 << QVariant::List | 1 << QVariant::String,
2260 /*QByteArray*/ 1 << QVariant::String | 1 << QVariant::Int | 1 << QVariant::UInt | 1 << QVariant::Bool
2261 | 1 << QVariant::Double | 1 << QVariant::LongLong | 1 << QVariant::ULongLong,
2265 /*QDate*/ 1 << QVariant::String | 1 << QVariant::DateTime,
2267 /*QTime*/ 1 << QVariant::String | 1 << QVariant::DateTime,
2269 /*QDateTime*/ 1 << QVariant::String | 1 << QVariant::Date,
2271 /*QUrl*/ 1 << QVariant::String,
2275 /*QRect*/ 1 << QVariant::RectF,
2277 /*QRectF*/ 1 << QVariant::Rect,
2279 /*QSize*/ 1 << QVariant::SizeF,
2281 /*QSizeF*/ 1 << QVariant::Size,
2283 /*QLine*/ 1 << QVariant::LineF,
2285 /*QLineF*/ 1 << QVariant::Line,
2287 /*QPoint*/ 1 << QVariant::PointF,
2289 /*QPointF*/ 1 << QVariant::Point,
2299 Returns true if the variant's type can be cast to the requested
2300 type, \a t. Such casting is done automatically when calling the
2301 toInt(), toBool(), ... methods.
2303 The following casts are done automatically:
2306 \header \o Type \o Automatically Cast To
2307 \row \o \l Bool \o \l Char, \l Double, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2308 \row \o \l ByteArray \o \l Double, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2309 \row \o \l Char \o \l Bool, \l Int, \l UInt, \l LongLong, \l ULongLong
2310 \row \o \l Color \o \l String
2311 \row \o \l Date \o \l DateTime, \l String
2312 \row \o \l DateTime \o \l Date, \l String, \l Time
2313 \row \o \l Double \o \l Bool, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2314 \row \o \l Font \o \l String
2315 \row \o \l Int \o \l Bool, \l Char, \l Double, \l LongLong, \l String, \l UInt, \l ULongLong
2316 \row \o \l KeySequence \o \l Int, \l String
2317 \row \o \l List \o \l StringList (if the list's items can be converted to strings)
2318 \row \o \l LongLong \o \l Bool, \l ByteArray, \l Char, \l Double, \l Int, \l String, \l UInt, \l ULongLong
2319 \row \o \l Point \o PointF
2320 \row \o \l Rect \o RectF
2321 \row \o \l String \o \l Bool, \l ByteArray, \l Char, \l Color, \l Date, \l DateTime, \l Double,
2322 \l Font, \l Int, \l KeySequence, \l LongLong, \l StringList, \l Time, \l UInt,
2324 \row \o \l StringList \o \l List, \l String (if the list contains exactly one item)
2325 \row \o \l Time \o \l String
2326 \row \o \l UInt \o \l Bool, \l Char, \l Double, \l Int, \l LongLong, \l String, \l ULongLong
2327 \row \o \l ULongLong \o \l Bool, \l Char, \l Double, \l Int, \l LongLong, \l String, \l UInt
2332 bool QVariant::canConvert(Type t) const
2334 //we can treat floats as double
2335 //the reason for not doing it the "proper" way is that QMetaType::Float's value is 135,
2336 //which can't be handled by qCanConvertMatrix
2337 //In addition QVariant::Type doesn't have a Float value, so we're using QMetaType::Float
2338 const uint currentType = ((d.type == QMetaType::Float) ? QVariant::Double : d.type);
2339 if (uint(t) == uint(QMetaType::Float)) t = QVariant::Double;
2341 if (currentType == uint(t))
2344 if (currentType > QVariant::LastCoreType || t > QVariant::LastCoreType) {
2347 return currentType == QVariant::KeySequence
2348 || currentType == QMetaType::ULong
2349 || currentType == QMetaType::Long
2350 || currentType == QMetaType::UShort
2351 || currentType == QMetaType::UChar
2352 || currentType == QMetaType::Char
2353 || currentType == QMetaType::Short;
2354 case QVariant::Image:
2355 return currentType == QVariant::Pixmap || currentType == QVariant::Bitmap;
2356 case QVariant::Pixmap:
2357 return currentType == QVariant::Image || currentType == QVariant::Bitmap
2358 || currentType == QVariant::Brush;
2359 case QVariant::Bitmap:
2360 return currentType == QVariant::Pixmap || currentType == QVariant::Image;
2361 case QVariant::ByteArray:
2362 return currentType == QVariant::Color;
2363 case QVariant::String:
2364 return currentType == QVariant::KeySequence || currentType == QVariant::Font
2365 || currentType == QVariant::Color;
2366 case QVariant::KeySequence:
2367 return currentType == QVariant::String || currentType == QVariant::Int;
2368 case QVariant::Font:
2369 return currentType == QVariant::String;
2370 case QVariant::Color:
2371 return currentType == QVariant::String || currentType == QVariant::ByteArray
2372 || currentType == QVariant::Brush;
2373 case QVariant::Brush:
2374 return currentType == QVariant::Color || currentType == QVariant::Pixmap;
2375 case QMetaType::Long:
2376 case QMetaType::Char:
2377 case QMetaType::UChar:
2378 case QMetaType::ULong:
2379 case QMetaType::Short:
2380 case QMetaType::UShort:
2381 return qCanConvertMatrix[QVariant::Int] & (1 << currentType) || currentType == QVariant::Int;
2387 if(t == String && currentType == StringList)
2388 return v_cast<QStringList>(&d)->count() == 1;
2390 return qCanConvertMatrix[t] & (1 << currentType);
2394 Casts the variant to the requested type, \a t. If the cast cannot be
2395 done, the variant is cleared. Returns true if the current type of
2396 the variant was successfully cast; otherwise returns false.
2398 \warning For historical reasons, converting a null QVariant results
2399 in a null value of the desired type (e.g., an empty string for
2400 QString) and a result of false.
2402 \sa canConvert(), clear()
2405 bool QVariant::convert(Type t)
2407 if (d.type == uint(t))
2410 QVariant oldValue = *this;
2413 if (!oldValue.canConvert(t))
2417 if (oldValue.isNull())
2421 if (!handler->convert(&oldValue.d, t, data(), &isOk))
2428 \fn bool operator==(const QVariant &v1, const QVariant &v2)
2432 Returns true if \a v1 and \a v2 are equal; otherwise returns false.
2434 \warning This function doesn't support custom types registered
2435 with qRegisterMetaType().
2438 \fn bool operator!=(const QVariant &v1, const QVariant &v2)
2442 Returns false if \a v1 and \a v2 are equal; otherwise returns true.
2444 \warning This function doesn't support custom types registered
2445 with qRegisterMetaType().
2448 /*! \fn bool QVariant::operator==(const QVariant &v) const
2450 Compares this QVariant with \a v and returns true if they are
2451 equal; otherwise returns false.
2453 In the case of custom types, their equalness operators are not called.
2454 Instead the values' addresses are compared.
2458 \fn bool QVariant::operator!=(const QVariant &v) const
2460 Compares this QVariant with \a v and returns true if they are not
2461 equal; otherwise returns false.
2463 \warning This function doesn't support custom types registered
2464 with qRegisterMetaType().
2467 static bool qIsNumericType(uint tp)
2469 return (tp >= QVariant::Bool && tp <= QVariant::Double)
2470 || (tp >= QMetaType::Long && tp <= QMetaType::Float);
2473 static bool qIsFloatingPoint(uint tp)
2475 return tp == QVariant::Double || tp == QMetaType::Float;
2480 bool QVariant::cmp(const QVariant &v) const
2483 if (d.type != v2.d.type) {
2484 if (qIsNumericType(d.type) && qIsNumericType(v.d.type)) {
2485 if (qIsFloatingPoint(d.type) || qIsFloatingPoint(v.d.type))
2486 return qFuzzyCompare(toReal(), v.toReal());
2488 return toLongLong() == v.toLongLong();
2490 if (!v2.canConvert(Type(d.type)) || !v2.convert(Type(d.type)))
2493 return handler->compare(&d, &v2.d);
2499 const void *QVariant::constData() const
2501 return d.is_shared ? d.data.shared->ptr : reinterpret_cast<const void *>(&d.data.ptr);
2505 \fn const void* QVariant::data() const
2511 void* QVariant::data()
2514 return const_cast<void *>(constData());
2519 Returns true if this is a NULL variant, false otherwise.
2521 bool QVariant::isNull() const
2523 return handler->isNull(&d);
2526 #ifndef QT_NO_DEBUG_STREAM
2527 QDebug operator<<(QDebug dbg, const QVariant &v)
2529 #ifndef Q_BROKEN_DEBUG_STREAM
2530 dbg.nospace() << "QVariant(" << v.typeName() << ", ";
2531 QVariant::handler->debugStream(dbg, v);
2532 dbg.nospace() << ')';
2535 qWarning("This compiler doesn't support streaming QVariant to QDebug");
2541 QDebug operator<<(QDebug dbg, const QVariant::Type p)
2543 #ifndef Q_BROKEN_DEBUG_STREAM
2544 dbg.nospace() << "QVariant::" << QVariant::typeToName(p);
2547 qWarning("This compiler doesn't support streaming QVariant::Type to QDebug");
2555 /*! \fn void QVariant::setValue(const T &value)
2557 Stores a copy of \a value. If \c{T} is a type that QVariant
2558 doesn't support, QMetaType is used to store the value. A compile
2559 error will occur if QMetaType doesn't handle the type.
2563 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 4
2565 \sa value(), fromValue(), canConvert()
2568 /*! \fn T QVariant::value() const
2570 Returns the stored value converted to the template type \c{T}.
2571 Call canConvert() to find out whether a type can be converted.
2572 If the value cannot be converted, \l{default-constructed value}
2575 If the type \c{T} is supported by QVariant, this function behaves
2576 exactly as toString(), toInt() etc.
2580 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 5
2582 \sa setValue(), fromValue(), canConvert()
2585 /*! \fn bool QVariant::canConvert() const
2587 Returns true if the variant can be converted to the template type \c{T},
2592 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 6
2597 /*! \fn static QVariant QVariant::fromValue(const T &value)
2599 Returns a QVariant containing a copy of \a value. Behaves
2600 exactly like setValue() otherwise.
2604 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 7
2606 \note If you are working with custom types, you should use
2607 the Q_DECLARE_METATYPE() macro to register your custom type.
2609 \sa setValue(), value()
2613 \fn QVariant qVariantFromValue(const T &value)
2617 Returns a variant containing a copy of the given \a value
2618 with template type \c{T}.
2620 This function is equivalent to QVariant::fromValue(\a value).
2622 \note This function was provided as a workaround for MSVC 6
2623 which did not support member template functions. It is advised
2624 to use the other form in new code.
2626 For example, a QObject pointer can be stored in a variant with the
2629 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 8
2631 \sa QVariant::fromValue()
2634 /*! \fn void qVariantSetValue(QVariant &variant, const T &value)
2638 Sets the contents of the given \a variant to a copy of the
2639 \a value with the specified template type \c{T}.
2641 This function is equivalent to QVariant::setValue(\a value).
2643 \note This function was provided as a workaround for MSVC 6
2644 which did not support member template functions. It is advised
2645 to use the other form in new code.
2647 \sa QVariant::setValue()
2651 \fn T qvariant_cast(const QVariant &value)
2654 Returns the given \a value converted to the template type \c{T}.
2656 This function is equivalent to QVariant::value().
2658 \sa QVariant::value()
2661 /*! \fn T qVariantValue(const QVariant &value)
2665 Returns the given \a value converted to the template type \c{T}.
2667 This function is equivalent to
2668 \l{QVariant::value()}{QVariant::value}<T>(\a value).
2670 \note This function was provided as a workaround for MSVC 6
2671 which did not support member template functions. It is advised
2672 to use the other form in new code.
2674 \sa QVariant::value(), qvariant_cast()
2677 /*! \fn bool qVariantCanConvert(const QVariant &value)
2681 Returns true if the given \a value can be converted to the
2682 template type specified; otherwise returns false.
2684 This function is equivalent to QVariant::canConvert(\a value).
2686 \note This function was provided as a workaround for MSVC 6
2687 which did not support member template functions. It is advised
2688 to use the other form in new code.
2690 \sa QVariant::canConvert()
2694 \typedef QVariantList
2697 Synonym for QList<QVariant>.
2701 \typedef QVariantMap
2704 Synonym for QMap<QString, QVariant>.
2708 \typedef QVariantHash
2712 Synonym for QHash<QString, QVariant>.
2716 \typedef QVariant::DataPtr
2721 \fn DataPtr &QVariant::data_ptr()