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

Last change on this file since 1168 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

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