Qt 4.8
qvariant.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
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 
67 
68 #ifndef DBL_DIG
69 # define DBL_DIG 10
70 #endif
71 #ifndef FLT_DIG
72 # define FLT_DIG 6
73 #endif
74 
75 static 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;
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 #ifndef QT_BOOTSTRAPPED
140  case QVariant::Url:
141  v_construct<QUrl>(x, copy);
142  break;
143 #endif
144  case QVariant::Locale:
145  v_construct<QLocale>(x, copy);
146  break;
147 #ifndef QT_NO_REGEXP
148  case QVariant::RegExp:
149  v_construct<QRegExp>(x, copy);
150  break;
151 #endif
152 #ifndef QT_BOOTSTRAPPED
154  v_construct<QEasingCurve>(x, copy);
155  break;
156 #endif
157  case QVariant::Int:
158  x->data.i = copy ? *static_cast<const int *>(copy) : 0;
159  break;
160  case QVariant::UInt:
161  x->data.u = copy ? *static_cast<const uint *>(copy) : 0u;
162  break;
163  case QVariant::Bool:
164  x->data.b = copy ? *static_cast<const bool *>(copy) : false;
165  break;
166  case QVariant::Double:
167  x->data.d = copy ? *static_cast<const double*>(copy) : 0.0;
168  break;
169  case QMetaType::Float:
170  x->data.f = copy ? *static_cast<const float*>(copy) : 0.0f;
171  break;
173  x->data.o = copy ? *static_cast<QObject *const*>(copy) : 0;
174  break;
175  case QVariant::LongLong:
176  x->data.ll = copy ? *static_cast<const qlonglong *>(copy) : Q_INT64_C(0);
177  break;
178  case QVariant::ULongLong:
179  x->data.ull = copy ? *static_cast<const qulonglong *>(copy) : Q_UINT64_C(0);
180  break;
181  case QVariant::Invalid:
182  case QVariant::UserType:
183  break;
184  default:
185  void *ptr = QMetaType::construct(x->type, copy);
186  if (!ptr) {
187  x->type = QVariant::Invalid;
188  } else {
189  x->is_shared = true;
190  x->data.shared = new QVariant::PrivateShared(ptr);
191  }
192  break;
193  }
194  x->is_null = !copy;
195 }
196 
197 static void clear(QVariant::Private *d)
198 {
199  switch (d->type) {
200  case QVariant::String:
201  v_clear<QString>(d);
202  break;
203  case QVariant::Char:
204  v_clear<QChar>(d);
205  break;
207  v_clear<QStringList>(d);
208  break;
209  case QVariant::Map:
210  v_clear<QVariantMap>(d);
211  break;
212  case QVariant::Hash:
213  v_clear<QVariantHash>(d);
214  break;
215  case QVariant::List:
216  v_clear<QVariantList>(d);
217  break;
218  case QVariant::Date:
219  v_clear<QDate>(d);
220  break;
221  case QVariant::Time:
222  v_clear<QTime>(d);
223  break;
224  case QVariant::DateTime:
225  v_clear<QDateTime>(d);
226  break;
227  case QVariant::ByteArray:
228  v_clear<QByteArray>(d);
229  break;
230  case QVariant::BitArray:
231  v_clear<QBitArray>(d);
232  break;
233 #ifndef QT_NO_GEOM_VARIANT
234  case QVariant::Point:
235  v_clear<QPoint>(d);
236  break;
237  case QVariant::PointF:
238  v_clear<QPointF>(d);
239  break;
240  case QVariant::Size:
241  v_clear<QSize>(d);
242  break;
243  case QVariant::SizeF:
244  v_clear<QSizeF>(d);
245  break;
246  case QVariant::Rect:
247  v_clear<QRect>(d);
248  break;
249  case QVariant::LineF:
250  v_clear<QLineF>(d);
251  break;
252  case QVariant::Line:
253  v_clear<QLine>(d);
254  break;
255  case QVariant::RectF:
256  v_clear<QRectF>(d);
257  break;
258 #endif
259 #ifndef QT_BOOTSTRAPPED
260  case QVariant::Url:
261  v_clear<QUrl>(d);
262  break;
263 #endif
264  case QVariant::Locale:
265  v_clear<QLocale>(d);
266  break;
267 #ifndef QT_NO_REGEXP
268  case QVariant::RegExp:
269  v_clear<QRegExp>(d);
270  break;
271 #endif
272 #ifndef QT_BOOTSTRAPPED
274  v_clear<QEasingCurve>(d);
275  break;
276 #endif
277  case QVariant::LongLong:
278  case QVariant::ULongLong:
279  case QVariant::Double:
280  case QMetaType::Float:
282  break;
283  case QVariant::Invalid:
284  case QVariant::UserType:
285  case QVariant::Int:
286  case QVariant::UInt:
287  case QVariant::Bool:
288  break;
289  default:
290  QMetaType::destroy(d->type, d->data.shared->ptr);
291  delete d->data.shared;
292  break;
293  }
294 
295  d->type = QVariant::Invalid;
296  d->is_null = true;
297  d->is_shared = false;
298 }
299 
300 static bool isNull(const QVariant::Private *d)
301 {
302  switch(d->type) {
303  case QVariant::String:
304  return v_cast<QString>(d)->isNull();
305  case QVariant::Char:
306  return v_cast<QChar>(d)->isNull();
307  case QVariant::Date:
308  return v_cast<QDate>(d)->isNull();
309  case QVariant::Time:
310  return v_cast<QTime>(d)->isNull();
311  case QVariant::DateTime:
312  return v_cast<QDateTime>(d)->isNull();
313  case QVariant::ByteArray:
314  return v_cast<QByteArray>(d)->isNull();
315  case QVariant::BitArray:
316  return v_cast<QBitArray>(d)->isNull();
317 #ifndef QT_NO_GEOM_VARIANT
318  case QVariant::Size:
319  return v_cast<QSize>(d)->isNull();
320  case QVariant::SizeF:
321  return v_cast<QSizeF>(d)->isNull();
322  case QVariant::Rect:
323  return v_cast<QRect>(d)->isNull();
324  case QVariant::Line:
325  return v_cast<QLine>(d)->isNull();
326  case QVariant::LineF:
327  return v_cast<QLineF>(d)->isNull();
328  case QVariant::RectF:
329  return v_cast<QRectF>(d)->isNull();
330  case QVariant::Point:
331  return v_cast<QPoint>(d)->isNull();
332  case QVariant::PointF:
333  return v_cast<QPointF>(d)->isNull();
334 #endif
335 #ifndef QT_BOOTSTRAPPED
337  case QVariant::Url:
338 #endif
339  case QVariant::Locale:
340  case QVariant::RegExp:
342  case QVariant::Map:
343  case QVariant::Hash:
344  case QVariant::List:
345  case QVariant::Invalid:
346  case QVariant::UserType:
347  case QVariant::Int:
348  case QVariant::UInt:
349  case QVariant::LongLong:
350  case QVariant::ULongLong:
351  case QVariant::Bool:
352  case QVariant::Double:
353  case QMetaType::Float:
355  break;
356  }
357  return d->is_null;
358 }
359 
360 /*
361  \internal
362  \since 4.4
363 
364  We cannot use v_cast() for QMetaType's numeric types because they're smaller than QVariant::Private::Data,
365  which in turns makes v_cast() believe the value is stored in d->data.c. But
366  it's not, since we're a QMetaType type.
367  */
368 template<typename T>
369 inline bool compareNumericMetaType(const QVariant::Private *const a, const QVariant::Private *const b)
370 {
371  return *static_cast<const T *>(a->data.shared->ptr) == *static_cast<const T *>(b->data.shared->ptr);
372 }
373 
383 static bool compare(const QVariant::Private *a, const QVariant::Private *b)
384 {
385  switch(a->type) {
386  case QVariant::List:
387  return *v_cast<QVariantList>(a) == *v_cast<QVariantList>(b);
388  case QVariant::Map: {
389  const QVariantMap *m1 = v_cast<QVariantMap>(a);
390  const QVariantMap *m2 = v_cast<QVariantMap>(b);
391  if (m1->count() != m2->count())
392  return false;
394  QVariantMap::ConstIterator it2 = m2->constBegin();
395  while (it != m1->constEnd()) {
396  if (*it != *it2 || it.key() != it2.key())
397  return false;
398  ++it;
399  ++it2;
400  }
401  return true;
402  }
403  case QVariant::Hash:
404  return *v_cast<QVariantHash>(a) == *v_cast<QVariantHash>(b);
405  case QVariant::String:
406  return *v_cast<QString>(a) == *v_cast<QString>(b);
407  case QVariant::Char:
408  return *v_cast<QChar>(a) == *v_cast<QChar>(b);
410  return *v_cast<QStringList>(a) == *v_cast<QStringList>(b);
411 #ifndef QT_NO_GEOM_VARIANT
412  case QVariant::Size:
413  return *v_cast<QSize>(a) == *v_cast<QSize>(b);
414  case QVariant::SizeF:
415  return *v_cast<QSizeF>(a) == *v_cast<QSizeF>(b);
416  case QVariant::Rect:
417  return *v_cast<QRect>(a) == *v_cast<QRect>(b);
418  case QVariant::Line:
419  return *v_cast<QLine>(a) == *v_cast<QLine>(b);
420  case QVariant::LineF:
421  return *v_cast<QLineF>(a) == *v_cast<QLineF>(b);
422  case QVariant::RectF:
423  return *v_cast<QRectF>(a) == *v_cast<QRectF>(b);
424  case QVariant::Point:
425  return *v_cast<QPoint>(a) == *v_cast<QPoint>(b);
426  case QVariant::PointF:
427  return *v_cast<QPointF>(a) == *v_cast<QPointF>(b);
428 #endif
429 #ifndef QT_BOOTSTRAPPED
430  case QVariant::Url:
431  return *v_cast<QUrl>(a) == *v_cast<QUrl>(b);
432 #endif
433  case QVariant::Locale:
434  return *v_cast<QLocale>(a) == *v_cast<QLocale>(b);
435 #ifndef QT_NO_REGEXP
436  case QVariant::RegExp:
437  return *v_cast<QRegExp>(a) == *v_cast<QRegExp>(b);
438 #endif
439  case QVariant::Int:
440  return a->data.i == b->data.i;
441  case QVariant::UInt:
442  return a->data.u == b->data.u;
443  case QVariant::LongLong:
444  return a->data.ll == b->data.ll;
445  case QVariant::ULongLong:
446  return a->data.ull == b->data.ull;
447  case QVariant::Bool:
448  return a->data.b == b->data.b;
449  case QVariant::Double:
450  return a->data.d == b->data.d;
451  case QMetaType::Float:
452  return a->data.f == b->data.f;
454  return a->data.o == b->data.o;
455  case QVariant::Date:
456  return *v_cast<QDate>(a) == *v_cast<QDate>(b);
457  case QVariant::Time:
458  return *v_cast<QTime>(a) == *v_cast<QTime>(b);
459  case QVariant::DateTime:
460  return *v_cast<QDateTime>(a) == *v_cast<QDateTime>(b);
461 #ifndef QT_BOOTSTRAPPED
463  return *v_cast<QEasingCurve>(a) == *v_cast<QEasingCurve>(b);
464 #endif
465  case QVariant::ByteArray:
466  return *v_cast<QByteArray>(a) == *v_cast<QByteArray>(b);
467  case QVariant::BitArray:
468  return *v_cast<QBitArray>(a) == *v_cast<QBitArray>(b);
469  case QVariant::Invalid:
470  return true;
471  case QMetaType::Long:
472  return compareNumericMetaType<long>(a, b);
473  case QMetaType::ULong:
474  return compareNumericMetaType<ulong>(a, b);
475  case QMetaType::Short:
476  return compareNumericMetaType<short>(a, b);
477  case QMetaType::UShort:
478  return compareNumericMetaType<ushort>(a, b);
479  case QMetaType::UChar:
480  return compareNumericMetaType<uchar>(a, b);
481  case QMetaType::Char:
482  return compareNumericMetaType<char>(a, b);
483  default:
484  break;
485  }
486  if (!QMetaType::isRegistered(a->type))
487  qFatal("QVariant::compare: type %d unknown to QVariant.", a->type);
488 
489  const void *a_ptr = a->is_shared ? a->data.shared->ptr : &(a->data.ptr);
490  const void *b_ptr = b->is_shared ? b->data.shared->ptr : &(b->data.ptr);
491 
492  /* The reason we cannot place this test in a case branch above for the types
493  * QMetaType::VoidStar, QMetaType::QObjectStar and so forth, is that it wouldn't include
494  * user defined pointer types. */
495  const char *const typeName = QMetaType::typeName(a->type);
496  uint typeNameLen = qstrlen(typeName);
497  if (typeNameLen > 0 && typeName[typeNameLen - 1] == '*')
498  return *static_cast<void *const *>(a_ptr) == *static_cast<void *const *>(b_ptr);
499 
500  if (a->is_null && b->is_null)
501  return true;
502 
503  return a_ptr == b_ptr;
504 }
505 
509 static qlonglong qMetaTypeNumber(const QVariant::Private *d)
510 {
511  switch (d->type) {
512  case QMetaType::Int:
513  return d->data.i;
514  case QMetaType::LongLong:
515  return d->data.ll;
516  case QMetaType::Char:
517  return qlonglong(*static_cast<signed char *>(d->data.shared->ptr));
518  case QMetaType::Short:
519  return qlonglong(*static_cast<short *>(d->data.shared->ptr));
520  case QMetaType::Long:
521  return qlonglong(*static_cast<long *>(d->data.shared->ptr));
522  case QMetaType::Float:
523  return qRound64(d->data.f);
524  case QVariant::Double:
525  return qRound64(d->data.d);
526  }
527  Q_ASSERT(false);
528  return 0;
529 }
530 
531 static qulonglong qMetaTypeUNumber(const QVariant::Private *d)
532 {
533  switch (d->type) {
534  case QVariant::UInt:
535  return d->data.u;
536  case QVariant::ULongLong:
537  return d->data.ull;
538  case QMetaType::UChar:
539  return qulonglong(*static_cast<unsigned char *>(d->data.shared->ptr));
540  case QMetaType::UShort:
541  return qulonglong(*static_cast<ushort *>(d->data.shared->ptr));
542  case QMetaType::ULong:
543  return qulonglong(*static_cast<ulong *>(d->data.shared->ptr));
544  }
545  Q_ASSERT(false);
546  return 0;
547 }
548 
549 static qlonglong qConvertToNumber(const QVariant::Private *d, bool *ok)
550 {
551  *ok = true;
552 
553  switch (uint(d->type)) {
554  case QVariant::String:
555  return v_cast<QString>(d)->toLongLong(ok);
556  case QVariant::Char:
557  return v_cast<QChar>(d)->unicode();
558  case QVariant::ByteArray:
559  return v_cast<QByteArray>(d)->toLongLong(ok);
560  case QVariant::Bool:
561  return qlonglong(d->data.b);
562  case QVariant::Double:
563  case QVariant::Int:
564  case QMetaType::Char:
565  case QMetaType::Short:
566  case QMetaType::Long:
567  case QMetaType::Float:
568  case QMetaType::LongLong:
569  return qMetaTypeNumber(d);
570  case QVariant::ULongLong:
571  case QVariant::UInt:
572  case QMetaType::UChar:
573  case QMetaType::UShort:
574  case QMetaType::ULong:
575  return qlonglong(qMetaTypeUNumber(d));
576  }
577 
578  *ok = false;
579  return Q_INT64_C(0);
580 }
581 
582 static qulonglong qConvertToUnsignedNumber(const QVariant::Private *d, bool *ok)
583 {
584  *ok = true;
585 
586  switch (uint(d->type)) {
587  case QVariant::String:
588  return v_cast<QString>(d)->toULongLong(ok);
589  case QVariant::Char:
590  return v_cast<QChar>(d)->unicode();
591  case QVariant::ByteArray:
592  return v_cast<QByteArray>(d)->toULongLong(ok);
593  case QVariant::Bool:
594  return qulonglong(d->data.b);
595  case QVariant::Double:
596  case QVariant::Int:
597  case QMetaType::Char:
598  case QMetaType::Short:
599  case QMetaType::Long:
600  case QMetaType::Float:
601  case QMetaType::LongLong:
602  return qulonglong(qMetaTypeNumber(d));
603  case QVariant::ULongLong:
604  case QVariant::UInt:
605  case QMetaType::UChar:
606  case QMetaType::UShort:
607  case QMetaType::ULong:
608  return qMetaTypeUNumber(d);
609  }
610 
611  *ok = false;
612  return Q_UINT64_C(0);
613 }
614 
615 template<typename TInput, typename LiteralWrapper>
616 inline bool qt_convertToBool(const QVariant::Private *const d)
617 {
618  TInput str = v_cast<TInput>(d)->toLower();
619  return !(str == LiteralWrapper("0") || str == LiteralWrapper("false") || str.isEmpty());
620 }
621 
627 static bool convert(const QVariant::Private *d, QVariant::Type t, void *result, bool *ok)
628 {
629  Q_ASSERT(d->type != uint(t));
630  Q_ASSERT(result);
631 
632  bool dummy;
633  if (!ok)
634  ok = &dummy;
635 
636  switch (uint(t)) {
637 #ifndef QT_BOOTSTRAPPED
638  case QVariant::Url:
639  switch (d->type) {
640  case QVariant::String:
641  *static_cast<QUrl *>(result) = QUrl(*v_cast<QString>(d));
642  break;
643  default:
644  return false;
645  }
646  break;
647 #endif
648  case QVariant::String: {
649  QString *str = static_cast<QString *>(result);
650  switch (d->type) {
651  case QVariant::Char:
652  *str = QString(*v_cast<QChar>(d));
653  break;
654  case QMetaType::Char:
655  case QMetaType::UChar:
656  *str = QChar::fromAscii(*static_cast<char *>(d->data.shared->ptr));
657  break;
658  case QMetaType::Short:
659  case QMetaType::Long:
660  case QVariant::Int:
661  case QVariant::LongLong:
662  *str = QString::number(qMetaTypeNumber(d));
663  break;
664  case QVariant::UInt:
665  case QVariant::ULongLong:
666  case QMetaType::UShort:
667  case QMetaType::ULong:
669  break;
670  case QMetaType::Float:
671  *str = QString::number(d->data.f, 'g', FLT_DIG);
672  break;
673  case QVariant::Double:
674  *str = QString::number(d->data.d, 'g', DBL_DIG);
675  break;
676 #if !defined(QT_NO_DATESTRING)
677  case QVariant::Date:
678  *str = v_cast<QDate>(d)->toString(Qt::ISODate);
679  break;
680  case QVariant::Time:
681  *str = v_cast<QTime>(d)->toString(Qt::ISODate);
682  break;
683  case QVariant::DateTime:
685  break;
686 #endif
687  case QVariant::Bool:
688  *str = QLatin1String(d->data.b ? "true" : "false");
689  break;
690  case QVariant::ByteArray:
691  *str = QString::fromAscii(v_cast<QByteArray>(d)->constData());
692  break;
694  if (v_cast<QStringList>(d)->count() == 1)
695  *str = v_cast<QStringList>(d)->at(0);
696  break;
697 #ifndef QT_BOOTSTRAPPED
698  case QVariant::Url:
699  *str = v_cast<QUrl>(d)->toString();
700  break;
701 #endif
702  default:
703  return false;
704  }
705  break;
706  }
707  case QVariant::Char: {
708  QChar *c = static_cast<QChar *>(result);
709  switch (d->type) {
710  case QVariant::Int:
711  case QVariant::LongLong:
712  case QMetaType::Char:
713  case QMetaType::Short:
714  case QMetaType::Long:
715  case QMetaType::Float:
716  *c = QChar(ushort(qMetaTypeNumber(d)));
717  break;
718  case QVariant::UInt:
719  case QVariant::ULongLong:
720  case QMetaType::UChar:
721  case QMetaType::UShort:
722  case QMetaType::ULong:
723  *c = QChar(ushort(qMetaTypeUNumber(d)));
724  break;
725  default:
726  return false;
727  }
728  break;
729  }
730 #ifndef QT_NO_GEOM_VARIANT
731  case QVariant::Size: {
732  QSize *s = static_cast<QSize *>(result);
733  switch (d->type) {
734  case QVariant::SizeF:
735  *s = v_cast<QSizeF>(d)->toSize();
736  break;
737  default:
738  return false;
739  }
740  break;
741  }
742 
743  case QVariant::SizeF: {
744  QSizeF *s = static_cast<QSizeF *>(result);
745  switch (d->type) {
746  case QVariant::Size:
747  *s = QSizeF(*(v_cast<QSize>(d)));
748  break;
749  default:
750  return false;
751  }
752  break;
753  }
754 
755  case QVariant::Line: {
756  QLine *s = static_cast<QLine *>(result);
757  switch (d->type) {
758  case QVariant::LineF:
759  *s = v_cast<QLineF>(d)->toLine();
760  break;
761  default:
762  return false;
763  }
764  break;
765  }
766 
767  case QVariant::LineF: {
768  QLineF *s = static_cast<QLineF *>(result);
769  switch (d->type) {
770  case QVariant::Line:
771  *s = QLineF(*(v_cast<QLine>(d)));
772  break;
773  default:
774  return false;
775  }
776  break;
777  }
778 #endif
780  if (d->type == QVariant::List) {
781  QStringList *slst = static_cast<QStringList *>(result);
782  const QVariantList *list = v_cast<QVariantList >(d);
783  for (int i = 0; i < list->size(); ++i)
784  slst->append(list->at(i).toString());
785  } else if (d->type == QVariant::String) {
786  QStringList *slst = static_cast<QStringList *>(result);
787  *slst = QStringList(*v_cast<QString>(d));
788  } else {
789  return false;
790  }
791  break;
792  case QVariant::Date: {
793  QDate *dt = static_cast<QDate *>(result);
794  if (d->type == QVariant::DateTime)
795  *dt = v_cast<QDateTime>(d)->date();
796 #ifndef QT_NO_DATESTRING
797  else if (d->type == QVariant::String)
798  *dt = QDate::fromString(*v_cast<QString>(d), Qt::ISODate);
799 #endif
800  else
801  return false;
802 
803  return dt->isValid();
804  }
805  case QVariant::Time: {
806  QTime *t = static_cast<QTime *>(result);
807  switch (d->type) {
808  case QVariant::DateTime:
809  *t = v_cast<QDateTime>(d)->time();
810  break;
811 #ifndef QT_NO_DATESTRING
812  case QVariant::String:
813  *t = QTime::fromString(*v_cast<QString>(d), Qt::ISODate);
814  break;
815 #endif
816  default:
817  return false;
818  }
819  return t->isValid();
820  }
821  case QVariant::DateTime: {
822  QDateTime *dt = static_cast<QDateTime *>(result);
823  switch (d->type) {
824 #ifndef QT_NO_DATESTRING
825  case QVariant::String:
826  *dt = QDateTime::fromString(*v_cast<QString>(d), Qt::ISODate);
827  break;
828 #endif
829  case QVariant::Date:
830  *dt = QDateTime(*v_cast<QDate>(d));
831  break;
832  default:
833  return false;
834  }
835  return dt->isValid();
836  }
837  case QVariant::ByteArray: {
838  QByteArray *ba = static_cast<QByteArray *>(result);
839  switch (d->type) {
840  case QVariant::String:
841  *ba = v_cast<QString>(d)->toAscii();
842  break;
843  case QVariant::Double:
844  *ba = QByteArray::number(d->data.d, 'g', DBL_DIG);
845  break;
846  case QMetaType::Float:
847  *ba = QByteArray::number(d->data.f, 'g', FLT_DIG);
848  break;
849  case QMetaType::Char:
850  case QMetaType::UChar:
851  *ba = QByteArray(1, *static_cast<char *>(d->data.shared->ptr));
852  break;
853  case QVariant::Int:
854  case QVariant::LongLong:
855  case QMetaType::Short:
856  case QMetaType::Long:
858  break;
859  case QVariant::UInt:
860  case QVariant::ULongLong:
861  case QMetaType::UShort:
862  case QMetaType::ULong:
864  break;
865  case QVariant::Bool:
866  *ba = QByteArray(d->data.b ? "true" : "false");
867  break;
868  default:
869  return false;
870  }
871  }
872  break;
873  case QMetaType::Short:
874  *static_cast<short *>(result) = short(qConvertToNumber(d, ok));
875  return *ok;
876  case QMetaType::Long:
877  *static_cast<long *>(result) = long(qConvertToNumber(d, ok));
878  return *ok;
879  case QMetaType::UShort:
880  *static_cast<ushort *>(result) = ushort(qConvertToUnsignedNumber(d, ok));
881  return *ok;
882  case QMetaType::ULong:
883  *static_cast<ulong *>(result) = ulong(qConvertToUnsignedNumber(d, ok));
884  return *ok;
885  case QVariant::Int:
886  *static_cast<int *>(result) = int(qConvertToNumber(d, ok));
887  return *ok;
888  case QVariant::UInt:
889  *static_cast<uint *>(result) = uint(qConvertToUnsignedNumber(d, ok));
890  return *ok;
891  case QVariant::LongLong:
892  *static_cast<qlonglong *>(result) = qConvertToNumber(d, ok);
893  return *ok;
894  case QVariant::ULongLong: {
895  *static_cast<qulonglong *>(result) = qConvertToUnsignedNumber(d, ok);
896  return *ok;
897  }
898  case QMetaType::UChar: {
899  *static_cast<uchar *>(result) = qConvertToUnsignedNumber(d, ok);
900  return *ok;
901  }
902  case QVariant::Bool: {
903  bool *b = static_cast<bool *>(result);
904  switch(d->type) {
905  case QVariant::ByteArray:
906  *b = qt_convertToBool<QByteArray, QByteArray>(d);
907  break;
908  case QVariant::String:
909  *b = qt_convertToBool<QString, QLatin1String>(d);
910  break;
911  case QVariant::Char:
912  *b = !v_cast<QChar>(d)->isNull();
913  break;
914  case QVariant::Double:
915  case QVariant::Int:
916  case QVariant::LongLong:
917  case QMetaType::Char:
918  case QMetaType::Short:
919  case QMetaType::Long:
920  case QMetaType::Float:
921  *b = qMetaTypeNumber(d) != Q_INT64_C(0);
922  break;
923  case QVariant::UInt:
924  case QVariant::ULongLong:
925  case QMetaType::UChar:
926  case QMetaType::UShort:
927  case QMetaType::ULong:
928  *b = qMetaTypeUNumber(d) != Q_UINT64_C(0);
929  break;
930  default:
931  *b = false;
932  return false;
933  }
934  break;
935  }
936  case QVariant::Double: {
937  double *f = static_cast<double *>(result);
938  switch (d->type) {
939  case QVariant::String:
940  *f = v_cast<QString>(d)->toDouble(ok);
941  break;
942  case QVariant::ByteArray:
943  *f = v_cast<QByteArray>(d)->toDouble(ok);
944  break;
945  case QVariant::Bool:
946  *f = double(d->data.b);
947  break;
948  case QMetaType::Float:
949  *f = double(d->data.f);
950  break;
951  case QVariant::LongLong:
952  case QVariant::Int:
953  case QMetaType::Char:
954  case QMetaType::Short:
955  case QMetaType::Long:
956  *f = double(qMetaTypeNumber(d));
957  break;
958  case QVariant::UInt:
959  case QVariant::ULongLong:
960  case QMetaType::UChar:
961  case QMetaType::UShort:
962  case QMetaType::ULong:
963  *f = double(qMetaTypeUNumber(d));
964  break;
965  default:
966  *f = 0.0;
967  return false;
968  }
969  break;
970  }
971  case QMetaType::Float: {
972  float *f = static_cast<float *>(result);
973  switch (d->type) {
974  case QVariant::String:
975  *f = v_cast<QString>(d)->toFloat(ok);
976  break;
977  case QVariant::ByteArray:
978  *f = v_cast<QByteArray>(d)->toFloat(ok);
979  break;
980  case QVariant::Bool:
981  *f = float(d->data.b);
982  break;
983  case QVariant::Double:
984  *f = float(d->data.d);
985  break;
986  case QVariant::LongLong:
987  case QVariant::Int:
988  case QMetaType::Char:
989  case QMetaType::Short:
990  case QMetaType::Long:
991  *f = float(qMetaTypeNumber(d));
992  break;
993  case QVariant::UInt:
994  case QVariant::ULongLong:
995  case QMetaType::UChar:
996  case QMetaType::UShort:
997  case QMetaType::ULong:
998  *f = float(qMetaTypeUNumber(d));
999  break;
1000  default:
1001  *f = 0.0f;
1002  return false;
1003  }
1004  break;
1005  }
1006  case QVariant::List:
1007  if (d->type == QVariant::StringList) {
1008  QVariantList *lst = static_cast<QVariantList *>(result);
1009  const QStringList *slist = v_cast<QStringList>(d);
1010  for (int i = 0; i < slist->size(); ++i)
1011  lst->append(QVariant(slist->at(i)));
1012  } else if (qstrcmp(QMetaType::typeName(d->type), "QList<QVariant>") == 0) {
1013  *static_cast<QVariantList *>(result) =
1014  *static_cast<QList<QVariant> *>(d->data.shared->ptr);
1015  } else {
1016  return false;
1017  }
1018  break;
1019  case QVariant::Map:
1020  if (qstrcmp(QMetaType::typeName(d->type), "QMap<QString, QVariant>") == 0) {
1021  *static_cast<QVariantMap *>(result) =
1022  *static_cast<QMap<QString, QVariant> *>(d->data.shared->ptr);
1023  } else {
1024  return false;
1025  }
1026  break;
1027  case QVariant::Hash:
1028  if (qstrcmp(QMetaType::typeName(d->type), "QHash<QString, QVariant>") == 0) {
1029  *static_cast<QVariantHash *>(result) =
1030  *static_cast<QHash<QString, QVariant> *>(d->data.shared->ptr);
1031  } else {
1032  return false;
1033  }
1034  break;
1035 #ifndef QT_NO_GEOM_VARIANT
1036  case QVariant::Rect:
1037  if (d->type == QVariant::RectF)
1038  *static_cast<QRect *>(result) = (v_cast<QRectF>(d))->toRect();
1039  else
1040  return false;
1041  break;
1042  case QVariant::RectF:
1043  if (d->type == QVariant::Rect)
1044  *static_cast<QRectF *>(result) = *v_cast<QRect>(d);
1045  else
1046  return false;
1047  break;
1048  case QVariant::PointF:
1049  if (d->type == QVariant::Point)
1050  *static_cast<QPointF *>(result) = *v_cast<QPoint>(d);
1051  else
1052  return false;
1053  break;
1054  case QVariant::Point:
1055  if (d->type == QVariant::PointF)
1056  *static_cast<QPoint *>(result) = (v_cast<QPointF>(d))->toPoint();
1057  else
1058  return false;
1059  break;
1060  case QMetaType::Char:
1061  {
1062  *static_cast<qint8 *>(result) = qint8(qConvertToNumber(d, ok));
1063  return *ok;
1064  }
1065 #endif
1066  default:
1067  return false;
1068  }
1069  return true;
1070 }
1071 
1072 #if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)
1073 static void streamDebug(QDebug dbg, const QVariant &v)
1074 {
1075  switch (v.userType()) {
1076  case QVariant::Int:
1077  dbg.nospace() << v.toInt();
1078  break;
1079  case QVariant::UInt:
1080  dbg.nospace() << v.toUInt();
1081  break;
1082  case QVariant::LongLong:
1083  dbg.nospace() << v.toLongLong();
1084  break;
1085  case QVariant::ULongLong:
1086  dbg.nospace() << v.toULongLong();
1087  break;
1088  case QMetaType::Float:
1089  dbg.nospace() << v.toFloat();
1090  break;
1092  dbg.nospace() << qvariant_cast<QObject *>(v);
1093  break;
1094  case QVariant::Double:
1095  dbg.nospace() << v.toDouble();
1096  break;
1097  case QVariant::Bool:
1098  dbg.nospace() << v.toBool();
1099  break;
1100  case QVariant::String:
1101  dbg.nospace() << v.toString();
1102  break;
1103  case QVariant::Char:
1104  dbg.nospace() << v.toChar();
1105  break;
1106  case QVariant::StringList:
1107  dbg.nospace() << v.toStringList();
1108  break;
1109  case QVariant::Map:
1110  dbg.nospace() << v.toMap();
1111  break;
1112  case QVariant::Hash:
1113  dbg.nospace() << v.toHash();
1114  break;
1115  case QVariant::List:
1116  dbg.nospace() << v.toList();
1117  break;
1118  case QVariant::Date:
1119  dbg.nospace() << v.toDate();
1120  break;
1121  case QVariant::Time:
1122  dbg.nospace() << v.toTime();
1123  break;
1124  case QVariant::DateTime:
1125  dbg.nospace() << v.toDateTime();
1126  break;
1127 #ifndef QT_BOOTSTRAPPED
1128  case QVariant::EasingCurve:
1129  dbg.nospace() << v.toEasingCurve();
1130  break;
1131 #endif
1132  case QVariant::ByteArray:
1133  dbg.nospace() << v.toByteArray();
1134  break;
1135 #ifndef QT_BOOTSTRAPPED
1136  case QVariant::Url:
1137  dbg.nospace() << v.toUrl();
1138  break;
1139 #endif
1140 #ifndef QT_NO_GEOM_VARIANT
1141  case QVariant::Point:
1142  dbg.nospace() << v.toPoint();
1143  break;
1144  case QVariant::PointF:
1145  dbg.nospace() << v.toPointF();
1146  break;
1147  case QVariant::Rect:
1148  dbg.nospace() << v.toRect();
1149  break;
1150  case QVariant::Size:
1151  dbg.nospace() << v.toSize();
1152  break;
1153  case QVariant::SizeF:
1154  dbg.nospace() << v.toSizeF();
1155  break;
1156  case QVariant::Line:
1157  dbg.nospace() << v.toLine();
1158  break;
1159  case QVariant::LineF:
1160  dbg.nospace() << v.toLineF();
1161  break;
1162  case QVariant::RectF:
1163  dbg.nospace() << v.toRectF();
1164  break;
1165 #endif
1166  case QVariant::BitArray:
1167  //dbg.nospace() << v.toBitArray();
1168  break;
1169  default:
1170  break;
1171  }
1172 }
1173 #endif
1174 
1175 const QVariant::Handler qt_kernel_variant_handler = {
1176  construct,
1177  clear,
1178  isNull,
1179 #ifndef QT_NO_DATASTREAM
1180  0,
1181  0,
1182 #endif
1183  compare,
1184  convert,
1185  0,
1186 #if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)
1187  streamDebug
1188 #else
1189  0
1190 #endif
1191 };
1192 
1193 Q_CORE_EXPORT const QVariant::Handler *qcoreVariantHandler()
1194 {
1195  return &qt_kernel_variant_handler;
1196 }
1197 
1198 
1199 const QVariant::Handler *QVariant::handler = &qt_kernel_variant_handler;
1200 
1407 void QVariant::create(int type, const void *copy)
1408 {
1409  d.type = type;
1410  handler->construct(&d, copy);
1411 }
1412 
1428 {
1429  if ((d.is_shared && !d.data.shared->ref.deref()) || (!d.is_shared && d.type > Char && d.type < UserType))
1430  handler->clear(&d);
1431 }
1432 
1444  : d(p.d)
1445 {
1446  if (d.is_shared) {
1447  d.data.shared->ref.ref();
1448  } else if (p.d.type > Char && p.d.type < QVariant::UserType) {
1449  handler->construct(&d, p.constData());
1450  d.is_null = p.d.is_null;
1451  }
1452 }
1453 
1454 #ifndef QT_NO_DATASTREAM
1455 
1459 {
1460  d.is_null = true;
1461  s >> *this;
1462 }
1463 #endif //QT_NO_DATASTREAM
1464 
1500 #ifndef QT_NO_CAST_FROM_ASCII
1501 QVariant::QVariant(const char *val)
1502 {
1503  QString s = QString::fromAscii(val);
1504  create(String, &s);
1505 }
1506 #endif
1507 
1789 { create(type, 0); }
1790 QVariant::QVariant(int typeOrUserType, const void *copy)
1791 { create(typeOrUserType, copy); d.is_null = false; }
1792 
1796 QVariant::QVariant(int typeOrUserType, const void *copy, uint flags)
1797 {
1798  if (flags) { //type is a pointer type
1799  d.type = typeOrUserType;
1800  d.data.ptr = *reinterpret_cast<void *const*>(copy);
1801  d.is_null = false;
1802  } else {
1803  create(typeOrUserType, copy);
1804  d.is_null = false;
1805  }
1806 }
1807 
1809 { d.is_null = false; d.type = Int; d.data.i = val; }
1811 { d.is_null = false; d.type = UInt; d.data.u = val; }
1813 { d.is_null = false; d.type = LongLong; d.data.ll = val; }
1815 { d.is_null = false; d.type = ULongLong; d.data.ull = val; }
1817 { d.is_null = false; d.type = Bool; d.data.b = val; }
1819 { d.is_null = false; d.type = Double; d.data.d = val; }
1820 
1822 { d.is_null = false; d.type = ByteArray; v_construct<QByteArray>(&d, val); }
1824 { d.is_null = false; d.type = BitArray; v_construct<QBitArray>(&d, val); }
1826 { d.is_null = false; d.type = String; v_construct<QString>(&d, val); }
1828 { d.is_null = false; d.type = Char; v_construct<QChar>(&d, val); }
1830 { QString str(val); d.is_null = false; d.type = String; v_construct<QString>(&d, str); }
1832 { d.is_null = false; d.type = StringList; v_construct<QStringList>(&d, val); }
1833 
1835 { d.is_null = false; d.type = Date; v_construct<QDate>(&d, val); }
1837 { d.is_null = false; d.type = Time; v_construct<QTime>(&d, val); }
1839 { d.is_null = false; d.type = DateTime; v_construct<QDateTime>(&d, val); }
1840 #ifndef QT_BOOTSTRAPPED
1842 { d.is_null = false; d.type = EasingCurve; v_construct<QEasingCurve>(&d, val); }
1843 #endif
1845 { d.is_null = false; d.type = List; v_construct<QVariantList>(&d, list); }
1847 { d.is_null = false; d.type = Map; v_construct<QVariantMap>(&d, map); }
1849 { d.is_null = false; d.type = Hash; v_construct<QVariantHash>(&d, hash); }
1850 #ifndef QT_NO_GEOM_VARIANT
1851 QVariant::QVariant(const QPoint &pt) { d.is_null = false; d.type = Point; v_construct<QPoint>(&d, pt); }
1852 QVariant::QVariant(const QPointF &pt) { d.is_null = false; d.type = PointF; v_construct<QPointF>(&d, pt); }
1853 QVariant::QVariant(const QRectF &r) { d.is_null = false; d.type = RectF; v_construct<QRectF>(&d, r); }
1854 QVariant::QVariant(const QLineF &l) { d.is_null = false; d.type = LineF; v_construct<QLineF>(&d, l); }
1855 QVariant::QVariant(const QLine &l) { d.is_null = false; d.type = Line; v_construct<QLine>(&d, l); }
1856 QVariant::QVariant(const QRect &r) { d.is_null = false; d.type = Rect; v_construct<QRect>(&d, r); }
1857 QVariant::QVariant(const QSize &s) { d.is_null = false; d.type = Size; v_construct<QSize>(&d, s); }
1858 QVariant::QVariant(const QSizeF &s) { d.is_null = false; d.type = SizeF; v_construct<QSizeF>(&d, s); }
1859 #endif
1860 #ifndef QT_BOOTSTRAPPED
1861 QVariant::QVariant(const QUrl &u) { d.is_null = false; d.type = Url; v_construct<QUrl>(&d, u); }
1862 #endif
1863 QVariant::QVariant(const QLocale &l) { d.is_null = false; d.type = Locale; v_construct<QLocale>(&d, l); }
1864 #ifndef QT_NO_REGEXP
1865 QVariant::QVariant(const QRegExp &regExp) { d.is_null = false; d.type = RegExp; v_construct<QRegExp>(&d, regExp); }
1866 #endif
1868 
1902 {
1903  return d.type >= QMetaType::User ? UserType : static_cast<Type>(d.type);
1904 }
1905 
1914 {
1915  return d.type;
1916 }
1917 
1922 {
1923  if (this == &variant)
1924  return *this;
1925 
1926  clear();
1927  if (variant.d.is_shared) {
1928  variant.d.data.shared->ref.ref();
1929  d = variant.d;
1930  } else if (variant.d.type > Char && variant.d.type < UserType) {
1931  d.type = variant.d.type;
1932  handler->construct(&d, variant.constData());
1933  d.is_null = variant.d.is_null;
1934  } else {
1935  d = variant.d;
1936  }
1937 
1938  return *this;
1939 }
1940 
1959 {
1960  if (!d.is_shared || d.data.shared->ref == 1)
1961  return;
1962 
1963  Private dd;
1964  dd.type = d.type;
1965  handler->construct(&dd, constData());
1966  if (!d.data.shared->ref.deref())
1967  handler->clear(&d);
1968  d.data.shared = dd.data.shared;
1969 }
1970 
1977 // ### Qt 5: change typeName()(and froends= to return a QString. Suggestion from Harald.
1984 const char *QVariant::typeName() const
1985 {
1986  return typeToName(Type(d.type));
1987 }
1988 
1994 {
1995  if ((d.is_shared && !d.data.shared->ref.deref()) || (!d.is_shared && d.type < UserType && d.type > Char))
1996  handler->clear(&d);
1997  d.type = Invalid;
1998  d.is_null = true;
1999  d.is_shared = false;
2000 }
2001 
2008 const char *QVariant::typeToName(Type typ)
2009 {
2010  if (typ == Invalid)
2011  return 0;
2012  if (typ == UserType)
2013  return "UserType";
2014 
2015  return QMetaType::typeName(typ);
2016 }
2017 
2018 
2027 {
2028  if (!name || !*name)
2029  return Invalid;
2030  if (strcmp(name, "Q3CString") == 0)
2031  return ByteArray;
2032  if (strcmp(name, "Q_LLONG") == 0)
2033  return LongLong;
2034  if (strcmp(name, "Q_ULLONG") == 0)
2035  return ULongLong;
2036  if (strcmp(name, "QIconSet") == 0)
2037  return Icon;
2038  if (strcmp(name, "UserType") == 0)
2039  return UserType;
2040 
2041  int metaType = QMetaType::type(name);
2042  return metaType <= int(LastGuiType) ? QVariant::Type(metaType) : UserType;
2043 }
2044 
2045 #ifndef QT_NO_DATASTREAM
2046 enum { MapFromThreeCount = 36 };
2048 {
2050  QVariant::Map,
2061  63, // ColorGroup
2065  QVariant::Int,
2081  QVariant::Pen,
2085 };
2086 
2094 {
2095  clear();
2096 
2097  quint32 u;
2098  s >> u;
2099  if (s.version() < QDataStream::Qt_4_0) {
2100  if (u >= MapFromThreeCount)
2101  return;
2102  u = map_from_three[u];
2103  }
2104  qint8 is_null = false;
2105  if (s.version() >= QDataStream::Qt_4_2)
2106  s >> is_null;
2107  if (u == QVariant::UserType) {
2108  QByteArray name;
2109  s >> name;
2110  u = QMetaType::type(name);
2111  if (!u) {
2113  return;
2114  }
2115  }
2116  create(static_cast<int>(u), 0);
2117  d.is_null = is_null;
2118 
2119  if (!isValid()) {
2120  // Since we wrote something, we should read something
2121  QString x;
2122  s >> x;
2123  d.is_null = true;
2124  return;
2125  }
2126 
2127  // const cast is safe since we operate on a newly constructed variant
2128  if (!QMetaType::load(s, d.type, const_cast<void *>(constData()))) {
2130  qWarning("QVariant::load: unable to load type %d.", d.type);
2131  }
2132 }
2133 
2141 {
2142  quint32 tp = type();
2143  if (s.version() < QDataStream::Qt_4_0) {
2144  int i;
2145  for (i = MapFromThreeCount - 1; i >= 0; i--) {
2146  if (map_from_three[i] == tp) {
2147  tp = i;
2148  break;
2149  }
2150  }
2151  if (i == -1) {
2152  s << QVariant();
2153  return;
2154  }
2155  }
2156  s << tp;
2157  if (s.version() >= QDataStream::Qt_4_2)
2158  s << qint8(d.is_null);
2159  if (tp == QVariant::UserType) {
2160  s << QMetaType::typeName(userType());
2161  }
2162 
2163  if (!isValid()) {
2164  s << QString();
2165  return;
2166  }
2167 
2168  if (!QMetaType::save(s, d.type, constData())) {
2169  Q_ASSERT_X(false, "QVariant::save", "Invalid type to save");
2170  qWarning("QVariant::save: unable to save type %d.", d.type);
2171  }
2172 }
2173 
2185 {
2186  p.load(s);
2187  return s;
2188 }
2189 
2196 {
2197  p.save(s);
2198  return s;
2199 }
2200 
2205 {
2206  quint32 u;
2207  s >> u;
2208  p = (QVariant::Type)u;
2209 
2210  return s;
2211 }
2212 
2217 {
2218  s << static_cast<quint32>(p);
2219 
2220  return s;
2221 }
2222 
2223 #endif //QT_NO_DATASTREAM
2224 
2235 template <typename T>
2236 inline T qVariantToHelper(const QVariant::Private &d, QVariant::Type t,
2237  const QVariant::Handler *handler, T * = 0)
2238 {
2239  if (d.type == t)
2240  return *v_cast<T>(&d);
2241 
2242  T ret;
2243  handler->convert(&d, t, &ret, 0);
2244  return ret;
2245 }
2246 
2260 {
2261  return qVariantToHelper<QStringList>(d, StringList, handler);
2262 }
2263 
2271 {
2272  return qVariantToHelper<QString>(d, String, handler);
2273 }
2274 
2282 {
2283  return qVariantToHelper<QVariantMap>(d, Map, handler);
2284 }
2285 
2293 {
2294  return qVariantToHelper<QVariantHash>(d, Hash, handler);
2295 }
2296 
2312 {
2313  return qVariantToHelper<QDate>(d, Date, handler);
2314 }
2315 
2331 {
2332  return qVariantToHelper<QTime>(d, Time, handler);
2333 }
2334 
2350 {
2351  return qVariantToHelper<QDateTime>(d, DateTime, handler);
2352 }
2353 
2365 #ifndef QT_BOOTSTRAPPED
2367 {
2368  return qVariantToHelper<QEasingCurve>(d, EasingCurve, handler);
2369 }
2370 #endif
2371 
2384 {
2385  return qVariantToHelper<QByteArray>(d, ByteArray, handler);
2386 }
2387 
2388 #ifndef QT_NO_GEOM_VARIANT
2389 
2401 {
2402  return qVariantToHelper<QPoint>(d, Point, handler);
2403 }
2404 
2417 {
2418  return qVariantToHelper<QRect>(d, Rect, handler);
2419 }
2420 
2433 {
2434  return qVariantToHelper<QSize>(d, Size, handler);
2435 }
2436 
2448 {
2449  return qVariantToHelper<QSizeF>(d, SizeF, handler);
2450 }
2451 
2464 {
2465  return qVariantToHelper<QRectF>(d, RectF, handler);
2466 }
2467 
2479 {
2480  return qVariantToHelper<QLineF>(d, LineF, handler);
2481 }
2482 
2495 {
2496  return qVariantToHelper<QLine>(d, Line, handler);
2497 }
2498 
2510 {
2511  return qVariantToHelper<QPointF>(d, PointF, handler);
2512 }
2513 
2514 #endif // QT_NO_GEOM_VARIANT
2515 
2516 #ifndef QT_BOOTSTRAPPED
2517 
2529 {
2530  return qVariantToHelper<QUrl>(d, Url, handler);
2531 }
2532 #endif
2533 
2546 {
2547  return qVariantToHelper<QLocale>(d, Locale, handler);
2548 }
2549 
2561 #ifndef QT_NO_REGEXP
2563 {
2564  return qVariantToHelper<QRegExp>(d, RegExp, handler);
2565 }
2566 #endif
2567 
2580 {
2581  return qVariantToHelper<QChar>(d, Char, handler);
2582 }
2583 
2591 {
2592  return qVariantToHelper<QBitArray>(d, BitArray, handler);
2593 }
2594 
2595 template <typename T>
2596 inline T qNumVariantToHelper(const QVariant::Private &d,
2597  const QVariant::Handler *handler, bool *ok, const T& val)
2598 {
2599  uint t = qMetaTypeId<T>();
2600  if (ok)
2601  *ok = true;
2602  if (d.type == t)
2603  return val;
2604 
2605  T ret;
2606  if (!handler->convert(&d, QVariant::Type(t), &ret, ok) && ok)
2607  *ok = false;
2608  return ret;
2609 }
2610 
2625 int QVariant::toInt(bool *ok) const
2626 {
2627  return qNumVariantToHelper<int>(d, handler, ok, d.data.i);
2628 }
2629 
2644 uint QVariant::toUInt(bool *ok) const
2645 {
2646  return qNumVariantToHelper<uint>(d, handler, ok, d.data.u);
2647 }
2648 
2660 {
2661  return qNumVariantToHelper<qlonglong>(d, handler, ok, d.data.ll);
2662 }
2663 
2676 {
2677  return qNumVariantToHelper<qulonglong>(d, handler, ok, d.data.ull);
2678 }
2679 
2691 bool QVariant::toBool() const
2692 {
2693  if (d.type == Bool)
2694  return d.data.b;
2695 
2696  bool res = false;
2697  handler->convert(&d, Bool, &res, 0);
2698 
2699  return res;
2700 }
2701 
2710 double QVariant::toDouble(bool *ok) const
2711 {
2712  return qNumVariantToHelper<double>(d, handler, ok, d.data.d);
2713 }
2714 
2725 float QVariant::toFloat(bool *ok) const
2726 {
2727  return qNumVariantToHelper<float>(d, handler, ok, d.data.f);
2728 }
2729 
2740 qreal QVariant::toReal(bool *ok) const
2741 {
2742  return qNumVariantToHelper<qreal>(d, handler, ok, d.data.real);
2743 }
2744 
2752 {
2753  return qVariantToHelper<QVariantList>(d, List, handler);
2754 }
2755 
2772 {
2773 /*Invalid*/ 0,
2774 
2775 /*Bool*/ 1 << QVariant::Double | 1 << QVariant::Int | 1 << QVariant::UInt
2777  | 1 << QVariant::String | 1 << QVariant::Char,
2778 
2779 /*Int*/ 1 << QVariant::UInt | 1 << QVariant::String | 1 << QVariant::Double
2781  | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2782 
2783 /*UInt*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::Double
2785  | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2786 
2787 /*LLong*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::Double
2789  | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2790 
2791 /*ULlong*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::Double
2792  | 1 << QVariant::Bool | 1 << QVariant::UInt | 1 << QVariant::LongLong
2793  | 1 << QVariant::Char | 1 << QVariant::ByteArray,
2794 
2795 /*double*/ 1 << QVariant::Int | 1 << QVariant::String | 1 << QVariant::ULongLong
2796  | 1 << QVariant::Bool | 1 << QVariant::UInt | 1 << QVariant::LongLong
2797  | 1 << QVariant::ByteArray,
2798 
2799 /*QChar*/ 1 << QVariant::Int | 1 << QVariant::UInt | 1 << QVariant::LongLong
2800  | 1 << QVariant::ULongLong,
2801 
2802 /*QMap*/ 0,
2803 
2804 /*QList*/ 1 << QVariant::StringList,
2805 
2806 /*QString*/ 1 << QVariant::StringList | 1 << QVariant::ByteArray | 1 << QVariant::Int
2807  | 1 << QVariant::UInt | 1 << QVariant::Bool | 1 << QVariant::Double
2808  | 1 << QVariant::Date | 1 << QVariant::Time | 1 << QVariant::DateTime
2810  | 1 << QVariant::Url,
2811 
2812 /*QStringList*/ 1 << QVariant::List | 1 << QVariant::String,
2813 
2814 /*QByteArray*/ 1 << QVariant::String | 1 << QVariant::Int | 1 << QVariant::UInt | 1 << QVariant::Bool
2816 
2817 /*QBitArray*/ 0,
2818 
2819 /*QDate*/ 1 << QVariant::String | 1 << QVariant::DateTime,
2820 
2821 /*QTime*/ 1 << QVariant::String | 1 << QVariant::DateTime,
2822 
2823 /*QDateTime*/ 1 << QVariant::String | 1 << QVariant::Date,
2824 
2825 /*QUrl*/ 1 << QVariant::String,
2826 
2827 /*QLocale*/ 0,
2828 
2829 /*QRect*/ 1 << QVariant::RectF,
2830 
2831 /*QRectF*/ 1 << QVariant::Rect,
2832 
2833 /*QSize*/ 1 << QVariant::SizeF,
2834 
2835 /*QSizeF*/ 1 << QVariant::Size,
2836 
2837 /*QLine*/ 1 << QVariant::LineF,
2838 
2839 /*QLineF*/ 1 << QVariant::Line,
2840 
2841 /*QPoint*/ 1 << QVariant::PointF,
2842 
2843 /*QPointF*/ 1 << QVariant::Point,
2844 
2845 /*QRegExp*/ 0,
2846 
2847 /*QHash*/ 0,
2848 
2849 /*QEasingCurve*/ 0
2850 };
2851 
2887 {
2888  //we can treat floats as double
2889  //the reason for not doing it the "proper" way is that QMetaType::Float's value is 135,
2890  //which can't be handled by qCanConvertMatrix
2891  //In addition QVariant::Type doesn't have a Float value, so we're using QMetaType::Float
2892  const uint currentType = ((d.type == QMetaType::Float) ? QVariant::Double : d.type);
2893  if (uint(t) == uint(QMetaType::Float)) t = QVariant::Double;
2894 
2895  if (currentType == uint(t))
2896  return true;
2897 
2898  if (currentType > QVariant::LastCoreType || t > QVariant::LastCoreType) {
2899  switch (uint(t)) {
2900  case QVariant::Int:
2901  return currentType == QVariant::KeySequence
2902  || currentType == QMetaType::ULong
2903  || currentType == QMetaType::Long
2904  || currentType == QMetaType::UShort
2905  || currentType == QMetaType::UChar
2906  || currentType == QMetaType::Char
2907  || currentType == QMetaType::Short;
2908  case QVariant::Image:
2909  return currentType == QVariant::Pixmap || currentType == QVariant::Bitmap;
2910  case QVariant::Pixmap:
2911  return currentType == QVariant::Image || currentType == QVariant::Bitmap
2912  || currentType == QVariant::Brush;
2913  case QVariant::Bitmap:
2914  return currentType == QVariant::Pixmap || currentType == QVariant::Image;
2915  case QVariant::ByteArray:
2916  return currentType == QVariant::Color;
2917  case QVariant::String:
2918  return currentType == QVariant::KeySequence || currentType == QVariant::Font
2919  || currentType == QVariant::Color;
2920  case QVariant::KeySequence:
2921  return currentType == QVariant::String || currentType == QVariant::Int;
2922  case QVariant::Font:
2923  return currentType == QVariant::String;
2924  case QVariant::Color:
2925  return currentType == QVariant::String || currentType == QVariant::ByteArray
2926  || currentType == QVariant::Brush;
2927  case QVariant::Brush:
2928  return currentType == QVariant::Color || currentType == QVariant::Pixmap;
2929  case QMetaType::Long:
2930  case QMetaType::Char:
2931  case QMetaType::UChar:
2932  case QMetaType::ULong:
2933  case QMetaType::Short:
2934  case QMetaType::UShort:
2935  return qCanConvertMatrix[QVariant::Int] & (1 << currentType) || currentType == QVariant::Int;
2936  default:
2937  return false;
2938  }
2939  }
2940 
2941  if(t == String && currentType == StringList)
2942  return v_cast<QStringList>(&d)->count() == 1;
2943  else
2944  return qCanConvertMatrix[t] & (1 << currentType);
2945 }
2946 
2960 {
2961  if (d.type == uint(t))
2962  return true;
2963 
2964  QVariant oldValue = *this;
2965 
2966  clear();
2967  if (!oldValue.canConvert(t))
2968  return false;
2969 
2970  create(t, 0);
2971  if (oldValue.isNull())
2972  return false;
2973 
2974  bool isOk = true;
2975  if (!handler->convert(&oldValue.d, t, data(), &isOk))
2976  isOk = false;
2977  d.is_null = !isOk;
2978  return isOk;
2979 }
2980 
3033 static bool qIsNumericType(uint tp)
3034 {
3035  return (tp >= QVariant::Bool && tp <= QVariant::Double)
3036  || (tp >= QMetaType::Long && tp <= QMetaType::Float);
3037 }
3038 
3039 static bool qIsFloatingPoint(uint tp)
3040 {
3041  return tp == QVariant::Double || tp == QMetaType::Float;
3042 }
3043 
3046 bool QVariant::cmp(const QVariant &v) const
3047 {
3048  QVariant v2 = v;
3049  if (d.type != v2.d.type) {
3050  if (qIsNumericType(d.type) && qIsNumericType(v.d.type)) {
3051  if (qIsFloatingPoint(d.type) || qIsFloatingPoint(v.d.type))
3052  return qFuzzyCompare(toReal(), v.toReal());
3053  else
3054  return toLongLong() == v.toLongLong();
3055  }
3056  if (!v2.canConvert(Type(d.type)) || !v2.convert(Type(d.type)))
3057  return false;
3058  }
3059  return handler->compare(&d, &v2.d);
3060 }
3061 
3065 const void *QVariant::constData() const
3066 {
3067  return d.is_shared ? d.data.shared->ptr : reinterpret_cast<const void *>(&d.data.ptr);
3068 }
3069 
3078 {
3079  detach();
3080  return const_cast<void *>(constData());
3081 }
3082 
3083 
3084 #ifdef QT3_SUPPORT
3085 
3087 void *QVariant::castOrDetach(Type t)
3088 {
3089  if (d.type != uint(t)) {
3090  if (!convert(t))
3091  create(t, 0);
3092  } else {
3093  detach();
3094  }
3095  return data();
3096 }
3097 #endif
3098 
3102 bool QVariant::isNull() const
3103 {
3104  return handler->isNull(&d);
3105 }
3106 
3107 #ifndef QT_NO_DEBUG_STREAM
3109 {
3110 #ifndef Q_BROKEN_DEBUG_STREAM
3111  dbg.nospace() << "QVariant(" << v.typeName() << ", ";
3112  QVariant::handler->debugStream(dbg, v);
3113  dbg.nospace() << ')';
3114  return dbg.space();
3115 #else
3116  qWarning("This compiler doesn't support streaming QVariant to QDebug");
3117  return dbg;
3118  Q_UNUSED(v);
3119 #endif
3120 }
3121 
3123 {
3124 #ifndef Q_BROKEN_DEBUG_STREAM
3125  dbg.nospace() << "QVariant::" << QVariant::typeToName(p);
3126  return dbg.space();
3127 #else
3128  qWarning("This compiler doesn't support streaming QVariant::Type to QDebug");
3129  return dbg;
3130  Q_UNUSED(p);
3131 #endif
3132 }
3133 #endif
3134 
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
double d
Definition: qnumeric_p.h:62
static uint hash(const uchar *p, int n)
Definition: qhash.cpp:68
QVariant & operator=(const QVariant &other)
Assigns the value of the variant variant to this variant.
Definition: qvariant.cpp:1921
int type
Definition: qmetatype.cpp:239
double qreal
Definition: qglobal.h:1193
QRegExp toRegExp() const
Returns the variant as a QRegExp if the variant has type() RegExp ; otherwise returns an empty QRegEx...
Definition: qvariant.cpp:2562
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QPointF toPointF() const
Returns the variant as a QPointF if the variant has type() Point or PointF ; otherwise returns a null...
Definition: qvariant.cpp:2509
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
Definition: qstring.cpp:4276
QLine toLine() const
Returns the variant as a QLine if the variant has type() Line ; otherwise returns an invalid QLine...
Definition: qvariant.cpp:2494
~QVariant()
Destroys the QVariant and the contained object.
Definition: qvariant.cpp:1427
The QEasingCurve class provides easing curves for controlling animation.
Definition: qeasingcurve.h:55
void create(int type, const void *copy)
Definition: qvariant.cpp:1407
The QLine class provides a two-dimensional vector using integer precision.
Definition: qline.h:57
QMap< QString, QVariant > toMap() const
Returns the variant as a QMap<QString, QVariant> if the variant has type() Map ; otherwise returns an...
Definition: qvariant.cpp:2281
QDataStream & operator>>(QDataStream &s, QVariant &p)
Reads a variant p from the stream s.
Definition: qvariant.cpp:2184
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
bool isValid() const
Returns true if this date is valid; otherwise returns false.
Definition: qdatetime.cpp:340
bool isNull() const
Returns true if this is a NULL variant, false otherwise.
Definition: qvariant.cpp:3102
#define it(className, varName)
#define DBL_DIG
Definition: qvariant.cpp:69
QDebug & nospace()
Clears the stream&#39;s internal flag that records whether the last character was a space and returns a r...
Definition: qdebug.h:92
#define at(className, varName)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
static void clear(QVariant::Private *d)
Definition: qvariant.cpp:197
int count(const Key &key) const
Returns the number of items associated with key key.
Definition: qmap.h:539
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
QDateTime toDateTime() const
Returns the variant as a QDateTime if the variant has type() DateTime , Date , or String ; otherwise ...
Definition: qvariant.cpp:2349
QList< QVariant > toList() const
Returns the variant as a QVariantList if the variant has type() List or StringList ; otherwise return...
Definition: qvariant.cpp:2751
bool isValid() const
Returns true if both the date and the time are valid; otherwise returns false.
Definition: qdatetime.cpp:2346
Q_DECL_CONSTEXPR qint64 qRound64(qreal d)
Definition: qglobal.h:1210
The QDate class provides date functions.
Definition: qdatetime.h:55
static Q_DECL_CONSTEXPR bool qFuzzyCompare(double p1, double p2)
Definition: qglobal.h:2030
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
bool toBool() const
Returns the variant as a bool if the variant has type() Bool.
Definition: qvariant.cpp:2691
quint16 u
long ASN1_INTEGER_get ASN1_INTEGER * a
void setStatus(Status status)
Sets the status of the data stream to the status given.
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
int qstrcmp(const char *str1, const char *str2)
A safe strcmp() function.
Definition: qbytearray.cpp:231
QRectF toRectF() const
Returns the variant as a QRectF if the variant has type() Rect or RectF ; otherwise returns an invali...
Definition: qvariant.cpp:2463
static const char * typeToName(Type type)
Converts the enum representation of the storage type, typ, to its string representation.
Definition: qvariant.cpp:2008
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
static const ushort map_from_three[MapFromThreeCount]
Definition: qvariant.cpp:2047
const QVariant::Handler qt_kernel_variant_handler
Definition: qvariant.cpp:1175
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
static void * construct(int type, const void *copy=0)
Returns a copy of copy, assuming it is of type type.
Definition: qmetatype.cpp:1042
QByteArray toByteArray() const
Returns the variant as a QByteArray if the variant has type() ByteArray or String (converted using QS...
Definition: qvariant.cpp:2383
bool canConvert() const
Returns true if the variant can be converted to the template type T, otherwise false.
Definition: qvariant.h:340
GlobalColor
Definition: qnamespace.h:104
const T * v_cast(const QVariant::Private *d, T *=0)
Definition: qvariant_p.h:78
The QLineF class provides a two-dimensional vector using floating point precision.
Definition: qline.h:212
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
signed char qint8
Definition: qglobal.h:933
static void streamDebug(QDebug dbg, const QVariant &v)
Definition: qvariant.cpp:1073
unsigned char uchar
Definition: qglobal.h:994
static QString toString(Register *reg, int type, bool *ok=0)
QStringList toStringList() const
Returns the variant as a QStringList if the variant has type() StringList, String ...
Definition: qvariant.cpp:2259
static QDateTime fromString(const QString &s, Qt::DateFormat f=Qt::TextDate)
Returns the QDateTime represented by the string, using the format given, or an invalid datetime if th...
Definition: qdatetime.cpp:3487
void load(QDataStream &ds)
Internal function for loading a variant from stream s.
Definition: qvariant.cpp:2093
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
void * data()
Definition: qvariant.cpp:3077
The QTime class provides clock time functions.
Definition: qdatetime.h:148
QFuture< void > map(Sequence &sequence, MapFunction function)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
qlonglong toLongLong(bool *ok=0) const
Returns the variant as a long long int if the variant has type() LongLong , Bool , ByteArray , Char , Double , Int , String , UInt , or ULongLong ; otherwise returns 0.
Definition: qvariant.cpp:2659
static bool compare(const QVariant::Private *a, const QVariant::Private *b)
Compares a to b.
Definition: qvariant.cpp:383
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
static const Handler * handler
Definition: qvariant.h:419
const_iterator ConstIterator
Qt-style synonym for QMap::const_iterator.
Definition: qmap.h:389
bool canConvert(Type t) const
Returns true if the variant&#39;s type can be cast to the requested type, t.
Definition: qvariant.cpp:2886
#define FLT_DIG
Definition: qvariant.cpp:72
const char * typeName
Definition: qmetatype.cpp:239
const char * name
static bool qIsFloatingPoint(uint tp)
Definition: qvariant.cpp:3039
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QHash< QString, QVariant > toHash() const
Returns the variant as a QHash<QString, QVariant> if the variant has type() Hash ; otherwise returns ...
Definition: qvariant.cpp:2292
qulonglong toULongLong(bool *ok=0) const
Returns the variant as as an unsigned long long int if the variant has type() ULongLong ...
Definition: qvariant.cpp:2675
Q_CORE_EXPORT void qWarning(const char *,...)
QVariant()
Constructs an invalid variant.
Definition: qvariant.h:484
unsigned int uint
Definition: qglobal.h:996
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
Type
This enum type defines the types of variable that a QVariant can contain.
Definition: qvariant.h:95
static void destroy(int type, void *data)
Destroys the data, assuming it is of the type given.
Definition: qmetatype.cpp:1263
static bool qIsNumericType(uint tp)
Definition: qvariant.cpp:3033
const T * ptr(const T &t)
ushort Char
static int type(const char *typeName)
Returns a handle to the type called typeName, or 0 if there is no such type.
Definition: qmetatype.cpp:607
unsigned long ulong
Definition: qglobal.h:997
T qNumVariantToHelper(const QVariant::Private &d, const QVariant::Handler *handler, bool *ok, const T &val)
Definition: qvariant.cpp:2596
static const quint32 qCanConvertMatrix[QVariant::LastCoreType+1]
Definition: qvariant.cpp:2771
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the map.
Definition: qmap.h:374
QLocale toLocale() const
Returns the variant as a QLocale if the variant has type() Locale ; otherwise returns an invalid QLoc...
Definition: qvariant.cpp:2545
const char * typeName() const
Returns the name of the type stored in the variant.
Definition: qvariant.cpp:1984
QUrl toUrl() const
Returns the variant as a QUrl if the variant has type() Url ; otherwise returns an invalid QUrl...
Definition: qvariant.cpp:2528
QSize toSize() const
Returns the variant as a QSize if the variant has type() Size ; otherwise returns an invalid QSize...
Definition: qvariant.cpp:2432
static qulonglong qConvertToUnsignedNumber(const QVariant::Private *d, bool *ok)
Definition: qvariant.cpp:582
#define Q_INT64_C(c)
Definition: qglobal.h:940
T qVariantToHelper(const QVariant::Private &d, QVariant::Type t, const QVariant::Handler *handler, T *=0)
Definition: qvariant.cpp:2236
static QTime fromString(const QString &s, Qt::DateFormat f=Qt::TextDate)
Returns the time represented in the string as a QTime using the format given, or an invalid time if t...
Definition: qdatetime.cpp:1928
bool convert(Type t)
Casts the variant to the requested type, t.
Definition: qvariant.cpp:2959
int version() const
Returns the version number of the data serialization format.
Definition: qdatastream.h:212
The QBitArray class provides an array of bits.
Definition: qbitarray.h:54
QSizeF toSizeF() const
Returns the variant as a QSizeF if the variant has type() SizeF ; otherwise returns an invalid QSizeF...
Definition: qvariant.cpp:2447
static bool convert(const QVariant::Private *d, QVariant::Type t, void *result, bool *ok)
Definition: qvariant.cpp:627
QDate toDate() const
Returns the variant as a QDate if the variant has type() Date , DateTime , or String ; otherwise retu...
Definition: qvariant.cpp:2311
Q_CORE_EXPORT void qFatal(const char *,...)
void clear()
Convert this variant to type Invalid and free up any resources used.
Definition: qvariant.cpp:1993
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:380
QEasingCurve toEasingCurve() const
Returns the variant as a QEasingCurve if the variant has type() EasingCurve ; otherwise returns a def...
Definition: qvariant.cpp:2366
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913
void detach()
Definition: qvariant.cpp:1958
QLineF toLineF() const
Returns the variant as a QLineF if the variant has type() LineF ; otherwise returns an invalid QLineF...
Definition: qvariant.cpp:2478
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
bool qt_convertToBool(const QVariant::Private *const d)
Definition: qvariant.cpp:616
#define Q_CORE_EXPORT
Definition: qglobal.h:1449
Private d
Definition: qvariant.h:417
static void construct(QVariant::Private *x, const void *copy)
Definition: qvariant.cpp:75
static const char * typeName(int type)
Returns the type name associated with the given type, or 0 if no matching type was found...
Definition: qmetatype.cpp:406
unsigned short ushort
Definition: qglobal.h:995
QRect toRect() const
Returns the variant as a QRect if the variant has type() Rect ; otherwise returns an invalid QRect...
Definition: qvariant.cpp:2416
Type type() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1901
uint qstrlen(const char *str)
Definition: qbytearray.h:79
const void * constData() const
Definition: qvariant.cpp:3065
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
static QDate fromString(const QString &s, Qt::DateFormat f=Qt::TextDate)
Returns the QDate represented by the string, using the format given, or an invalid date if the string...
Definition: qdatetime.cpp:1203
uint toUInt(bool *ok=0) const
Returns the variant as an unsigned int if the variant has type() UInt , Bool , ByteArray ...
Definition: qvariant.cpp:2644
unsigned int quint32
Definition: qglobal.h:938
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
float toFloat(bool *ok=0) const
Returns the variant as a float if the variant has type() Double , QMetaType::Float ...
Definition: qvariant.cpp:2725
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
Q_CORE_EXPORT const QVariant::Handler * qcoreVariantHandler()
Definition: qvariant.cpp:1193
QFactoryLoader * l
double toDouble(bool *ok=0) const
Returns the variant as a double if the variant has type() Double , QMetaType::Float ...
Definition: qvariant.cpp:2710
static qreal toDouble(const QChar *&str)
void save(QDataStream &ds) const
Internal function for saving a variant to the stream s.
Definition: qvariant.cpp:2140
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
QTime toTime() const
Returns the variant as a QTime if the variant has type() Time , DateTime , or String ; otherwise retu...
Definition: qvariant.cpp:2330
quint64 qulonglong
Definition: qglobal.h:952
const char * variant
bool cmp(const QVariant &other) const
Definition: qvariant.cpp:3046
#define Q_UINT64_C(c)
Definition: qglobal.h:941
static qlonglong qConvertToNumber(const QVariant::Private *d, bool *ok)
Definition: qvariant.cpp:549
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485
static Type nameToType(const char *name)
Converts the string representation of the storage type given in name, to its enum representation...
Definition: qvariant.cpp:2026
static bool load(QDataStream &stream, int type, void *data)
Reads the object of the specified type from the given stream into data.
Definition: qmetatype.cpp:845
static qlonglong qMetaTypeNumber(const QVariant::Private *d)
Definition: qvariant.cpp:509
QDebug & space()
Writes a space character to the debug stream and returns a reference to the stream.
Definition: qdebug.h:91
static QChar fromAscii(char c)
Converts the ASCII character c to its equivalent QChar.
Definition: qchar.cpp:1521
QChar toChar() const
Returns the variant as a QChar if the variant has type() Char , Int , or UInt ; otherwise returns an ...
Definition: qvariant.cpp:2579
qint64 qlonglong
Definition: qglobal.h:951
static bool isRegistered(int type)
Returns true if the datatype with ID type is registered; otherwise returns false. ...
Definition: qmetatype.cpp:590
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
static qulonglong qMetaTypeUNumber(const QVariant::Private *d)
Definition: qvariant.cpp:531
static QByteArray number(int, int base=10)
Returns a byte array containing the string equivalent of the number n to base base (10 by default)...
static bool save(QDataStream &stream, int type, const void *data)
Writes the object pointed to by data with the ID type to the given stream.
Definition: qmetatype.cpp:646
QPoint toPoint() const
Returns the variant as a QPoint if the variant has type() Point or PointF ; otherwise returns a null ...
Definition: qvariant.cpp:2400
bool compareNumericMetaType(const QVariant::Private *const a, const QVariant::Private *const b)
Definition: qvariant.cpp:369
static bool isNull(const QVariant::Private *d)
Definition: qvariant.cpp:300
qreal toReal(bool *ok=0) const
Returns the variant as a qreal if the variant has type() Double , QMetaType::Float ...
Definition: qvariant.cpp:2740
bool isValid() const
Returns true if the time is valid; otherwise returns false.
Definition: qdatetime.cpp:1566
friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QVariant &)
Definition: qvariant.cpp:3108
T qvariant_cast(const QVariant &value)
Returns the given value converted to the template type T.
Definition: qvariant.h:571
QBitArray toBitArray() const
Returns the variant as a QBitArray if the variant has type() BitArray ; otherwise returns an empty bi...
Definition: qvariant.cpp:2590