source: trunk/src/corelib/kernel/qvariant.cpp@ 448

Last change on this file since 448 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 83.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qvariant.h"
43#include "qbitarray.h"
44#include "qbytearray.h"
45#include "qdatastream.h"
46#include "qdebug.h"
47#include "qmap.h"
48#include "qdatetime.h"
49#include "qlist.h"
50#include "qstring.h"
51#include "qstringlist.h"
52#include "qurl.h"
53#include "qlocale.h"
54#include "private/qvariant_p.h"
55
56#ifndef QT_NO_GEOM_VARIANT
57#include "qsize.h"
58#include "qpoint.h"
59#include "qrect.h"
60#include "qline.h"
61#endif
62
63#include <float.h>
64
65QT_BEGIN_NAMESPACE
66
67#ifndef DBL_DIG
68# define DBL_DIG 10
69#endif
70#ifndef FLT_DIG
71# define FLT_DIG 6
72#endif
73
74
75static const void *constDataHelper(const QVariant::Private &d)
76{
77 switch (d.type) {
78 case QVariant::Int:
79 return &d.data.i;
80 case QVariant::UInt:
81 return &d.data.u;
82 case QVariant::Bool:
83 return &d.data.b;
84 case QVariant::LongLong:
85 return &d.data.ll;
86 case QVariant::ULongLong:
87 return &d.data.ull;
88 case QVariant::Double:
89 return &d.data.d;
90 default:
91 return d.is_shared ? d.data.shared->ptr : reinterpret_cast<const void *>(&d.data.ptr);
92 }
93}
94
95static void construct(QVariant::Private *x, const void *copy)
96{
97 x->is_shared = false;
98
99 switch (x->type) {
100 case QVariant::String:
101 v_construct<QString>(x, copy);
102 break;
103 case QVariant::Char:
104 v_construct<QChar>(x, copy);
105 break;
106 case QVariant::StringList:
107 v_construct<QStringList>(x, copy);
108 break;
109 case QVariant::Map:
110 v_construct<QVariantMap>(x, copy);
111 break;
112 case QVariant::Hash:
113 v_construct<QVariantHash>(x, copy);
114 break;
115 case QVariant::List:
116 v_construct<QVariantList>(x, copy);
117 break;
118 case QVariant::Date:
119 v_construct<QDate>(x, copy);
120 break;
121 case QVariant::Time:
122 v_construct<QTime>(x, copy);
123 break;
124 case QVariant::DateTime:
125 v_construct<QDateTime>(x, copy);
126 break;
127 case QVariant::ByteArray:
128 v_construct<QByteArray>(x, copy);
129 break;
130 case QVariant::BitArray:
131 v_construct<QBitArray>(x, copy);
132 break;
133#ifndef QT_NO_GEOM_VARIANT
134 case QVariant::Size:
135 v_construct<QSize>(x, copy);
136 break;
137 case QVariant::SizeF:
138 v_construct<QSizeF>(x, copy);
139 break;
140 case QVariant::Rect:
141 v_construct<QRect>(x, copy);
142 break;
143 case QVariant::LineF:
144 v_construct<QLineF>(x, copy);
145 break;
146 case QVariant::Line:
147 v_construct<QLine>(x, copy);
148 break;
149 case QVariant::RectF:
150 v_construct<QRectF>(x, copy);
151 break;
152 case QVariant::Point:
153 v_construct<QPoint>(x, copy);
154 break;
155 case QVariant::PointF:
156 v_construct<QPointF>(x, copy);
157 break;
158#endif
159 case QVariant::Url:
160 v_construct<QUrl>(x, copy);
161 break;
162 case QVariant::Locale:
163 v_construct<QLocale>(x, copy);
164 break;
165#ifndef QT_NO_REGEXP
166 case QVariant::RegExp:
167 v_construct<QRegExp>(x, copy);
168 break;
169#endif
170 case QVariant::Int:
171 x->data.i = copy ? *static_cast<const int *>(copy) : 0;
172 break;
173 case QVariant::UInt:
174 x->data.u = copy ? *static_cast<const uint *>(copy) : 0u;
175 break;
176 case QVariant::Bool:
177 x->data.b = copy ? *static_cast<const bool *>(copy) : false;
178 break;
179 case QVariant::Double:
180 x->data.d = copy ? *static_cast<const double*>(copy) : 0.0;
181 break;
182 case QVariant::LongLong:
183 x->data.ll = copy ? *static_cast<const qlonglong *>(copy) : Q_INT64_C(0);
184 break;
185 case QVariant::ULongLong:
186 x->data.ull = copy ? *static_cast<const qulonglong *>(copy) : Q_UINT64_C(0);
187 break;
188 case QVariant::Invalid:
189 case QVariant::UserType:
190 break;
191 default:
192 x->is_shared = true;
193 x->data.shared = new QVariant::PrivateShared(QMetaType::construct(x->type, copy));
194 if (!x->data.shared->ptr)
195 x->type = QVariant::Invalid;
196 break;
197 }
198 x->is_null = !copy;
199}
200
201static void clear(QVariant::Private *d)
202{
203 switch (d->type) {
204 case QVariant::String:
205 v_clear<QString>(d);
206 break;
207 case QVariant::Char:
208 v_clear<QChar>(d);
209 break;
210 case QVariant::StringList:
211 v_clear<QStringList>(d);
212 break;
213 case QVariant::Map:
214 v_clear<QVariantMap>(d);
215 break;
216 case QVariant::Hash:
217 v_clear<QVariantHash>(d);
218 break;
219 case QVariant::List:
220 v_clear<QVariantList>(d);
221 break;
222 case QVariant::Date:
223 v_clear<QDate>(d);
224 break;
225 case QVariant::Time:
226 v_clear<QTime>(d);
227 break;
228 case QVariant::DateTime:
229 v_clear<QDateTime>(d);
230 break;
231 case QVariant::ByteArray:
232 v_clear<QByteArray>(d);
233 break;
234 case QVariant::BitArray:
235 v_clear<QBitArray>(d);
236 break;
237#ifndef QT_NO_GEOM_VARIANT
238 case QVariant::Point:
239 v_clear<QPoint>(d);
240 break;
241 case QVariant::PointF:
242 v_clear<QPointF>(d);
243 break;
244 case QVariant::Size:
245 v_clear<QSize>(d);
246 break;
247 case QVariant::SizeF:
248 v_clear<QSizeF>(d);
249 break;
250 case QVariant::Rect:
251 v_clear<QRect>(d);
252 break;
253 case QVariant::LineF:
254 v_clear<QLineF>(d);
255 break;
256 case QVariant::Line:
257 v_clear<QLine>(d);
258 break;
259 case QVariant::RectF:
260 v_clear<QRectF>(d);
261 break;
262#endif
263 case QVariant::Url:
264 v_clear<QUrl>(d);
265 break;
266 case QVariant::Locale:
267 v_clear<QLocale>(d);
268 break;
269#ifndef QT_NO_REGEXP
270 case QVariant::RegExp:
271 v_clear<QRegExp>(d);
272 break;
273#endif
274 case QVariant::LongLong:
275 case QVariant::ULongLong:
276 case QVariant::Double:
277 break;
278 case QVariant::Invalid:
279 case QVariant::UserType:
280 case QVariant::Int:
281 case QVariant::UInt:
282 case QVariant::Bool:
283 break;
284 default:
285 QMetaType::destroy(d->type, d->data.shared->ptr);
286 delete d->data.shared;
287 break;
288 }
289
290 d->type = QVariant::Invalid;
291 d->is_null = true;
292 d->is_shared = false;
293}
294
295static bool isNull(const QVariant::Private *d)
296{
297 switch(d->type) {
298 case QVariant::String:
299 return v_cast<QString>(d)->isNull();
300 case QVariant::Char:
301 return v_cast<QChar>(d)->isNull();
302 case QVariant::Date:
303 return v_cast<QDate>(d)->isNull();
304 case QVariant::Time:
305 return v_cast<QTime>(d)->isNull();
306 case QVariant::DateTime:
307 return v_cast<QDateTime>(d)->isNull();
308 case QVariant::ByteArray:
309 return v_cast<QByteArray>(d)->isNull();
310 case QVariant::BitArray:
311 return v_cast<QBitArray>(d)->isNull();
312#ifndef QT_NO_GEOM_VARIANT
313 case QVariant::Size:
314 return v_cast<QSize>(d)->isNull();
315 case QVariant::SizeF:
316 return v_cast<QSizeF>(d)->isNull();
317 case QVariant::Rect:
318 return v_cast<QRect>(d)->isNull();
319 case QVariant::Line:
320 return v_cast<QLine>(d)->isNull();
321 case QVariant::LineF:
322 return v_cast<QLineF>(d)->isNull();
323 case QVariant::RectF:
324 return v_cast<QRectF>(d)->isNull();
325 case QVariant::Point:
326 return v_cast<QPoint>(d)->isNull();
327 case QVariant::PointF:
328 return v_cast<QPointF>(d)->isNull();
329#endif
330 case QVariant::Url:
331 case QVariant::Locale:
332 case QVariant::RegExp:
333 case QVariant::StringList:
334 case QVariant::Map:
335 case QVariant::Hash:
336 case QVariant::List:
337 case QVariant::Invalid:
338 case QVariant::UserType:
339 case QVariant::Int:
340 case QVariant::UInt:
341 case QVariant::LongLong:
342 case QVariant::ULongLong:
343 case QVariant::Bool:
344 case QVariant::Double:
345 break;
346 }
347 return d->is_null;
348}
349
350/*
351 \internal
352 \since 4.4
353
354 We cannot use v_cast() for QMetaType's numeric types because they're smaller than QVariant::Private::Data,
355 which in turns makes v_cast() believe the value is stored in d->data.c. But
356 it's not, since we're a QMetaType type.
357 */
358template<typename T>
359inline bool compareNumericMetaType(const QVariant::Private *const a, const QVariant::Private *const b)
360{
361 return *static_cast<const T *>(a->data.shared->ptr) == *static_cast<const T *>(b->data.shared->ptr);
362}
363
364/*!
365 \internal
366
367 Compares \a a to \a b. The caller guarantees that \a a and \a b
368 are of the same type.
369 */
370static bool compare(const QVariant::Private *a, const QVariant::Private *b)
371{
372 switch(a->type) {
373 case QVariant::List:
374 return *v_cast<QVariantList>(a) == *v_cast<QVariantList>(b);
375 case QVariant::Map: {
376 const QVariantMap *m1 = v_cast<QVariantMap>(a);
377 const QVariantMap *m2 = v_cast<QVariantMap>(b);
378 if (m1->count() != m2->count())
379 return false;
380 QVariantMap::ConstIterator it = m1->constBegin();
381 QVariantMap::ConstIterator it2 = m2->constBegin();
382 while (it != m1->constEnd()) {
383 if (*it != *it2 || it.key() != it2.key())
384 return false;
385 ++it;
386 ++it2;
387 }
388 return true;
389 }
390 case QVariant::Hash:
391 return *v_cast<QVariantHash>(a) == *v_cast<QVariantHash>(b);
392 case QVariant::String:
393 return *v_cast<QString>(a) == *v_cast<QString>(b);
394 case QVariant::Char:
395 return *v_cast<QChar>(a) == *v_cast<QChar>(b);
396 case QVariant::StringList:
397 return *v_cast<QStringList>(a) == *v_cast<QStringList>(b);
398#ifndef QT_NO_GEOM_VARIANT
399 case QVariant::Size:
400 return *v_cast<QSize>(a) == *v_cast<QSize>(b);
401 case QVariant::SizeF:
402 return *v_cast<QSizeF>(a) == *v_cast<QSizeF>(b);
403 case QVariant::Rect:
404 return *v_cast<QRect>(a) == *v_cast<QRect>(b);
405 case QVariant::Line:
406 return *v_cast<QLine>(a) == *v_cast<QLine>(b);
407 case QVariant::LineF:
408 return *v_cast<QLineF>(a) == *v_cast<QLineF>(b);
409 case QVariant::RectF:
410 return *v_cast<QRectF>(a) == *v_cast<QRectF>(b);
411 case QVariant::Point:
412 return *v_cast<QPoint>(a) == *v_cast<QPoint>(b);
413 case QVariant::PointF:
414 return *v_cast<QPointF>(a) == *v_cast<QPointF>(b);
415#endif
416 case QVariant::Url:
417 return *v_cast<QUrl>(a) == *v_cast<QUrl>(b);
418 case QVariant::Locale:
419 return *v_cast<QLocale>(a) == *v_cast<QLocale>(b);
420#ifndef QT_NO_REGEXP
421 case QVariant::RegExp:
422 return *v_cast<QRegExp>(a) == *v_cast<QRegExp>(b);
423#endif
424 case QVariant::Int:
425 return a->data.i == b->data.i;
426 case QVariant::UInt:
427 return a->data.u == b->data.u;
428 case QVariant::LongLong:
429 return a->data.ll == b->data.ll;
430 case QVariant::ULongLong:
431 return a->data.ull == b->data.ull;
432 case QVariant::Bool:
433 return a->data.b == b->data.b;
434 case QVariant::Double:
435 return a->data.d == b->data.d;
436 case QVariant::Date:
437 return *v_cast<QDate>(a) == *v_cast<QDate>(b);
438 case QVariant::Time:
439 return *v_cast<QTime>(a) == *v_cast<QTime>(b);
440 case QVariant::DateTime:
441 return *v_cast<QDateTime>(a) == *v_cast<QDateTime>(b);
442 case QVariant::ByteArray:
443 return *v_cast<QByteArray>(a) == *v_cast<QByteArray>(b);
444 case QVariant::BitArray:
445 return *v_cast<QBitArray>(a) == *v_cast<QBitArray>(b);
446 case QVariant::Invalid:
447 return true;
448 case QMetaType::Long:
449 return compareNumericMetaType<long>(a, b);
450 case QMetaType::ULong:
451 return compareNumericMetaType<ulong>(a, b);
452 case QMetaType::Short:
453 return compareNumericMetaType<short>(a, b);
454 case QMetaType::UShort:
455 return compareNumericMetaType<ushort>(a, b);
456 case QMetaType::UChar:
457 return compareNumericMetaType<uchar>(a, b);
458 case QMetaType::Char:
459 return compareNumericMetaType<char>(a, b);
460 default:
461 break;
462 }
463 if (!QMetaType::isRegistered(a->type))
464 qFatal("QVariant::compare: type %d unknown to QVariant.", a->type);
465
466 /* The reason we cannot place this test in a case branch above for the types
467 * QMetaType::VoidStar, QMetaType::QObjectStar and so forth, is that it wouldn't include
468 * user defined pointer types. */
469 const char *const typeName = QMetaType::typeName(a->type);
470 if (typeName[qstrlen(typeName) - 1] == '*')
471 return *static_cast<void **>(a->data.shared->ptr) ==
472 *static_cast<void **>(b->data.shared->ptr);
473
474 return a->data.shared->ptr == b->data.shared->ptr;
475}
476
477/*!
478 \internal
479 */
480static qlonglong qMetaTypeNumber(const QVariant::Private *d)
481{
482 switch (d->type) {
483 case QMetaType::Int:
484 return d->data.i;
485 case QMetaType::LongLong:
486 return d->data.ll;
487 case QMetaType::Char:
488 return qlonglong(*static_cast<signed char *>(d->data.shared->ptr));
489 case QMetaType::Short:
490 return qlonglong(*static_cast<short *>(d->data.shared->ptr));
491 case QMetaType::Long:
492 return qlonglong(*static_cast<long *>(d->data.shared->ptr));
493 case QMetaType::Float:
494 return qRound64(*static_cast<float *>(d->data.shared->ptr));
495 case QVariant::Double:
496 return qRound64(d->data.d);
497 }
498 Q_ASSERT(false);
499 return 0;
500}
501
502static qulonglong qMetaTypeUNumber(const QVariant::Private *d)
503{
504 switch (d->type) {
505 case QVariant::UInt:
506 return d->data.u;
507 case QVariant::ULongLong:
508 return d->data.ull;
509 case QMetaType::UChar:
510 return qulonglong(*static_cast<unsigned char *>(d->data.shared->ptr));
511 case QMetaType::UShort:
512 return qulonglong(*static_cast<ushort *>(d->data.shared->ptr));
513 case QMetaType::ULong:
514 return qulonglong(*static_cast<ulong *>(d->data.shared->ptr));
515 }
516 Q_ASSERT(false);
517 return 0;
518}
519
520static qlonglong qConvertToNumber(const QVariant::Private *d, bool *ok)
521{
522 *ok = true;
523
524 switch (uint(d->type)) {
525 case QVariant::String:
526 return v_cast<QString>(d)->toLongLong(ok);
527 case QVariant::Char:
528 return v_cast<QChar>(d)->unicode();
529 case QVariant::ByteArray:
530 return v_cast<QByteArray>(d)->toLongLong(ok);
531 case QVariant::Bool:
532 return qlonglong(d->data.b);
533 case QVariant::Double:
534 case QVariant::Int:
535 case QMetaType::Char:
536 case QMetaType::Short:
537 case QMetaType::Long:
538 case QMetaType::Float:
539 case QMetaType::LongLong:
540 return qMetaTypeNumber(d);
541 case QVariant::ULongLong:
542 case QVariant::UInt:
543 case QMetaType::UChar:
544 case QMetaType::UShort:
545 case QMetaType::ULong:
546 return qlonglong(qMetaTypeUNumber(d));
547 }
548
549 *ok = false;
550 return Q_INT64_C(0);
551}
552
553static qulonglong qConvertToUnsignedNumber(const QVariant::Private *d, bool *ok)
554{
555 *ok = true;
556
557 switch (uint(d->type)) {
558 case QVariant::String:
559 return v_cast<QString>(d)->toULongLong(ok);
560 case QVariant::Char:
561 return v_cast<QChar>(d)->unicode();
562 case QVariant::ByteArray:
563 return v_cast<QByteArray>(d)->toULongLong(ok);
564 case QVariant::Bool:
565 return qulonglong(d->data.b);
566 case QVariant::Double:
567 case QVariant::Int:
568 case QMetaType::Char:
569 case QMetaType::Short:
570 case QMetaType::Long:
571 case QMetaType::Float:
572 case QMetaType::LongLong:
573 return qulonglong(qMetaTypeNumber(d));
574 case QVariant::ULongLong:
575 case QVariant::UInt:
576 case QMetaType::UChar:
577 case QMetaType::UShort:
578 case QMetaType::ULong:
579 return qMetaTypeUNumber(d);
580 }
581
582 *ok = false;
583 return Q_UINT64_C(0);
584}
585
586template<typename TInput, typename LiteralWrapper>
587inline bool qt_convertToBool(const QVariant::Private *const d)
588{
589 TInput str = v_cast<TInput>(d)->toLower();
590 return !(str == LiteralWrapper("0") || str == LiteralWrapper("false") || str.isEmpty());
591}
592
593/*!
594 \internal
595
596 Converts \a d to type \a t, which is placed in \a result.
597 */
598static bool convert(const QVariant::Private *d, QVariant::Type t, void *result, bool *ok)
599{
600 Q_ASSERT(d->type != uint(t));
601 Q_ASSERT(result);
602
603 bool dummy;
604 if (!ok)
605 ok = &dummy;
606
607 switch (uint(t)) {
608 case QVariant::String: {
609 QString *str = static_cast<QString *>(result);
610 switch (d->type) {
611 case QVariant::Char:
612 *str = QString(*v_cast<QChar>(d));
613 break;
614 case QMetaType::Char:
615 case QMetaType::UChar:
616 *str = QChar::fromAscii(*static_cast<char *>(d->data.shared->ptr));
617 break;
618 case QMetaType::Short:
619 case QMetaType::Long:
620 case QVariant::Int:
621 case QVariant::LongLong:
622 *str = QString::number(qMetaTypeNumber(d));
623 break;
624 case QVariant::UInt:
625 case QVariant::ULongLong:
626 case QMetaType::UShort:
627 case QMetaType::ULong:
628 *str = QString::number(qMetaTypeUNumber(d));
629 break;
630 case QMetaType::Float:
631 *str = QString::number(*static_cast<float *>(d->data.shared->ptr), 'g', FLT_DIG);
632 break;
633 case QVariant::Double:
634 *str = QString::number(d->data.d, 'g', DBL_DIG);
635 break;
636#if !defined(QT_NO_DATESTRING)
637 case QVariant::Date:
638 *str = v_cast<QDate>(d)->toString(Qt::ISODate);
639 break;
640 case QVariant::Time:
641 *str = v_cast<QTime>(d)->toString(Qt::ISODate);
642 break;
643 case QVariant::DateTime:
644 *str = v_cast<QDateTime>(d)->toString(Qt::ISODate);
645 break;
646#endif
647 case QVariant::Bool:
648 *str = QLatin1String(d->data.b ? "true" : "false");
649 break;
650 case QVariant::ByteArray:
651 *str = QString::fromAscii(v_cast<QByteArray>(d)->constData());
652 break;
653 case QVariant::StringList:
654 if (v_cast<QStringList>(d)->count() == 1)
655 *str = v_cast<QStringList>(d)->at(0);
656 break;
657 default:
658 return false;
659 }
660 break;
661 }
662 case QVariant::Char: {
663 QChar *c = static_cast<QChar *>(result);
664 switch (d->type) {
665 case QVariant::Int:
666 case QVariant::LongLong:
667 case QMetaType::Char:
668 case QMetaType::Short:
669 case QMetaType::Long:
670 case QMetaType::Float:
671 *c = QChar(ushort(qMetaTypeNumber(d)));
672 break;
673 case QVariant::UInt:
674 case QVariant::ULongLong:
675 case QMetaType::UChar:
676 case QMetaType::UShort:
677 case QMetaType::ULong:
678 *c = QChar(ushort(qMetaTypeUNumber(d)));
679 break;
680 default:
681 return false;
682 }
683 break;
684 }
685#ifndef QT_NO_GEOM_VARIANT
686 case QVariant::Size: {
687 QSize *s = static_cast<QSize *>(result);
688 switch (d->type) {
689 case QVariant::SizeF:
690 *s = v_cast<QSizeF>(d)->toSize();
691 break;
692 default:
693 return false;
694 }
695 break;
696 }
697
698 case QVariant::SizeF: {
699 QSizeF *s = static_cast<QSizeF *>(result);
700 switch (d->type) {
701 case QVariant::Size:
702 *s = QSizeF(*(v_cast<QSize>(d)));
703 break;
704 default:
705 return false;
706 }
707 break;
708 }
709
710 case QVariant::Line: {
711 QLine *s = static_cast<QLine *>(result);
712 switch (d->type) {
713 case QVariant::LineF:
714 *s = v_cast<QLineF>(d)->toLine();
715 break;
716 default:
717 return false;
718 }
719 break;
720 }
721
722 case QVariant::LineF: {
723 QLineF *s = static_cast<QLineF *>(result);
724 switch (d->type) {
725 case QVariant::Line:
726 *s = QLineF(*(v_cast<QLine>(d)));
727 break;
728 default:
729 return false;
730 }
731 break;
732 }
733#endif
734 case QVariant::StringList:
735 if (d->type == QVariant::List) {
736 QStringList *slst = static_cast<QStringList *>(result);
737 const QVariantList *list = v_cast<QVariantList >(d);
738 for (int i = 0; i < list->size(); ++i)
739 slst->append(list->at(i).toString());
740 } else if (d->type == QVariant::String) {
741 QStringList *slst = static_cast<QStringList *>(result);
742 *slst = QStringList(*v_cast<QString>(d));
743 } else {
744 return false;
745 }
746 break;
747 case QVariant::Date: {
748 QDate *dt = static_cast<QDate *>(result);
749 if (d->type == QVariant::DateTime)
750 *dt = v_cast<QDateTime>(d)->date();
751#ifndef QT_NO_DATESTRING
752 else if (d->type == QVariant::String)
753 *dt = QDate::fromString(*v_cast<QString>(d), Qt::ISODate);
754#endif
755 else
756 return false;
757
758 return dt->isValid();
759 }
760 case QVariant::Time: {
761 QTime *t = static_cast<QTime *>(result);
762 switch (d->type) {
763 case QVariant::DateTime:
764 *t = v_cast<QDateTime>(d)->time();
765 break;
766#ifndef QT_NO_DATESTRING
767 case QVariant::String:
768 *t = QTime::fromString(*v_cast<QString>(d), Qt::ISODate);
769 break;
770#endif
771 default:
772 return false;
773 }
774 return t->isValid();
775 }
776 case QVariant::DateTime: {
777 QDateTime *dt = static_cast<QDateTime *>(result);
778 switch (d->type) {
779#ifndef QT_NO_DATESTRING
780 case QVariant::String:
781 *dt = QDateTime::fromString(*v_cast<QString>(d), Qt::ISODate);
782 break;
783#endif
784 case QVariant::Date:
785 *dt = QDateTime(*v_cast<QDate>(d));
786 break;
787 default:
788 return false;
789 }
790 return dt->isValid();
791 }
792 case QVariant::ByteArray: {
793 QByteArray *ba = static_cast<QByteArray *>(result);
794 switch (d->type) {
795 case QVariant::String:
796 *ba = v_cast<QString>(d)->toAscii();
797 break;
798 case QVariant::Double:
799 *ba = QByteArray::number(d->data.d, 'g', DBL_DIG);
800 break;
801 case QMetaType::Float:
802 *ba = QByteArray::number(*static_cast<float *>(d->data.shared->ptr), 'g', FLT_DIG);
803 break;
804 case QMetaType::Char:
805 case QMetaType::UChar:
806 *ba = QByteArray(1, *static_cast<char *>(d->data.shared->ptr));
807 break;
808 case QVariant::Int:
809 case QVariant::LongLong:
810 case QMetaType::Short:
811 case QMetaType::Long:
812 *ba = QByteArray::number(qMetaTypeNumber(d));
813 break;
814 case QVariant::UInt:
815 case QVariant::ULongLong:
816 case QMetaType::UShort:
817 case QMetaType::ULong:
818 *ba = QByteArray::number(qMetaTypeUNumber(d));
819 break;
820 case QVariant::Bool:
821 *ba = QByteArray(d->data.b ? "true" : "false");
822 break;
823 default:
824 return false;
825 }
826 }
827 break;
828 case QMetaType::Short:
829 *static_cast<short *>(result) = short(qConvertToNumber(d, ok));
830 return *ok;
831 case QMetaType::Long:
832 *static_cast<long *>(result) = long(qConvertToNumber(d, ok));
833 return *ok;
834 case QMetaType::UShort:
835 *static_cast<ushort *>(result) = ushort(qConvertToUnsignedNumber(d, ok));
836 return *ok;
837 case QMetaType::ULong:
838 *static_cast<ulong *>(result) = ulong(qConvertToUnsignedNumber(d, ok));
839 return *ok;
840 case QVariant::Int:
841 *static_cast<int *>(result) = int(qConvertToNumber(d, ok));
842 return *ok;
843 case QVariant::UInt:
844 *static_cast<uint *>(result) = uint(qConvertToUnsignedNumber(d, ok));
845 return *ok;
846 case QVariant::LongLong:
847 *static_cast<qlonglong *>(result) = qConvertToNumber(d, ok);
848 return *ok;
849 case QVariant::ULongLong: {
850 *static_cast<qulonglong *>(result) = qConvertToUnsignedNumber(d, ok);
851 return *ok;
852 }
853 case QMetaType::UChar: {
854 *static_cast<uchar *>(result) = qConvertToUnsignedNumber(d, ok);
855 return *ok;
856 }
857 case QVariant::Bool: {
858 bool *b = static_cast<bool *>(result);
859 switch(d->type) {
860 case QVariant::ByteArray:
861 *b = qt_convertToBool<QByteArray, QByteArray>(d);
862 break;
863 case QVariant::String:
864 *b = qt_convertToBool<QString, QLatin1String>(d);
865 break;
866 case QVariant::Char:
867 *b = !v_cast<QChar>(d)->isNull();
868 break;
869 case QVariant::Double:
870 case QVariant::Int:
871 case QVariant::LongLong:
872 case QMetaType::Char:
873 case QMetaType::Short:
874 case QMetaType::Long:
875 case QMetaType::Float:
876 *b = qMetaTypeNumber(d) != Q_INT64_C(0);
877 break;
878 case QVariant::UInt:
879 case QVariant::ULongLong:
880 case QMetaType::UChar:
881 case QMetaType::UShort:
882 case QMetaType::ULong:
883 *b = qMetaTypeUNumber(d) != Q_UINT64_C(0);
884 break;
885 default:
886 *b = false;
887 return false;
888 }
889 break;
890 }
891 case QVariant::Double: {
892 double *f = static_cast<double *>(result);
893 switch (d->type) {
894 case QVariant::String:
895 *f = v_cast<QString>(d)->toDouble(ok);
896 break;
897 case QVariant::ByteArray:
898 *f = v_cast<QByteArray>(d)->toDouble(ok);
899 break;
900 case QVariant::Bool:
901 *f = double(d->data.b);
902 break;
903 case QMetaType::Float:
904 *f = *static_cast<float *>(d->data.shared->ptr);
905 break;
906 case QVariant::LongLong:
907 case QVariant::Int:
908 case QMetaType::Char:
909 case QMetaType::Short:
910 case QMetaType::Long:
911 *f = double(qMetaTypeNumber(d));
912 break;
913 case QVariant::UInt:
914 case QVariant::ULongLong:
915 case QMetaType::UChar:
916 case QMetaType::UShort:
917 case QMetaType::ULong:
918#if defined(Q_CC_MSVC) && !defined(Q_CC_MSVC_NET)
919 *f = (double)(qlonglong)qMetaTypeUNumber(d);
920#else
921 *f = double(qMetaTypeUNumber(d));
922#endif
923 break;
924 default:
925 *f = 0.0;
926 return false;
927 }
928 break;
929 }
930 case QMetaType::Float: {
931 float *f = static_cast<float *>(result);
932 switch (d->type) {
933 case QVariant::String:
934 *f = float(v_cast<QString>(d)->toDouble(ok));
935 break;
936 case QVariant::ByteArray:
937 *f = float(v_cast<QByteArray>(d)->toDouble(ok));
938 break;
939 case QVariant::Bool:
940 *f = float(d->data.b);
941 break;
942 case QVariant::Double:
943 *f = float(d->data.d);
944 break;
945 case QVariant::LongLong:
946 case QVariant::Int:
947 case QMetaType::Char:
948 case QMetaType::Short:
949 case QMetaType::Long:
950 *f = float(qMetaTypeNumber(d));
951 break;
952 case QVariant::UInt:
953 case QVariant::ULongLong:
954 case QMetaType::UChar:
955 case QMetaType::UShort:
956 case QMetaType::ULong:
957#if defined(Q_CC_MSVC) && !defined(Q_CC_MSVC_NET)
958 *f = (float)(qlonglong)qMetaTypeUNumber(d);
959#else
960 *f = float(qMetaTypeUNumber(d));
961#endif
962 break;
963 default:
964 *f = 0.0f;
965 return false;
966 }
967 break;
968 }
969 case QVariant::List:
970 if (d->type == QVariant::StringList) {
971 QVariantList *lst = static_cast<QVariantList *>(result);
972 const QStringList *slist = v_cast<QStringList>(d);
973 for (int i = 0; i < slist->size(); ++i)
974 lst->append(QVariant(slist->at(i)));
975 } else if (qstrcmp(QMetaType::typeName(d->type), "QList<QVariant>") == 0) {
976 *static_cast<QVariantList *>(result) =
977 *static_cast<QList<QVariant> *>(d->data.shared->ptr);
978 } else {
979 return false;
980 }
981 break;
982 case QVariant::Map:
983 if (qstrcmp(QMetaType::typeName(d->type), "QMap<QString, QVariant>") == 0) {
984 *static_cast<QVariantMap *>(result) =
985 *static_cast<QMap<QString, QVariant> *>(d->data.shared->ptr);
986 } else {
987 return false;
988 }
989 break;
990 case QVariant::Hash:
991 if (qstrcmp(QMetaType::typeName(d->type), "QHash<QString, QVariant>") == 0) {
992 *static_cast<QVariantHash *>(result) =
993 *static_cast<QHash<QString, QVariant> *>(d->data.shared->ptr);
994 } else {
995 return false;
996 }
997 break;
998#ifndef QT_NO_GEOM_VARIANT
999 case QVariant::Rect:
1000 if (d->type == QVariant::RectF)
1001 *static_cast<QRect *>(result) = (v_cast<QRectF>(d))->toRect();
1002 else
1003 return false;
1004 break;
1005 case QVariant::RectF:
1006 if (d->type == QVariant::Rect)
1007 *static_cast<QRectF *>(result) = *v_cast<QRect>(d);
1008 else
1009 return false;
1010 break;
1011 case QVariant::PointF:
1012 if (d->type == QVariant::Point)
1013 *static_cast<QPointF *>(result) = *v_cast<QPoint>(d);
1014 else
1015 return false;
1016 break;
1017 case QVariant::Point:
1018 if (d->type == QVariant::PointF)
1019 *static_cast<QPoint *>(result) = (v_cast<QPointF>(d))->toPoint();
1020 else
1021 return false;
1022 break;
1023 case QMetaType::Char:
1024 {
1025 *static_cast<qint8 *>(result) = qint8(qConvertToNumber(d, ok));
1026 return *ok;
1027 }
1028#endif
1029 default:
1030 return false;
1031 }
1032 return true;
1033}
1034
1035#if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)
1036static void streamDebug(QDebug dbg, const QVariant &v)
1037{
1038 switch (v.type()) {
1039 case QVariant::Int:
1040 dbg.nospace() << v.toInt();
1041 break;
1042 case QVariant::UInt:
1043 dbg.nospace() << v.toUInt();
1044 break;
1045 case QVariant::LongLong:
1046 dbg.nospace() << v.toLongLong();
1047 break;
1048 case QVariant::ULongLong:
1049 dbg.nospace() << v.toULongLong();
1050 break;
1051 case QVariant::Double:
1052 dbg.nospace() << v.toDouble();
1053 break;
1054 case QVariant::Bool:
1055 dbg.nospace() << v.toBool();
1056 break;
1057 case QVariant::String:
1058 dbg.nospace() << v.toString();
1059 break;
1060 case QVariant::Char:
1061 dbg.nospace() << v.toChar();
1062 break;
1063 case QVariant::StringList:
1064 dbg.nospace() << v.toStringList();
1065 break;
1066 case QVariant::Map:
1067 dbg.nospace() << v.toMap();
1068 break;
1069 case QVariant::Hash:
1070 dbg.nospace() << v.toHash();
1071 break;
1072 case QVariant::List:
1073 dbg.nospace() << v.toList();
1074 break;
1075 case QVariant::Date:
1076 dbg.nospace() << v.toDate();
1077 break;
1078 case QVariant::Time:
1079 dbg.nospace() << v.toTime();
1080 break;
1081 case QVariant::DateTime:
1082 dbg.nospace() << v.toDateTime();
1083 break;
1084 case QVariant::ByteArray:
1085 dbg.nospace() << v.toByteArray();
1086 break;
1087 case QVariant::Url:
1088 dbg.nospace() << v.toUrl();
1089 break;
1090#ifndef QT_NO_GEOM_VARIANT
1091 case QVariant::Point:
1092 dbg.nospace() << v.toPoint();
1093 break;
1094 case QVariant::PointF:
1095 dbg.nospace() << v.toPointF();
1096 break;
1097 case QVariant::Rect:
1098 dbg.nospace() << v.toRect();
1099 break;
1100 case QVariant::Size:
1101 dbg.nospace() << v.toSize();
1102 break;
1103 case QVariant::SizeF:
1104 dbg.nospace() << v.toSizeF();
1105 break;
1106 case QVariant::Line:
1107 dbg.nospace() << v.toLine();
1108 break;
1109 case QVariant::LineF:
1110 dbg.nospace() << v.toLineF();
1111 break;
1112 case QVariant::RectF:
1113 dbg.nospace() << v.toRectF();
1114 break;
1115#endif
1116 case QVariant::BitArray:
1117 //dbg.nospace() << v.toBitArray();
1118 break;
1119 default:
1120 break;
1121 }
1122}
1123#endif
1124
1125const QVariant::Handler qt_kernel_variant_handler = {
1126 construct,
1127 clear,
1128 isNull,
1129#ifndef QT_NO_DATASTREAM
1130 0,
1131 0,
1132#endif
1133 compare,
1134 convert,
1135 0,
1136#if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)
1137 streamDebug
1138#else
1139 0
1140#endif
1141};
1142
1143Q_CORE_EXPORT const QVariant::Handler *qcoreVariantHandler()
1144{
1145 return &qt_kernel_variant_handler;
1146}
1147
1148
1149const QVariant::Handler *QVariant::handler = &qt_kernel_variant_handler;
1150
1151/*!
1152 \class QVariant
1153 \brief The QVariant class acts like a union for the most common Qt data types.
1154
1155 \ingroup objectmodel
1156 \ingroup misc
1157 \ingroup shared
1158 \mainclass
1159
1160 Because C++ forbids unions from including types that have
1161 non-default constructors or destructors, most interesting Qt
1162 classes cannot be used in unions. Without QVariant, this would be
1163 a problem for QObject::property() and for database work, etc.
1164
1165 A QVariant object holds a single value of a single type() at a
1166 time. (Some type()s are multi-valued, for example a string list.)
1167 You can find out what type, T, the variant holds, convert it to a
1168 different type using convert(), get its value using one of the
1169 toT() functions (e.g., toSize()) and check whether the type can
1170 be converted to a particular type using canConvert().
1171
1172 The methods named toT() (e.g., toInt(), toString()) are const. If
1173 you ask for the stored type, they return a copy of the stored
1174 object. If you ask for a type that can be generated from the
1175 stored type, toT() copies and converts and leaves the object
1176 itself unchanged. If you ask for a type that cannot be generated
1177 from the stored type, the result depends on the type; see the
1178 function documentation for details.
1179
1180 Here is some example code to demonstrate the use of QVariant:
1181
1182 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 0
1183
1184 You can even store QList<QVariant> and QMap<QString, QVariant>
1185 values in a variant, so you can easily construct arbitrarily
1186 complex data structures of arbitrary types. This is very powerful
1187 and versatile, but may prove less memory and speed efficient than
1188 storing specific types in standard data structures.
1189
1190 QVariant also supports the notion of null values, where you have
1191 a defined type with no value set.
1192
1193 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 1
1194
1195 QVariant can be extended to support other types than those
1196 mentioned in the \l Type enum. See the \l QMetaType documentation
1197 for details.
1198
1199 \section1 A Note on GUI Types
1200
1201 Because QVariant is part of the QtCore library, it cannot provide
1202 conversion functions to data types defined in QtGui, such as
1203 QColor, QImage, and QPixmap. In other words, there is no \c
1204 toColor() function. Instead, you can use the QVariant::value() or
1205 the qVariantValue() template function. For example:
1206
1207 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 2
1208
1209 The inverse conversion (e.g., from QColor to QVariant) is
1210 automatic for all data types supported by QVariant, including
1211 GUI-related types:
1212
1213 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 3
1214
1215 \section1 Using canConvert() and convert() Consecutively
1216
1217 When using canConvert() and convert() consecutively, it is possible for
1218 canConvert() to return true, but convert() to return false. This
1219 is typically because canConvert() only reports the general ability of
1220 QVariant to convert between types given suitable data; it is still
1221 possible to supply data which cannot actually be converted.
1222
1223 For example, canConvert() would return true when called on a variant
1224 containing a string because, in principle, QVariant is able to convert
1225 strings of numbers to integers.
1226 However, if the string contains non-numeric characters, it cannot be
1227 converted to an integer, and any attempt to convert it will fail.
1228 Hence, it is important to have both functions return true for a
1229 successful conversion.
1230
1231 \sa QMetaType
1232*/
1233
1234/*!
1235 \enum QVariant::Type
1236
1237 This enum type defines the types of variable that a QVariant can
1238 contain.
1239
1240 \value Invalid no type
1241 \value BitArray a QBitArray
1242 \value Bitmap a QBitmap
1243 \value Bool a bool
1244 \value Brush a QBrush
1245 \value ByteArray a QByteArray
1246 \value Char a QChar
1247 \value Color a QColor
1248 \value Cursor a QCursor
1249 \value Date a QDate
1250 \value DateTime a QDateTime
1251 \value Double a double
1252 \value Font a QFont
1253 \value Hash a QVariantHash
1254 \value Icon a QIcon
1255 \value Image a QImage
1256 \value Int an int
1257 \value KeySequence a QKeySequence
1258 \value Line a QLine
1259 \value LineF a QLineF
1260 \value List a QVariantList
1261 \value Locale a QLocale
1262 \value LongLong a \l qlonglong
1263 \value Map a QVariantMap
1264 \value Matrix a QMatrix
1265 \value Transform a QTransform
1266 \value Palette a QPalette
1267 \value Pen a QPen
1268 \value Pixmap a QPixmap
1269 \value Point a QPoint
1270 \value PointArray a QPointArray
1271 \value PointF a QPointF
1272 \value Polygon a QPolygon
1273 \value Rect a QRect
1274 \value RectF a QRectF
1275 \value RegExp a QRegExp
1276 \value Region a QRegion
1277 \value Size a QSize
1278 \value SizeF a QSizeF
1279 \value SizePolicy a QSizePolicy
1280 \value String a QString
1281 \value StringList a QStringList
1282 \value TextFormat a QTextFormat
1283 \value TextLength a QTextLength
1284 \value Time a QTime
1285 \value UInt a \l uint
1286 \value ULongLong a \l qulonglong
1287 \value Url a QUrl
1288
1289 \value UserType Base value for user-defined types.
1290
1291 \omitvalue CString
1292 \omitvalue ColorGroup
1293 \omitvalue IconSet
1294 \omitvalue LastGuiType
1295 \omitvalue LastCoreType
1296 \omitvalue LastType
1297*/
1298
1299/*!
1300 \fn QVariant::QVariant()
1301
1302 Constructs an invalid variant.
1303*/
1304
1305
1306/*!
1307 \fn QVariant::QVariant(int typeOrUserType, const void *copy)
1308
1309 Constructs variant of type \a typeOrUserType, and initializes with
1310 \a copy if \a copy is not 0.
1311
1312 Note that you have to pass the address of the variable you want stored.
1313
1314 Usually, you never have to use this constructor, use qVariantFromValue()
1315 instead to construct variants from the pointer types represented by
1316 \c QMetaType::VoidStar, \c QMetaType::QObjectStar and
1317 \c QMetaType::QWidgetStar.
1318
1319 \sa qVariantFromValue(), Type
1320*/
1321
1322/*!
1323 \fn QVariant::QVariant(Type type)
1324
1325 Constructs a null variant of type \a type.
1326*/
1327
1328
1329
1330/*!
1331 \fn QVariant::create(int type, const void *copy)
1332
1333 \internal
1334
1335 Constructs a variant private of type \a type, and initializes with \a copy if
1336 \a copy is not 0.
1337*/
1338
1339void QVariant::create(int type, const void *copy)
1340{
1341 d.type = type;
1342 handler->construct(&d, copy);
1343}
1344
1345/*!
1346 \fn QVariant::~QVariant()
1347
1348 Destroys the QVariant and the contained object.
1349
1350 Note that subclasses that reimplement clear() should reimplement
1351 the destructor to call clear(). This destructor calls clear(), but
1352 because it is the destructor, QVariant::clear() is called rather
1353 than a subclass's clear().
1354*/
1355
1356QVariant::~QVariant()
1357{
1358 if (d.type > Char && (!d.is_shared || !d.data.shared->ref.deref()))
1359 handler->clear(&d);
1360}
1361
1362/*!
1363 \fn QVariant::QVariant(const QVariant &p)
1364
1365 Constructs a copy of the variant, \a p, passed as the argument to
1366 this constructor.
1367*/
1368
1369QVariant::QVariant(const QVariant &p)
1370 : d(p.d)
1371{
1372 if (d.is_shared) {
1373 d.data.shared->ref.ref();
1374 } else if (p.d.type > Char) {
1375 handler->construct(&d, p.constData());
1376 d.is_null = p.d.is_null;
1377 }
1378}
1379
1380#ifndef QT_NO_DATASTREAM
1381/*!
1382 Reads the variant from the data stream, \a s.
1383*/
1384QVariant::QVariant(QDataStream &s)
1385{
1386 d.is_null = true;
1387 s >> *this;
1388}
1389#endif //QT_NO_DATASTREAM
1390
1391/*!
1392 \fn QVariant::QVariant(const QString &val)
1393
1394 Constructs a new variant with a string value, \a val.
1395*/
1396
1397/*!
1398 \fn QVariant::QVariant(const QLatin1String &val)
1399
1400 Constructs a new variant with a string value, \a val.
1401*/
1402
1403/*!
1404 \fn QVariant::QVariant(const char *val)
1405
1406 Constructs a new variant with a string value of \a val.
1407 The variant creates a deep copy of \a val, using the encoding
1408 set by QTextCodec::setCodecForCStrings().
1409
1410 Note that \a val is converted to a QString for storing in the
1411 variant and QVariant::type() will return QMetaType::QString for
1412 the variant.
1413
1414 You can disable this operator by defining \c
1415 QT_NO_CAST_FROM_ASCII when you compile your applications.
1416
1417 \sa QTextCodec::setCodecForCStrings()
1418*/
1419
1420#ifndef QT_NO_CAST_FROM_ASCII
1421QVariant::QVariant(const char *val)
1422{
1423 QString s = QString::fromAscii(val);
1424 create(String, &s);
1425}
1426#endif
1427
1428/*!
1429 \fn QVariant::QVariant(const QStringList &val)
1430
1431 Constructs a new variant with a string list value, \a val.
1432*/
1433
1434/*!
1435 \fn QVariant::QVariant(const QMap<QString, QVariant> &val)
1436
1437 Constructs a new variant with a map of QVariants, \a val.
1438*/
1439
1440/*!
1441 \fn QVariant::QVariant(const QHash<QString, QVariant> &val)
1442
1443 Constructs a new variant with a hash of QVariants, \a val.
1444*/
1445
1446/*!
1447 \fn QVariant::QVariant(const QDate &val)
1448
1449 Constructs a new variant with a date value, \a val.
1450*/
1451
1452/*!
1453 \fn QVariant::QVariant(const QTime &val)
1454
1455 Constructs a new variant with a time value, \a val.
1456*/
1457
1458/*!
1459 \fn QVariant::QVariant(const QDateTime &val)
1460
1461 Constructs a new variant with a date/time value, \a val.
1462*/
1463
1464/*!
1465 \fn QVariant::QVariant(const QByteArray &val)
1466
1467 Constructs a new variant with a bytearray value, \a val.
1468*/
1469
1470/*!
1471 \fn QVariant::QVariant(const QBitArray &val)
1472
1473 Constructs a new variant with a bitarray value, \a val.
1474*/
1475
1476/*!
1477 \fn QVariant::QVariant(const QPoint &val)
1478
1479 Constructs a new variant with a point value of \a val.
1480 */
1481
1482/*!
1483 \fn QVariant::QVariant(const QPointF &val)
1484
1485 Constructs a new variant with a point value of \a val.
1486 */
1487
1488/*!
1489 \fn QVariant::QVariant(const QRectF &val)
1490
1491 Constructs a new variant with a rect value of \a val.
1492 */
1493
1494/*!
1495 \fn QVariant::QVariant(const QLineF &val)
1496
1497 Constructs a new variant with a line value of \a val.
1498 */
1499
1500/*!
1501 \fn QVariant::QVariant(const QLine &val)
1502
1503 Constructs a new variant with a line value of \a val.
1504 */
1505
1506/*!
1507 \fn QVariant::QVariant(const QRect &val)
1508
1509 Constructs a new variant with a rect value of \a val.
1510 */
1511
1512/*!
1513 \fn QVariant::QVariant(const QSize &val)
1514
1515 Constructs a new variant with a size value of \a val.
1516 */
1517
1518/*!
1519 \fn QVariant::QVariant(const QSizeF &val)
1520
1521 Constructs a new variant with a size value of \a val.
1522 */
1523
1524/*!
1525 \fn QVariant::QVariant(const QUrl &val)
1526
1527 Constructs a new variant with a url value of \a val.
1528 */
1529
1530/*!
1531 \fn QVariant::QVariant(int val)
1532
1533 Constructs a new variant with an integer value, \a val.
1534*/
1535
1536/*!
1537 \fn QVariant::QVariant(uint val)
1538
1539 Constructs a new variant with an unsigned integer value, \a val.
1540*/
1541
1542/*!
1543 \fn QVariant::QVariant(qlonglong val)
1544
1545 Constructs a new variant with a long long integer value, \a val.
1546*/
1547
1548/*!
1549 \fn QVariant::QVariant(qulonglong val)
1550
1551 Constructs a new variant with an unsigned long long integer value, \a val.
1552*/
1553
1554
1555/*!
1556 \fn QVariant::QVariant(bool val)
1557
1558 Constructs a new variant with a boolean value, \a val.
1559*/
1560
1561/*!
1562 \fn QVariant::QVariant(double val)
1563
1564 Constructs a new variant with a floating point value, \a val.
1565*/
1566
1567/*!
1568 \fn QVariant::QVariant(const QList<QVariant> &val)
1569
1570 Constructs a new variant with a list value, \a val.
1571*/
1572
1573/*!
1574 \fn QVariant::QVariant(const QChar &c)
1575
1576 Constructs a new variant with a char value, \a c.
1577*/
1578
1579/*!
1580 \fn QVariant::QVariant(const QLocale &l)
1581
1582 Constructs a new variant with a locale value, \a l.
1583*/
1584
1585/*!
1586 \fn QVariant::QVariant(const QRegExp &regExp)
1587
1588 Constructs a new variant with the regexp value \a regExp.
1589*/
1590
1591/*! \since 4.2
1592 \fn QVariant::QVariant(Qt::GlobalColor color)
1593
1594 Constructs a new variant of type QVariant::Color and initializes
1595 it with \a color.
1596
1597 This is a convenience constructor that allows \c{QVariant(Qt::blue);}
1598 to create a valid QVariant storing a QColor.
1599
1600 Note: This constructor will assert if the application does not link
1601 to the Qt GUI library.
1602 */
1603
1604QVariant::QVariant(Type type)
1605{ create(type, 0); }
1606QVariant::QVariant(int typeOrUserType, const void *copy)
1607{ create(typeOrUserType, copy); d.is_null = false; }
1608QVariant::QVariant(int val)
1609{ d.is_null = false; d.type = Int; d.data.i = val; }
1610QVariant::QVariant(uint val)
1611{ d.is_null = false; d.type = UInt; d.data.u = val; }
1612QVariant::QVariant(qlonglong val)
1613{ d.is_null = false; d.type = LongLong; d.data.ll = val; }
1614QVariant::QVariant(qulonglong val)
1615{ d.is_null = false; d.type = ULongLong; d.data.ull = val; }
1616QVariant::QVariant(bool val)
1617{ d.is_null = false; d.type = Bool; d.data.b = val; }
1618QVariant::QVariant(double val)
1619{ d.is_null = false; d.type = Double; d.data.d = val; }
1620
1621QVariant::QVariant(const QByteArray &val)
1622{ create(ByteArray, &val); }
1623QVariant::QVariant(const QBitArray &val)
1624{ create(BitArray, &val); }
1625QVariant::QVariant(const QString &val)
1626{ create(String, &val); }
1627QVariant::QVariant(const QChar &val)
1628{ create (Char, &val); }
1629QVariant::QVariant(const QLatin1String &val)
1630{ QString str(val); create(String, &str); }
1631QVariant::QVariant(const QStringList &val)
1632{ create(StringList, &val); }
1633
1634QVariant::QVariant(const QDate &val)
1635{ create(Date, &val); }
1636QVariant::QVariant(const QTime &val)
1637{ create(Time, &val); }
1638QVariant::QVariant(const QDateTime &val)
1639{ create(DateTime, &val); }
1640QVariant::QVariant(const QList<QVariant> &list)
1641{ create(List, &list); }
1642QVariant::QVariant(const QMap<QString, QVariant> &map)
1643{ create(Map, &map); }
1644QVariant::QVariant(const QHash<QString, QVariant> &hash)
1645{ create(Hash, &hash); }
1646#ifndef QT_NO_GEOM_VARIANT
1647QVariant::QVariant(const QPoint &pt) { create(Point, &pt); }
1648QVariant::QVariant(const QPointF &pt) { create (PointF, &pt); }
1649QVariant::QVariant(const QRectF &r) { create (RectF, &r); }
1650QVariant::QVariant(const QLineF &l) { create (LineF, &l); }
1651QVariant::QVariant(const QLine &l) { create (Line, &l); }
1652QVariant::QVariant(const QRect &r) { create(Rect, &r); }
1653QVariant::QVariant(const QSize &s) { create(Size, &s); }
1654QVariant::QVariant(const QSizeF &s) { create(SizeF, &s); }
1655#endif
1656QVariant::QVariant(const QUrl &u) { create(Url, &u); }
1657QVariant::QVariant(const QLocale &l) { create(Locale, &l); }
1658#ifndef QT_NO_REGEXP
1659QVariant::QVariant(const QRegExp &regExp) { create(RegExp, &regExp); }
1660#endif
1661QVariant::QVariant(Qt::GlobalColor color) { create(62, &color); }
1662
1663/*!
1664 Returns the storage type of the value stored in the variant.
1665 Although this function is declared as returning QVariant::Type,
1666 the return value should be interpreted as QMetaType::Type. In
1667 particular, QVariant::UserType is returned here only if the value
1668 is equal or greater than QMetaType::User.
1669
1670 Note that return values in the ranges QVariant::Char through
1671 QVariant::RegExp and QVariant::Font through QVariant::Transform
1672 correspond to the values in the ranges QMetaType::QChar through
1673 QMetaType::QRegExp and QMetaType::QFont through QMetaType::QTransform.
1674
1675 Pay particular attention when working with char and QChar
1676 variants. Note that there is no QVariant constructor specifically
1677 for type char, but there is one for QChar. For a variant of type
1678 QChar, this function returns QVariant::Char, which is the same as
1679 QMetaType::QChar, but for a variant of type \c char, this function
1680 returns QMetaType::Char, which is \e not the same as
1681 QVariant::Char.
1682
1683 Also note that the types \c void*, \c long, \c short, \c unsigned
1684 \c long, \c unsigned \c short, \c unsigned \c char, \c float, \c
1685 QObject*, and \c QWidget* are represented in QMetaType::Type but
1686 not in QVariant::Type, and they can be returned by this function.
1687 However, they are considered to be user defined types when tested
1688 against QVariant::Type.
1689
1690 To test whether an instance of QVariant contains a data type that
1691 is compatible with the data type you are interested in, use
1692 canConvert().
1693*/
1694
1695QVariant::Type QVariant::type() const
1696{
1697 return d.type >= QMetaType::User ? UserType : static_cast<Type>(d.type);
1698}
1699
1700/*!
1701 Returns the storage type of the value stored in the variant. For
1702 non-user types, this is the same as type().
1703
1704 \sa type()
1705*/
1706
1707int QVariant::userType() const
1708{
1709 return d.type;
1710}
1711
1712/*!
1713 Assigns the value of the variant \a variant to this variant.
1714*/
1715QVariant& QVariant::operator=(const QVariant &variant)
1716{
1717 if (this == &variant)
1718 return *this;
1719
1720 clear();
1721 if (variant.d.is_shared) {
1722 variant.d.data.shared->ref.ref();
1723 d = variant.d;
1724 } else if (variant.d.type > Char) {
1725 d.type = variant.d.type;
1726 handler->construct(&d, variant.constData());
1727 d.is_null = variant.d.is_null;
1728 } else {
1729 d = variant.d;
1730 }
1731
1732 return *this;
1733}
1734
1735/*!
1736 \fn void QVariant::detach()
1737
1738 \internal
1739*/
1740
1741void QVariant::detach()
1742{
1743 if (!d.is_shared || d.data.shared->ref == 1)
1744 return;
1745
1746 Private dd;
1747 dd.type = d.type;
1748 handler->construct(&dd, constData());
1749 if (!d.data.shared->ref.deref())
1750 handler->clear(&d);
1751 d.data.shared = dd.data.shared;
1752}
1753
1754/*!
1755 \fn bool QVariant::isDetached() const
1756
1757 \internal
1758*/
1759
1760// ### Qt 5: change typeName()(and froends= to return a QString. Suggestion from Harald.
1761/*!
1762 Returns the name of the type stored in the variant. The returned
1763 strings describe the C++ datatype used to store the data: for
1764 example, "QFont", "QString", or "QVariantList". An Invalid
1765 variant returns 0.
1766*/
1767const char *QVariant::typeName() const
1768{
1769 return typeToName(Type(d.type));
1770}
1771
1772/*!
1773 Convert this variant to type Invalid and free up any resources
1774 used.
1775*/
1776void QVariant::clear()
1777{
1778 if (!d.is_shared || !d.data.shared->ref.deref())
1779 handler->clear(&d);
1780 d.type = Invalid;
1781 d.is_null = true;
1782 d.is_shared = false;
1783}
1784
1785/*!
1786 Converts the enum representation of the storage type, \a typ, to
1787 its string representation.
1788
1789 Returns a null pointer if the type is QVariant::Invalid or doesn't exist.
1790*/
1791const char *QVariant::typeToName(Type typ)
1792{
1793 if (typ == Invalid)
1794 return 0;
1795 if (typ == UserType)
1796 return "UserType";
1797
1798 return QMetaType::typeName(typ);
1799}
1800
1801
1802/*!
1803 Converts the string representation of the storage type given in \a
1804 name, to its enum representation.
1805
1806 If the string representation cannot be converted to any enum
1807 representation, the variant is set to \c Invalid.
1808*/
1809QVariant::Type QVariant::nameToType(const char *name)
1810{
1811 if (!name || !*name)
1812 return Invalid;
1813 if (strcmp(name, "Q3CString") == 0)
1814 return ByteArray;
1815 if (strcmp(name, "Q_LLONG") == 0)
1816 return LongLong;
1817 if (strcmp(name, "Q_ULLONG") == 0)
1818 return ULongLong;
1819 if (strcmp(name, "QIconSet") == 0)
1820 return Icon;
1821 if (strcmp(name, "UserType") == 0)
1822 return UserType;
1823
1824 int metaType = QMetaType::type(name);
1825 return metaType <= int(LastGuiType) ? QVariant::Type(metaType) : UserType;
1826}
1827
1828#ifndef QT_NO_DATASTREAM
1829enum { MapFromThreeCount = 35 };
1830static const uint map_from_three[MapFromThreeCount] =
1831{
1832 QVariant::Invalid,
1833 QVariant::Map,
1834 QVariant::List,
1835 QVariant::String,
1836 QVariant::StringList,
1837 QVariant::Font,
1838 QVariant::Pixmap,
1839 QVariant::Brush,
1840 QVariant::Rect,
1841 QVariant::Size,
1842 QVariant::Color,
1843 QVariant::Palette,
1844 63, // ColorGroup
1845 QVariant::Icon,
1846 QVariant::Point,
1847 QVariant::Image,
1848 QVariant::Int,
1849 QVariant::UInt,
1850 QVariant::Bool,
1851 QVariant::Double,
1852 QVariant::ByteArray,
1853 QVariant::Polygon,
1854 QVariant::Region,
1855 QVariant::Bitmap,
1856 QVariant::Cursor,
1857 QVariant::SizePolicy,
1858 QVariant::Date,
1859 QVariant::Time,
1860 QVariant::DateTime,
1861 QVariant::ByteArray,
1862 QVariant::BitArray,
1863 QVariant::KeySequence,
1864 QVariant::Pen,
1865 QVariant::LongLong,
1866 QVariant::ULongLong
1867};
1868
1869/*!
1870 Internal function for loading a variant from stream \a s. Use the
1871 stream operators instead.
1872
1873 \internal
1874*/
1875void QVariant::load(QDataStream &s)
1876{
1877 clear();
1878
1879 quint32 u;
1880 s >> u;
1881 if (s.version() < QDataStream::Qt_4_0) {
1882 if (u >= MapFromThreeCount)
1883 return;
1884 u = map_from_three[u];
1885 }
1886 qint8 is_null = false;
1887 if (s.version() >= QDataStream::Qt_4_2)
1888 s >> is_null;
1889 if (u == QVariant::UserType) {
1890 QByteArray name;
1891 s >> name;
1892 u = QMetaType::type(name);
1893 if (!u) {
1894 s.setStatus(QDataStream::ReadCorruptData);
1895 return;
1896 }
1897 }
1898 create(static_cast<int>(u), 0);
1899 d.is_null = is_null;
1900
1901 if (d.type == QVariant::Invalid) {
1902 // Since we wrote something, we should read something
1903 QString x;
1904 s >> x;
1905 d.is_null = true;
1906 return;
1907 }
1908
1909 // const cast is safe since we operate on a newly constructed variant
1910 if (!QMetaType::load(s, d.type, const_cast<void *>(constDataHelper(d)))) {
1911 s.setStatus(QDataStream::ReadCorruptData);
1912 qWarning("QVariant::load: unable to load type %d.", d.type);
1913 }
1914}
1915
1916/*!
1917 Internal function for saving a variant to the stream \a s. Use the
1918 stream operators instead.
1919
1920 \internal
1921*/
1922void QVariant::save(QDataStream &s) const
1923{
1924 quint32 tp = type();
1925 if (s.version() < QDataStream::Qt_4_0) {
1926 int i;
1927 for (i = MapFromThreeCount - 1; i >= 0; i--) {
1928 if (map_from_three[i] == tp) {
1929 tp = i;
1930 break;
1931 }
1932 }
1933 if (i == -1) {
1934 s << QVariant();
1935 return;
1936 }
1937 }
1938 s << tp;
1939 if (s.version() >= QDataStream::Qt_4_2)
1940 s << qint8(d.is_null);
1941 if (tp == QVariant::UserType) {
1942 s << QMetaType::typeName(userType());
1943 }
1944
1945 if (d.type == QVariant::Invalid) {
1946 s << QString();
1947 return;
1948 }
1949
1950 if (!QMetaType::save(s, d.type, constDataHelper(d))) {
1951 Q_ASSERT_X(false, "QVariant::save", "Invalid type to save");
1952 qWarning("QVariant::save: unable to save type %d.", d.type);
1953 }
1954}
1955
1956/*!
1957 \since 4.4
1958
1959 Reads a variant \a p from the stream \a s.
1960
1961 \sa \link datastreamformat.html Format of the QDataStream
1962 operators \endlink
1963*/
1964QDataStream& operator>>(QDataStream &s, QVariant &p)
1965{
1966 p.load(s);
1967 return s;
1968}
1969
1970/*!
1971 Writes a variant \a p to the stream \a s.
1972
1973 \sa \link datastreamformat.html Format of the QDataStream
1974 operators \endlink
1975*/
1976QDataStream& operator<<(QDataStream &s, const QVariant &p)
1977{
1978 p.save(s);
1979 return s;
1980}
1981
1982/*!
1983 Reads a variant type \a p in enum representation from the stream \a s.
1984*/
1985QDataStream& operator>>(QDataStream &s, QVariant::Type &p)
1986{
1987 quint32 u;
1988 s >> u;
1989 p = (QVariant::Type)u;
1990
1991 return s;
1992}
1993
1994/*!
1995 Writes a variant type \a p to the stream \a s.
1996*/
1997QDataStream& operator<<(QDataStream &s, const QVariant::Type p)
1998{
1999 s << static_cast<quint32>(p);
2000
2001 return s;
2002}
2003
2004#endif //QT_NO_DATASTREAM
2005
2006/*!
2007 \fn bool QVariant::isValid() const
2008
2009 Returns true if the storage type of this variant is not
2010 QVariant::Invalid; otherwise returns false.
2011*/
2012
2013template <typename T>
2014inline T qVariantToHelper(const QVariant::Private &d, QVariant::Type t,
2015 const QVariant::Handler *handler, T * = 0)
2016{
2017 if (d.type == t)
2018 return *v_cast<T>(&d);
2019
2020 T ret;
2021 handler->convert(&d, t, &ret, 0);
2022 return ret;
2023}
2024
2025/*!
2026 \fn QStringList QVariant::toStringList() const
2027
2028 Returns the variant as a QStringList if the variant has type()
2029 StringList, \l String, or \l List of a type that can be converted
2030 to QString; otherwise returns an empty list.
2031
2032 \sa canConvert(), convert()
2033*/
2034QStringList QVariant::toStringList() const
2035{
2036 return qVariantToHelper<QStringList>(d, StringList, handler);
2037}
2038
2039/*!
2040 Returns the variant as a QString if the variant has type() \l
2041 String, \l Bool, \l ByteArray, \l Char, \l Date, \l DateTime, \l
2042 Double, \l Int, \l LongLong, \l StringList, \l Time, \l UInt, or
2043 \l ULongLong; otherwise returns an empty string.
2044
2045 \sa canConvert(), convert()
2046*/
2047QString QVariant::toString() const
2048{
2049 return qVariantToHelper<QString>(d, String, handler);
2050}
2051
2052/*!
2053 Returns the variant as a QMap<QString, QVariant> if the variant
2054 has type() \l Map; otherwise returns an empty map.
2055
2056 \sa canConvert(), convert()
2057*/
2058QVariantMap QVariant::toMap() const
2059{
2060 return qVariantToHelper<QVariantMap>(d, Map, handler);
2061}
2062
2063/*!
2064 Returns the variant as a QHash<QString, QVariant> if the variant
2065 has type() \l Hash; otherwise returns an empty map.
2066
2067 \sa canConvert(), convert()
2068*/
2069QVariantHash QVariant::toHash() const
2070{
2071 return qVariantToHelper<QVariantHash>(d, Hash, handler);
2072}
2073
2074/*!
2075 \fn QDate QVariant::toDate() const
2076
2077 Returns the variant as a QDate if the variant has type() \l Date,
2078 \l DateTime, or \l String; otherwise returns an invalid date.
2079
2080 If the type() is \l String, an invalid date will be returned if the
2081 string cannot be parsed as a Qt::ISODate format date.
2082
2083 \sa canConvert(), convert()
2084*/
2085QDate QVariant::toDate() const
2086{
2087 return qVariantToHelper<QDate>(d, Date, handler);
2088}
2089
2090/*!
2091 \fn QTime QVariant::toTime() const
2092
2093 Returns the variant as a QTime if the variant has type() \l Time,
2094 \l DateTime, or \l String; otherwise returns an invalid time.
2095
2096 If the type() is \l String, an invalid time will be returned if
2097 the string cannot be parsed as a Qt::ISODate format time.
2098
2099 \sa canConvert(), convert()
2100*/
2101QTime QVariant::toTime() const
2102{
2103 return qVariantToHelper<QTime>(d, Time, handler);
2104}
2105
2106/*!
2107 \fn QDateTime QVariant::toDateTime() const
2108
2109 Returns the variant as a QDateTime if the variant has type() \l
2110 DateTime, \l Date, or \l String; otherwise returns an invalid
2111 date/time.
2112
2113 If the type() is \l String, an invalid date/time will be returned
2114 if the string cannot be parsed as a Qt::ISODate format date/time.
2115
2116 \sa canConvert(), convert()
2117*/
2118QDateTime QVariant::toDateTime() const
2119{
2120 return qVariantToHelper<QDateTime>(d, DateTime, handler);
2121}
2122
2123/*!
2124 \fn QByteArray QVariant::toByteArray() const
2125
2126 Returns the variant as a QByteArray if the variant has type() \l
2127 ByteArray or \l String (converted using QString::fromAscii());
2128 otherwise returns an empty byte array.
2129
2130 \sa canConvert(), convert()
2131*/
2132QByteArray QVariant::toByteArray() const
2133{
2134 return qVariantToHelper<QByteArray>(d, ByteArray, handler);
2135}
2136
2137#ifndef QT_NO_GEOM_VARIANT
2138/*!
2139 \fn QPoint QVariant::toPoint() const
2140
2141 Returns the variant as a QPoint if the variant has type()
2142 \l Point or \l PointF; otherwise returns a null QPoint.
2143
2144 \sa canConvert(), convert()
2145*/
2146QPoint QVariant::toPoint() const
2147{
2148 return qVariantToHelper<QPoint>(d, Point, handler);
2149}
2150
2151/*!
2152 \fn QRect QVariant::toRect() const
2153
2154 Returns the variant as a QRect if the variant has type() \l Rect;
2155 otherwise returns an invalid QRect.
2156
2157 \sa canConvert(), convert()
2158*/
2159QRect QVariant::toRect() const
2160{
2161 return qVariantToHelper<QRect>(d, Rect, handler);
2162}
2163
2164/*!
2165 \fn QSize QVariant::toSize() const
2166
2167 Returns the variant as a QSize if the variant has type() \l Size;
2168 otherwise returns an invalid QSize.
2169
2170 \sa canConvert(), convert()
2171*/
2172QSize QVariant::toSize() const
2173{
2174 return qVariantToHelper<QSize>(d, Size, handler);
2175}
2176
2177/*!
2178 \fn QSizeF QVariant::toSizeF() const
2179
2180 Returns the variant as a QSizeF if the variant has type() \l
2181 SizeF; otherwise returns an invalid QSizeF.
2182
2183 \sa canConvert(), convert()
2184*/
2185QSizeF QVariant::toSizeF() const
2186{
2187 return qVariantToHelper<QSizeF>(d, SizeF, handler);
2188}
2189
2190/*!
2191 \fn QRectF QVariant::toRectF() const
2192
2193 Returns the variant as a QRectF if the variant has type() \l Rect
2194 or \l RectF; otherwise returns an invalid QRectF.
2195
2196 \sa canConvert(), convert()
2197*/
2198QRectF QVariant::toRectF() const
2199{
2200 return qVariantToHelper<QRectF>(d, RectF, handler);
2201}
2202
2203/*!
2204 \fn QLineF QVariant::toLineF() const
2205
2206 Returns the variant as a QLineF if the variant has type() \l
2207 LineF; otherwise returns an invalid QLineF.
2208
2209 \sa canConvert(), convert()
2210*/
2211QLineF QVariant::toLineF() const
2212{
2213 return qVariantToHelper<QLineF>(d, LineF, handler);
2214}
2215
2216/*!
2217 \fn QLine QVariant::toLine() const
2218
2219 Returns the variant as a QLine if the variant has type() \l Line;
2220 otherwise returns an invalid QLine.
2221
2222 \sa canConvert(), convert()
2223*/
2224QLine QVariant::toLine() const
2225{
2226 return qVariantToHelper<QLine>(d, Line, handler);
2227}
2228
2229/*!
2230 \fn QPointF QVariant::toPointF() const
2231
2232 Returns the variant as a QPointF if the variant has type() \l
2233 Point or \l PointF; otherwise returns a null QPointF.
2234
2235 \sa canConvert(), convert()
2236*/
2237QPointF QVariant::toPointF() const
2238{
2239 return qVariantToHelper<QPointF>(d, PointF, handler);
2240}
2241
2242#endif // QT_NO_GEOM_VARIANT
2243
2244/*!
2245 \fn QUrl QVariant::toUrl() const
2246
2247 Returns the variant as a QUrl if the variant has type()
2248 \l Url; otherwise returns an invalid QUrl.
2249
2250 \sa canConvert(), convert()
2251*/
2252QUrl QVariant::toUrl() const
2253{
2254 return qVariantToHelper<QUrl>(d, Url, handler);
2255}
2256
2257/*!
2258 \fn QLocale QVariant::toLocale() const
2259
2260 Returns the variant as a QLocale if the variant has type()
2261 \l Locale; otherwise returns an invalid QLocale.
2262
2263 \sa canConvert(), convert()
2264*/
2265QLocale QVariant::toLocale() const
2266{
2267 return qVariantToHelper<QLocale>(d, Locale, handler);
2268}
2269
2270/*!
2271 \fn QRegExp QVariant::toRegExp() const
2272 \since 4.1
2273
2274 Returns the variant as a QRegExp if the variant has type() \l
2275 RegExp; otherwise returns an empty QRegExp.
2276
2277 \sa canConvert(), convert()
2278*/
2279#ifndef QT_NO_REGEXP
2280QRegExp QVariant::toRegExp() const
2281{
2282 return qVariantToHelper<QRegExp>(d, RegExp, handler);
2283}
2284#endif
2285
2286/*!
2287 \fn QChar QVariant::toChar() const
2288
2289 Returns the variant as a QChar if the variant has type() \l Char,
2290 \l Int, or \l UInt; otherwise returns an invalid QChar.
2291
2292 \sa canConvert(), convert()
2293*/
2294QChar QVariant::toChar() const
2295{
2296 return qVariantToHelper<QChar>(d, Char, handler);
2297}
2298
2299/*!
2300 Returns the variant as a QBitArray if the variant has type()
2301 \l BitArray; otherwise returns an empty bit array.
2302
2303 \sa canConvert(), convert()
2304*/
2305QBitArray QVariant::toBitArray() const
2306{
2307 return qVariantToHelper<QBitArray>(d, BitArray, handler);
2308}
2309
2310template <typename T>
2311inline T qNumVariantToHelper(const QVariant::Private &d, QVariant::Type t,
2312 const QVariant::Handler *handler, bool *ok, const T& val)
2313{
2314 if (ok)
2315 *ok = true;
2316 if (d.type == t)
2317 return val;
2318
2319 T ret;
2320 if (!handler->convert(&d, t, &ret, ok) && ok)
2321 *ok = false;
2322 return ret;
2323}
2324
2325/*!
2326 Returns the variant as an int if the variant has type() \l Int,
2327 \l Bool, \l ByteArray, \l Char, \l Double, \l LongLong, \l
2328 String, \l UInt, or \l ULongLong; otherwise returns 0.
2329
2330 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2331 converted to an int; otherwise \c{*}\a{ok} is set to false.
2332
2333 \bold{Warning:} If the value is convertible to a \l LongLong but is too
2334 large to be represented in an int, the resulting arithmetic overflow will
2335 not be reflected in \a ok. A simple workaround is to use QString::toInt().
2336 Fixing this bug has been postponed to Qt 5 in order to avoid breaking existing code.
2337
2338 \sa canConvert(), convert()
2339*/
2340int QVariant::toInt(bool *ok) const
2341{
2342 return qNumVariantToHelper<int>(d, Int, handler, ok, d.data.i);
2343}
2344
2345/*!
2346 Returns the variant as an unsigned int if the variant has type()
2347 \l UInt, \l Bool, \l ByteArray, \l Char, \l Double, \l Int, \l
2348 LongLong, \l String, or \l ULongLong; otherwise returns 0.
2349
2350 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2351 converted to an unsigned int; otherwise \c{*}\a{ok} is set to false.
2352
2353 \bold{Warning:} If the value is convertible to a \l ULongLong but is too
2354 large to be represented in an unsigned int, the resulting arithmetic overflow will
2355 not be reflected in \a ok. A simple workaround is to use QString::toUInt().
2356 Fixing this bug has been postponed to Qt 5 in order to avoid breaking existing code.
2357
2358 \sa canConvert(), convert()
2359*/
2360uint QVariant::toUInt(bool *ok) const
2361{
2362 return qNumVariantToHelper<uint>(d, UInt, handler, ok, d.data.u);
2363}
2364
2365/*!
2366 Returns the variant as a long long int if the variant has type()
2367 \l LongLong, \l Bool, \l ByteArray, \l Char, \l Double, \l Int,
2368 \l String, \l UInt, or \l ULongLong; otherwise returns 0.
2369
2370 If \a ok is non-null: \c{*}\c{ok} is set to true if the value could be
2371 converted to an int; otherwise \c{*}\c{ok} is set to false.
2372
2373 \sa canConvert(), convert()
2374*/
2375qlonglong QVariant::toLongLong(bool *ok) const
2376{
2377 return qNumVariantToHelper<qlonglong>(d, LongLong, handler, ok, d.data.ll);
2378}
2379
2380/*!
2381 Returns the variant as as an unsigned long long int if the
2382 variant has type() \l ULongLong, \l Bool, \l ByteArray, \l Char,
2383 \l Double, \l Int, \l LongLong, \l String, or \l UInt; otherwise
2384 returns 0.
2385
2386 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2387 converted to an int; otherwise \c{*}\a{ok} is set to false.
2388
2389 \sa canConvert(), convert()
2390*/
2391qulonglong QVariant::toULongLong(bool *ok) const
2392{
2393 return qNumVariantToHelper<qulonglong>(d, ULongLong, handler, ok, d.data.ull);
2394}
2395
2396/*!
2397 Returns the variant as a bool if the variant has type() Bool.
2398
2399 Returns true if the variant has type() \l Bool, \l Char, \l Double,
2400 \l Int, \l LongLong, \l UInt, or \l ULongLong and the value is
2401 non-zero, or if the variant has type \l String or \l ByteArray and
2402 its lower-case content is not empty, "0" or "false"; otherwise
2403 returns false.
2404
2405 \sa canConvert(), convert()
2406*/
2407bool QVariant::toBool() const
2408{
2409 if (d.type == Bool)
2410 return d.data.b;
2411
2412 bool res = false;
2413 handler->convert(&d, Bool, &res, 0);
2414
2415 return res;
2416}
2417
2418/*!
2419 Returns the variant as a double if the variant has type() \l
2420 Double, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
2421 UInt, or \l ULongLong; otherwise returns 0.0.
2422
2423 If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2424 converted to a double; otherwise \c{*}\a{ok} is set to false.
2425
2426 \sa canConvert(), convert()
2427*/
2428double QVariant::toDouble(bool *ok) const
2429{
2430 return qNumVariantToHelper<double>(d, Double, handler, ok, d.data.d);
2431}
2432
2433/*!
2434 Returns the variant as a QVariantList if the variant has type()
2435 \l List or \l StringList; otherwise returns an empty list.
2436
2437 \sa canConvert(), convert()
2438*/
2439QVariantList QVariant::toList() const
2440{
2441 return qVariantToHelper<QVariantList>(d, List, handler);
2442}
2443
2444/*! \fn QVariant::canCast(Type t) const
2445 Use canConvert() instead.
2446*/
2447
2448/*! \fn QVariant::cast(Type t)
2449 Use convert() instead.
2450*/
2451
2452
2453static const quint32 qCanConvertMatrix[QVariant::LastCoreType + 1] =
2454{
2455/*Invalid*/ 0,
2456
2457/*Bool*/ 1 << QVariant::Double | 1 << QVariant::Int | 1 << QVariant::UInt
2458 | 1 << QVariant::LongLong | 1 << QVariant::ULongLong | 1 << QVariant::ByteArray
2459 | 1 << QVariant::String | 1 << QVariant::Char,
2460
2461/*Int*/ 1 << QVariant::UInt | 1 << QVariant::String | 1 << QVariant::Double
2462 | 1 << QVariant::Bool | 1 << QVariant::LongLong | 1 << QVariant::ULongLong
2463 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2464
2465/*UInt*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::Double
2466 | 1 << QVariant::Bool | 1 << QVariant::LongLong | 1 << QVariant::ULongLong
2467 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2468
2469/*LLong*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::Double
2470 | 1 << QVariant::Bool | 1 << QVariant::UInt | 1 << QVariant::ULongLong
2471 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2472
2473/*ULlong*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::Double
2474 | 1 << QVariant::Bool | 1 << QVariant::UInt | 1 << QVariant::LongLong
2475 | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2476
2477/*double*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::ULongLong
2478 | 1 << QVariant::Bool | 1 << QVariant::UInt | 1 << QVariant::LongLong
2479 | 1 << QVariant::ByteArray,
2480
2481/*QChar*/ 1 << QVariant::Int | 1 << QVariant::UInt | 1 << QVariant::LongLong
2482 | 1 << QVariant::ULongLong,
2483
2484/*QMap*/ 0,
2485
2486/*QList*/ 1 << QVariant::StringList,
2487
2488/*QString*/ 1 << QVariant::StringList | 1 << QVariant::ByteArray | 1 << QVariant::Int
2489 | 1 << QVariant::UInt | 1 << QVariant::Bool | 1 << QVariant::Double
2490 | 1 << QVariant::Date | 1 << QVariant::Time | 1 << QVariant::DateTime
2491 | 1 << QVariant::LongLong | 1 << QVariant::ULongLong | 1 << QVariant::Char,
2492
2493/*QStringList*/ 1 << QVariant::List | 1 << QVariant::String,
2494
2495/*QByteArray*/ 1 << QVariant::String | 1 << QVariant::Int | 1 << QVariant::UInt | 1 << QVariant::Bool
2496 | 1 << QVariant::Double | 1 << QVariant::LongLong | 1 << QVariant::ULongLong,
2497
2498/*QBitArray*/ 0,
2499
2500/*QDate*/ 1 << QVariant::String | 1 << QVariant::DateTime,
2501
2502/*QTime*/ 1 << QVariant::String | 1 << QVariant::DateTime,
2503
2504/*QDateTime*/ 1 << QVariant::String | 1 << QVariant::Date,
2505
2506/*QUrl*/ 0,
2507
2508/*QLocale*/ 0,
2509
2510/*QRect*/ 1 << QVariant::RectF,
2511
2512/*QRectF*/ 1 << QVariant::Rect,
2513
2514/*QSize*/ 1 << QVariant::SizeF,
2515
2516/*QSizeF*/ 1 << QVariant::Size,
2517
2518/*QLine*/ 1 << QVariant::LineF,
2519
2520/*QLineF*/ 1 << QVariant::Line,
2521
2522/*QPoint*/ 1 << QVariant::PointF,
2523
2524/*QPointF*/ 1 << QVariant::Point,
2525
2526/*QRegExp*/ 0,
2527
2528/*QHash*/ 0
2529
2530};
2531
2532/*!
2533 Returns true if the variant's type can be cast to the requested
2534 type, \a t. Such casting is done automatically when calling the
2535 toInt(), toBool(), ... methods.
2536
2537 The following casts are done automatically:
2538
2539 \table
2540 \header \o Type \o Automatically Cast To
2541 \row \o \l Bool \o \l Char, \l Double, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2542 \row \o \l ByteArray \o \l Double, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2543 \row \o \l Char \o \l Bool, \l Int, \l UInt, \l LongLong, \l ULongLong
2544 \row \o \l Color \o \l String
2545 \row \o \l Date \o \l DateTime, \l String
2546 \row \o \l DateTime \o \l Date, \l String, \l Time
2547 \row \o \l Double \o \l Bool, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2548 \row \o \l Font \o \l String
2549 \row \o \l Int \o \l Bool, \l Char, \l Double, \l LongLong, \l String, \l UInt, \l ULongLong
2550 \row \o \l KeySequence \o \l Int, \l String
2551 \row \o \l List \o \l StringList (if the list's items can be converted to strings)
2552 \row \o \l LongLong \o \l Bool, \l ByteArray, \l Char, \l Double, \l Int, \l String, \l UInt, \l ULongLong
2553 \row \o \l Point \o PointF
2554 \row \o \l Rect \o RectF
2555 \row \o \l String \o \l Bool, \l ByteArray, \l Char, \l Color, \l Date, \l DateTime, \l Double,
2556 \l Font, \l Int, \l KeySequence, \l LongLong, \l StringList, \l Time, \l UInt,
2557 \l ULongLong
2558 \row \o \l StringList \o \l List, \l String (if the list contains exactly one item)
2559 \row \o \l Time \o \l String
2560 \row \o \l UInt \o \l Bool, \l Char, \l Double, \l Int, \l LongLong, \l String, \l ULongLong
2561 \row \o \l ULongLong \o \l Bool, \l Char, \l Double, \l Int, \l LongLong, \l String, \l UInt
2562 \endtable
2563
2564 \sa convert()
2565*/
2566bool QVariant::canConvert(Type t) const
2567{
2568 if (d.type == uint(t))
2569 return true;
2570
2571 if (d.type > QVariant::LastCoreType || t > QVariant::LastCoreType) {
2572 switch (uint(t)) {
2573 case QVariant::Int:
2574 return d.type == QVariant::KeySequence
2575 || d.type == QMetaType::ULong
2576 || d.type == QMetaType::Long
2577 || d.type == QMetaType::UShort
2578 || d.type == QMetaType::UChar
2579 || d.type == QMetaType::Char
2580 || d.type == QMetaType::Short;
2581 case QVariant::Image:
2582 return d.type == QVariant::Pixmap || d.type == QVariant::Bitmap;
2583 case QVariant::Pixmap:
2584 return d.type == QVariant::Image || d.type == QVariant::Bitmap
2585 || d.type == QVariant::Brush;
2586 case QVariant::Bitmap:
2587 return d.type == QVariant::Pixmap || d.type == QVariant::Image;
2588 case QVariant::ByteArray:
2589 return d.type == QVariant::Color;
2590 case QVariant::String:
2591 return d.type == QVariant::KeySequence || d.type == QVariant::Font
2592 || d.type == QVariant::Color;
2593 case QVariant::KeySequence:
2594 return d.type == QVariant::String || d.type == QVariant::Int;
2595 case QVariant::Font:
2596 return d.type == QVariant::String;
2597 case QVariant::Color:
2598 return d.type == QVariant::String || d.type == QVariant::ByteArray
2599 || d.type == QVariant::Brush;
2600 case QVariant::Brush:
2601 return d.type == QVariant::Color || d.type == QVariant::Pixmap;
2602 case QMetaType::Long:
2603 case QMetaType::Char:
2604 case QMetaType::UChar:
2605 case QMetaType::ULong:
2606 case QMetaType::Short:
2607 case QMetaType::UShort:
2608 case QMetaType::Float:
2609 return qCanConvertMatrix[QVariant::Int] & (1 << d.type) || d.type == QVariant::Int;
2610 default:
2611 return false;
2612 }
2613 }
2614
2615 if(t == String && d.type == StringList)
2616 return v_cast<QStringList>(&d)->count() == 1;
2617 else
2618 return qCanConvertMatrix[t] & (1 << d.type);
2619}
2620
2621/*!
2622 Casts the variant to the requested type, \a t. If the cast cannot be
2623 done, the variant is cleared. Returns true if the current type of
2624 the variant was successfully cast; otherwise returns false.
2625
2626 \warning For historical reasons, converting a null QVariant results
2627 in a null value of the desired type (e.g., an empty string for
2628 QString) and a result of false.
2629
2630 \sa canConvert(), clear()
2631*/
2632
2633bool QVariant::convert(Type t)
2634{
2635 if (d.type == uint(t))
2636 return true;
2637
2638 QVariant oldValue = *this;
2639
2640 clear();
2641 if (!oldValue.canConvert(t))
2642 return false;
2643
2644 create(t, 0);
2645 if (oldValue.isNull())
2646 return false;
2647
2648 bool isOk = true;
2649 if (!handler->convert(&oldValue.d, t, data(), &isOk))
2650 isOk = false;
2651 d.is_null = !isOk;
2652 return isOk;
2653}
2654
2655/*!
2656 \fn bool operator==(const QVariant &v1, const QVariant &v2)
2657
2658 \relates QVariant
2659
2660 Returns true if \a v1 and \a v2 are equal; otherwise returns false.
2661
2662 \warning This function doesn't support custom types registered
2663 with qRegisterMetaType().
2664*/
2665/*!
2666 \fn bool operator!=(const QVariant &v1, const QVariant &v2)
2667
2668 \relates QVariant
2669
2670 Returns false if \a v1 and \a v2 are equal; otherwise returns true.
2671
2672 \warning This function doesn't support custom types registered
2673 with qRegisterMetaType().
2674*/
2675
2676/*! \fn bool QVariant::operator==(const QVariant &v) const
2677
2678 Compares this QVariant with \a v and returns true if they are
2679 equal; otherwise returns false.
2680
2681 In the case of custom types, their equalness operators are not called.
2682 Instead the values' addresses are compared.
2683*/
2684
2685/*!
2686 \fn bool QVariant::operator!=(const QVariant &v) const
2687
2688 Compares this QVariant with \a v and returns true if they are not
2689 equal; otherwise returns false.
2690
2691 \warning This function doesn't support custom types registered
2692 with qRegisterMetaType().
2693*/
2694
2695static bool qIsNumericType(uint tp)
2696{
2697 return (tp >= QVariant::Bool && tp <= QVariant::Double)
2698 || (tp >= QMetaType::Long && tp <= QMetaType::Float);
2699}
2700
2701static bool qIsFloatingPoint(uint tp)
2702{
2703 return tp == QVariant::Double || tp == QMetaType::Float;
2704}
2705
2706/*! \internal
2707 */
2708bool QVariant::cmp(const QVariant &v) const
2709{
2710 QVariant v2 = v;
2711 if (d.type != v2.d.type) {
2712 if (qIsNumericType(d.type) && qIsNumericType(v.d.type)) {
2713 if (qIsFloatingPoint(d.type) || qIsFloatingPoint(v.d.type))
2714 return qFuzzyCompare(toDouble(), v.toDouble());
2715 else
2716 return toLongLong() == v.toLongLong();
2717 }
2718 if (!v2.canConvert(Type(d.type)) || !v2.convert(Type(d.type)))
2719 return false;
2720 }
2721 return handler->compare(&d, &v2.d);
2722}
2723
2724/*! \internal
2725 */
2726
2727const void *QVariant::constData() const
2728{
2729 return constDataHelper(d);
2730}
2731
2732/*!
2733 \fn const void* QVariant::data() const
2734
2735 \internal
2736*/
2737
2738/*! \internal */
2739void* QVariant::data()
2740{
2741 detach();
2742 return const_cast<void *>(constDataHelper(d));
2743}
2744
2745
2746#ifdef QT3_SUPPORT
2747/*! \internal
2748 */
2749void *QVariant::castOrDetach(Type t)
2750{
2751 if (d.type != uint(t)) {
2752 if (!convert(t))
2753 create(t, 0);
2754 } else {
2755 detach();
2756 }
2757 return data();
2758}
2759#endif
2760
2761/*!
2762 Returns true if this is a NULL variant, false otherwise.
2763*/
2764bool QVariant::isNull() const
2765{
2766 return handler->isNull(&d);
2767}
2768
2769#ifndef QT_NO_DEBUG_STREAM
2770QDebug operator<<(QDebug dbg, const QVariant &v)
2771{
2772#ifndef Q_BROKEN_DEBUG_STREAM
2773 dbg.nospace() << "QVariant(" << v.typeName() << ", ";
2774 QVariant::handler->debugStream(dbg, v);
2775 dbg.nospace() << ')';
2776 return dbg.space();
2777#else
2778 qWarning("This compiler doesn't support streaming QVariant to QDebug");
2779 return dbg;
2780 Q_UNUSED(v);
2781#endif
2782}
2783
2784QDebug operator<<(QDebug dbg, const QVariant::Type p)
2785{
2786#ifndef Q_BROKEN_DEBUG_STREAM
2787 dbg.nospace() << "QVariant::" << QVariant::typeToName(p);
2788 return dbg.space();
2789#else
2790 qWarning("This compiler doesn't support streaming QVariant::Type to QDebug");
2791 return dbg;
2792 Q_UNUSED(p);
2793#endif
2794}
2795#endif
2796
2797/*!
2798 \fn int &QVariant::asInt()
2799
2800 Use toInt() instead.
2801*/
2802
2803/*!
2804 \fn uint &QVariant::asUInt()
2805
2806 Use toUInt() instead.
2807*/
2808
2809/*!
2810 \fn qlonglong &QVariant::asLongLong()
2811
2812 Use toLongLong() instead.
2813*/
2814
2815/*!
2816 \fn qulonglong &QVariant::asULongLong()
2817
2818 Use toULongLong() instead.
2819*/
2820
2821/*!
2822 \fn bool &QVariant::asBool()
2823
2824 Use toBool() instead.
2825*/
2826
2827/*!
2828 \fn double &QVariant::asDouble()
2829
2830 Use toDouble() instead.
2831*/
2832
2833/*!
2834 \fn QByteArray &QVariant::asByteArray()
2835
2836 Use toByteArray() instead.
2837*/
2838
2839/*!
2840 \fn QBitArray &QVariant::asBitArray()
2841
2842 Use toBitArray() instead.
2843*/
2844
2845/*!
2846 \fn QString &QVariant::asString()
2847
2848 Use toString() instead.
2849*/
2850
2851/*!
2852 \fn QStringList &QVariant::asStringList()
2853
2854 Use toStringList() instead.
2855*/
2856
2857/*!
2858 \fn QDate &QVariant::asDate()
2859
2860 Use toDate() instead.
2861*/
2862
2863/*!
2864 \fn QTime &QVariant::asTime()
2865
2866 Use toTime() instead.
2867*/
2868
2869/*!
2870 \fn QDateTime &QVariant::asDateTime()
2871
2872 Use toDateTime() instead.
2873*/
2874
2875/*!
2876 \fn QList<QVariant> &QVariant::asList()
2877
2878 Use toList() instead.
2879*/
2880
2881/*!
2882 \fn QMap<QString, QVariant> &QVariant::asMap()
2883
2884 Use toMap() instead.
2885*/
2886
2887/*!
2888 \fn QVariant::QVariant(bool b, int dummy)
2889
2890 Use the QVariant(bool) constructor instead.
2891
2892*/
2893
2894/*!
2895 \fn const QByteArray QVariant::toCString() const
2896
2897 Use toByteArray() instead.
2898*/
2899
2900/*!
2901 \fn QByteArray &QVariant::asCString()
2902
2903 Use toByteArray() instead.
2904*/
2905
2906/*!
2907 \fn QPoint &QVariant::asPoint()
2908
2909 Use toPoint() instead.
2910 */
2911
2912/*!
2913 \fn QRect &QVariant::asRect()
2914
2915 Use toRect() instead.
2916 */
2917
2918/*!
2919 \fn QSize &QVariant::asSize()
2920
2921 Use toSize() instead.
2922 */
2923
2924/*! \fn void QVariant::setValue(const T &value)
2925
2926 Stores a copy of \a value. If \c{T} is a type that QVariant
2927 doesn't support, QMetaType is used to store the value. A compile
2928 error will occur if QMetaType doesn't handle the type.
2929
2930 Example:
2931
2932 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 4
2933
2934 \warning This function is not available with MSVC 6. Use
2935 qVariantSetValue() instead if you need to support that version of
2936 the compiler.
2937
2938 \sa value(), fromValue(), canConvert()
2939 */
2940
2941/*! \fn T QVariant::value() const
2942
2943 Returns the stored value converted to the template type \c{T}.
2944 Call canConvert() to find out whether a type can be converted.
2945 If the value cannot be converted, \l{default-constructed value}
2946 will be returned.
2947
2948 If the type \c{T} is supported by QVariant, this function behaves
2949 exactly as toString(), toInt() etc.
2950
2951 Example:
2952
2953 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 5
2954
2955 \warning This function is not available with MSVC 6. Use
2956 qVariantValue() or qvariant_cast() instead if you need to support
2957 that version of the compiler.
2958
2959 \sa setValue(), fromValue(), canConvert()
2960*/
2961
2962/*! \fn bool QVariant::canConvert() const
2963
2964 Returns true if the variant can be converted to the template type \c{T},
2965 otherwise false.
2966
2967 Example:
2968
2969 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 6
2970
2971 \warning This function is not available with MSVC 6. Use
2972 qVariantCanConvert() instead if you need to support that version
2973 of the compiler.
2974
2975 \sa convert()
2976*/
2977
2978/*! \fn static QVariant QVariant::fromValue(const T &value)
2979
2980 Returns a QVariant containing a copy of \a value. Behaves
2981 exactly like setValue() otherwise.
2982
2983 Example:
2984
2985 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 7
2986
2987 \note If you are working with custom types, you should use
2988 the Q_DECLARE_METATYPE() macro to register your custom type.
2989
2990 \warning This function is not available with MSVC 6. Use
2991 qVariantFromValue() instead if you need to support that version
2992 of the compiler.
2993
2994 \sa setValue(), value()
2995*/
2996
2997/*!
2998 \fn QVariant qVariantFromValue(const T &value)
2999 \relates QVariant
3000
3001 Returns a variant containing a copy of the given \a value
3002 with template type \c{T}.
3003
3004 This function is equivalent to QVariant::fromValue(\a value). It
3005 is provided as a work-around for MSVC 6, which doesn't support
3006 member template functions.
3007
3008 For example, a QObject pointer can be stored in a variant with the
3009 following code:
3010
3011 \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 8
3012
3013 \sa QVariant::fromValue()
3014*/
3015
3016/*! \fn void qVariantSetValue(QVariant &variant, const T &value)
3017 \relates QVariant
3018
3019 Sets the contents of the given \a variant to a copy of the
3020 \a value with the specified template type \c{T}.
3021
3022 This function is equivalent to QVariant::setValue(\a value). It
3023 is provided as a work-around for MSVC 6, which doesn't support
3024 member template functions.
3025
3026 \sa QVariant::setValue()
3027*/
3028
3029/*!
3030 \fn T qvariant_cast(const QVariant &value)
3031 \relates QVariant
3032
3033 Returns the given \a value converted to the template type \c{T}.
3034
3035 This function is equivalent to qVariantValue().
3036
3037 \sa qVariantValue(), QVariant::value()
3038*/
3039
3040/*! \fn T qVariantValue(const QVariant &value)
3041 \relates QVariant
3042
3043 Returns the given \a value converted to the template type \c{T}.
3044
3045 This function is equivalent to
3046 \l{QVariant::value()}{QVariant::value}<T>(\a value). It is
3047 provided as a work-around for MSVC 6, which doesn't support
3048 member template functions.
3049
3050 \sa QVariant::value(), qvariant_cast()
3051*/
3052
3053/*! \fn bool qVariantCanConvert(const QVariant &value)
3054 \relates QVariant
3055
3056 Returns true if the given \a value can be converted to the
3057 template type specified; otherwise returns false.
3058
3059 This function is equivalent to QVariant::canConvert(\a value). It
3060 is provided as a work-around for MSVC 6, which doesn't support
3061 member template functions.
3062
3063 \sa QVariant::canConvert()
3064*/
3065
3066/*!
3067 \typedef QVariantList
3068 \relates QVariant
3069
3070 Synonym for QList<QVariant>.
3071*/
3072
3073/*!
3074 \typedef QVariantMap
3075 \relates QVariant
3076
3077 Synonym for QMap<QString, QVariant>.
3078*/
3079
3080/*!
3081 \typedef QVariantHash
3082 \relates QVariant
3083 \since 4.5
3084
3085 Synonym for QHash<QString, QVariant>.
3086*/
3087
3088/*!
3089 \typedef QVariant::DataPtr
3090 \internal
3091*/
3092
3093/*!
3094 \fn DataPtr &QVariant::data_ptr()
3095 \internal
3096*/
3097
3098QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.