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