9#include "private/qlocale_p.h"
12#if QT_CONFIG(itemmodel)
13#include "qabstractitemmodel.h"
24#if QT_CONFIG(easingcurve)
25#include "qeasingcurve.h"
38#if QT_CONFIG(regularexpression)
39#include "qregularexpression.h"
53using namespace Qt::StringLiterals;
57static qlonglong qMetaTypeNumberBySize(
const QVariant::Private *d)
59 switch (d->typeInterface()->size) {
61 return d->get<
signed char>();
63 return d->get<
short>();
67 return d->get<qlonglong>();
69 Q_UNREACHABLE_RETURN(0);
72static qlonglong qMetaTypeNumber(
const QVariant::Private *d)
74 switch (d->typeInterface()->typeId) {
76 case QMetaType::LongLong:
78 case QMetaType::SChar:
79 case QMetaType::Short:
81 return qMetaTypeNumberBySize(d);
82 case QMetaType::Float:
83 return qRound64(d->get<
float>());
84 case QMetaType::Double:
85 return qRound64(d->get<
double>());
86 case QMetaType::QJsonValue:
87 return d->get<QJsonValue>().toDouble();
88 case QMetaType::QCborValue:
89 return d->get<QCborValue>().toInteger();
91 Q_UNREACHABLE_RETURN(0);
94static qulonglong qMetaTypeUNumber(
const QVariant::Private *d)
96 switch (d->typeInterface()->size) {
98 return d->get<
unsigned char>();
100 return d->get<
unsigned short>();
102 return d->get<
unsigned int>();
104 return d->get<qulonglong>();
106 Q_UNREACHABLE_RETURN(0);
109static std::optional<qlonglong> qConvertToNumber(
const QVariant::Private *d,
bool allowStringToBool =
false)
112 switch (d->typeInterface()->typeId) {
113 case QMetaType::QString: {
114 const QString &s = d->get<QString>();
115 if (qlonglong l = s.toLongLong(&ok); ok)
117 if (allowStringToBool) {
118 if (s ==
"false"_L1 || s ==
"0"_L1)
120 if (s ==
"true"_L1 || s ==
"1"_L1)
125 case QMetaType::QChar:
126 return d->get<QChar>().unicode();
127 case QMetaType::QByteArray:
128 if (qlonglong l = d->get<
QByteArray>().toLongLong(&ok); ok)
131 case QMetaType::Bool:
132 return qlonglong(d->get<
bool>());
133 case QMetaType::QCborValue:
134 if (!d->get<QCborValue>().isInteger() && !d->get<QCborValue>().isDouble())
136 return qMetaTypeNumber(d);
137 case QMetaType::QJsonValue:
138 if (!d->get<QJsonValue>().isDouble())
141 case QMetaType::Double:
143 case QMetaType::Char:
144 case QMetaType::SChar:
145 case QMetaType::Short:
146 case QMetaType::Long:
147 case QMetaType::Float:
148 case QMetaType::LongLong:
149 return qMetaTypeNumber(d);
150 case QMetaType::ULongLong:
151 case QMetaType::UInt:
152 case QMetaType::UChar:
153 case QMetaType::Char16:
154 case QMetaType::Char32:
155 case QMetaType::UShort:
156 case QMetaType::ULong:
157 return qlonglong(qMetaTypeUNumber(d));
158 case QMetaType::QCborSimpleType:
162 if (d->typeInterface()->flags & QMetaType::IsUnsignedEnumeration)
163 return qMetaTypeUNumber(d);
164 if (d->typeInterface()->flags & QMetaType::IsEnumeration)
165 return qMetaTypeNumberBySize(d);
170static std::optional<
double> qConvertToRealNumber(
const QVariant::Private *d)
173 switch (d->typeInterface()->typeId) {
174 case QMetaType::QString:
175 if (
double r = d->get<QString>().toDouble(&ok); ok)
178 case QMetaType::Double:
179 return d->get<
double>();
180 case QMetaType::Float:
181 return double(d->get<
float>());
182 case QMetaType::Float16:
184 case QMetaType::ULongLong:
185 case QMetaType::UInt:
186 case QMetaType::UChar:
187 case QMetaType::Char16:
188 case QMetaType::Char32:
189 case QMetaType::UShort:
190 case QMetaType::ULong:
191 return double(qMetaTypeUNumber(d));
192 case QMetaType::QCborValue:
193 return d->get<QCborValue>().toDouble();
194 case QMetaType::QJsonValue:
195 return d->get<QJsonValue>().toDouble();
198 if (std::optional<qlonglong> l = qConvertToNumber(d))
207 if (!iface || iface->size == 0)
210 Q_ASSERT(!isInterfaceFor<
void>(iface));
214 qWarning(
"QVariant: Provided metatype for '%s' does not support destruction and "
215 "copy construction", iface
->name);
221 qWarning(
"QVariant: Cannot create type '%s' without a default constructor", iface
->name);
228enum CustomConstructMoveOptions {
234enum CustomConstructNullabilityOption {
241template <CustomConstructMoveOptions moveOption = UseCopy, CustomConstructNullabilityOption nullability = MaybeNull>
243 std::conditional_t<moveOption == ForceMove,
void *,
const void *> copy)
247 Q_ASSERT(iface->size);
248 Q_ASSERT(!isInterfaceFor<
void>(iface));
252 if constexpr (moveOption == ForceMove)
254 if constexpr (nullability == NonNull)
255 Q_ASSERT(copy !=
nullptr);
260 d->is_null = !copy QT6_ONLY(|| isInterfaceFor<std::nullptr_t>(iface));
262 if (QVariant::Private::canUseInternalSpace(iface)) {
263 d->is_shared =
false;
266 if constexpr (moveOption == ForceMove && nullability == NonNull)
267 moveConstruct(iface, d->data.data, copy);
269 construct(iface, d->data.data, copy);
271 d->data.shared = customConstructShared(iface->size, iface->alignment, [=](
void *where) {
272 if constexpr (moveOption == ForceMove && nullability == NonNull)
273 moveConstruct(iface, where, copy);
275 construct(iface, where, copy);
281static void customClear(QVariant::Private *d)
290 QVariant::PrivateShared::free(d->data.shared);
294static QVariant::Private clonePrivate(
const QVariant::Private &other)
296 QVariant::Private d = other;
298 d.data.shared->ref.ref();
300 if (Q_LIKELY(d.canUseInternalSpace(iface))) {
308 d.data.shared = QVariant::PrivateShared::create(iface->size, iface->alignment);
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
469
470
471
472
473
474
475
478
479
480
481
482
483
486
487
488
489
491#if QT_REMOVAL_QT7_DEPRECATED_SINCE(6
, 16
)
493
494
495
496
497
498
499
500
501void QVariant::create(
int type,
const void *copy)
503 *
this = QVariant::fromMetaType(QMetaType(type), copy);
507
508
509
510void QVariant::create(QMetaType type,
const void *copy)
512 *
this = QVariant::fromMetaType(type, copy);
517
518
519
520
524 if (!d.is_shared || !d.data.shared->ref.deref())
529
530
531
532
533
535QVariant::QVariant(
const QVariant &p)
536 : d(clonePrivate(p.d))
541
542
543
544
545
546
547
548
549
550
551
552
553
556
557
558
559
560
561
562
563
564
568
569
570
571
572
573
574
575
576
579
580
581
582
583
584
585
586
588QVariant::QVariant(std::in_place_t, QMetaType type) : d(type.iface())
592 if (!Private::canUseInternalSpace(type.iface())) {
593 d.data.shared = PrivateShared::create(type.sizeOf(), type.alignOf());
599
600
601
602
603
604void *QVariant::prepareForEmplace(QMetaType type)
607
608
609
610
611
612 auto typeFits = [&] {
613 auto newIface = type.iface();
614 auto oldIface = d.typeInterface();
615 auto newSize = PrivateShared::computeAllocationSize(newIface->size, newIface->alignment);
616 auto oldSize = PrivateShared::computeAllocationSize(oldIface->size, oldIface->alignment);
617 return newSize <= oldSize;
619 if (Private::canUseInternalSpace(type.iface())) {
621 d.packedType = quintptr(type.iface()) >> 2;
623 }
else if (d.is_shared && isDetached() && typeFits()) {
624 QtMetaTypePrivate::destruct(d.typeInterface(), d.data.shared->data());
626 const auto ps = d.data.shared;
627 const auto align = type.alignOf();
628 ps->offset = PrivateShared::computeOffset(ps, align);
629 d.packedType = quintptr(type.iface()) >> 2;
633 QVariant newVariant(std::in_place, type);
636 return const_cast<
void *>(d.storage());
640
641
642
643
646
647
648
649
652
653
654
655
656
659
660
661
662
663
664
665
666
667
668
669
670
671
674
675
676
677
680
681
682
683
686
687
688
689
692
693
694
695
698
699
700
701
704
705
706
707
710
711
712
713
716
717
718
719
722
723
724
725
728
729
730
731
734
735
736
737
738
741
742
743
744
747
748
749
750
751
754
755
756
757
758
761
762
763
764
765
768
769
770
771
774
775
776
777
778
781
782
783
784
787
788
789
790
791
794
795
796
797
800
801
802
803
804
807
808
809
810
813
814
815
816
817
820
821
822
823
826
827
828
829
832
833
834
835
838
839
840
841
844
845
846
847
850
851
852
853
856
857
858
859
862
863
864
865
868
869
870
871
874
875
876
877
880
881
882
883
886
887
888
889
892
893
894
895
898
899
900
901
904
905
906
907
910
911
912
913
916
917
918
919
922
923
924
925
928
929
930
931
935
936
937
938
941
942
943
944
947
948
949
950
951
954
955
956
957
960
961
962
963
966
967
968
969
972
973
974
975
978
979
980
981
984
985
986
987
988
989
992
993
994
995
998
999
1000
1001
1002
1003
1004
1005
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025QVariant::QVariant(QMetaType type,
const void *copy)
1026 : QVariant(fromMetaType(type, copy))
1030#define MAKE_CTOR_BY_VALUE(...)
1031 QVariant::QVariant(__VA_ARGS__ val)
1032 noexcept(QVariant::Private::CanUseInternalSpace<__VA_ARGS__>)
1033 : d{std::in_place, std::move(val)} {}
1034 static_assert(std::is_nothrow_copy_constructible_v<__VA_ARGS__>)
1037#define MAKE_CTOR_BY_REF(...)
1038 QVariant::QVariant(__VA_ARGS__ &&val) noexcept
1039 : d{std::in_place, std::move(val)} {}
1040 QVariant::QVariant(const __VA_ARGS__ &val) noexcept
1041 : d{std::in_place, val} {}
1042 static_assert(QVariant::Private::CanUseInternalSpace<__VA_ARGS__>);
1043 static_assert(std::is_nothrow_copy_constructible_v<__VA_ARGS__>);
1044 static_assert(std::is_nothrow_move_constructible_v<__VA_ARGS__>)
1067QVariant::QVariant(QLatin1StringView val) : QVariant(QString(val)) {}
1069#if QT_CONFIG(easingcurve)
1070QVariant::QVariant(
const QEasingCurve &val) : d{std::in_place, val} {}
1071QVariant::QVariant(QEasingCurve &&val)
noexcept : d{std::in_place, std::move(val)} {}
1072static_assert(QVariant::Private::CanUseInternalSpace<QEasingCurve>);
1084#if QT_CONFIG(regularexpression)
1085MAKE_CTOR_BY_REF(QRegularExpression);
1088QVariant::QVariant(
const QJsonValue &jsonValue)
noexcept(Private::FitsInInternalSize<
sizeof(CborValueStandIn)>)
1089 : d{std::in_place, jsonValue}
1090{
static_assert(
sizeof(CborValueStandIn) ==
sizeof(QJsonValue)); }
1091QVariant::QVariant(QJsonValue &&jsonValue)
noexcept(Private::FitsInInternalSize<
sizeof(CborValueStandIn)>)
1092 : d{std::in_place, std::move(jsonValue)} {}
1095QVariant::QVariant(
const QJsonDocument &jsonDocument) : d{std::in_place, jsonDocument} {}
1096QVariant::QVariant(QJsonDocument &&jsonDocument)
noexcept
1097 : d{std::in_place, jsonDocument} {}
1098static_assert(QVariant::Private::CanUseInternalSpace<QJsonDocument>);
1099#if QT_CONFIG(itemmodel)
1100QVariant::QVariant(
const QModelIndex &modelIndex)
noexcept(Private::FitsInInternalSize<8 + 2 *
sizeof(quintptr)>)
1101 : d{std::in_place, modelIndex} {}
1102QVariant::QVariant(
const QPersistentModelIndex &modelIndex)
1103 : d{std::in_place, modelIndex} {}
1104QVariant::QVariant(QPersistentModelIndex &&modelIndex)
noexcept
1105 : d{std::in_place, std::move(modelIndex)} {}
1106static_assert(QVariant::Private::CanUseInternalSpace<QPersistentModelIndex>);
1109#undef MAKE_CTOR_BY_REF
1110#undef MAKE_CTOR_BY_VALUE
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1149
1150
1151
1152
1153
1154
1155
1158
1159
1160
1161
1162
1165
1166
1167QVariant &QVariant::operator=(
const QVariant &variant)
1169 if (
this == &variant)
1173 d = clonePrivate(variant.d);
1178
1179
1180
1181
1184
1185
1186
1187
1189void QVariant::detach()
1191 if (!d.is_shared || d.data.shared->ref.loadRelaxed() == 1)
1194 Q_ASSERT(isValidMetaTypeForVariant(d.typeInterface(), constData()));
1195 Private dd(d.typeInterface());
1197 customConstruct<UseCopy, NonNull>(d.typeInterface(), &dd, constData());
1198 if (!d.data.shared->ref.deref())
1200 d.data.shared = dd.data.shared;
1204
1205
1206
1207
1210
1211
1212
1213
1214
1215
1216
1219
1220
1221
1222void QVariant::clear()
1224 if (!d.is_shared || !d.data.shared->ref.deref())
1230
1231
1232
1233
1234
1235
1236
1237
1240
1241
1242
1243
1244
1245
1246
1247
1248
1250#ifndef QT_NO_DATASTREAM
1254 QMetaType::UnknownType,
1255 QMetaType::QVariantMap,
1256 QMetaType::QVariantList,
1258 QMetaType::QStringList,
1265 QMetaType::QPalette,
1275 QMetaType::QPolygon,
1279 QMetaType::QSizePolicy,
1282 QMetaType::QDateTime,
1283 QMetaType::QByteArray,
1284 QMetaType::QBitArray,
1285#if QT_CONFIG(shortcut)
1286 QMetaType::QKeySequence,
1291 QMetaType::LongLong,
1292 QMetaType::ULongLong,
1293#if QT_CONFIG(easingcurve)
1294 QMetaType::QEasingCurve
1311
1312
1313
1314
1315
1316void QVariant::load(QDataStream &s)
1322 if (s.version() < QDataStream::Qt_4_0) {
1324 if (typeId >= MapFromThreeCount)
1326 typeId = mapIdFromQt3ToCurrent[typeId];
1327 }
else if (s.version() < QDataStream::Qt_5_0) {
1329 if (typeId == 127 ) {
1330 typeId = Qt5UserType;
1331 }
else if (typeId >= 128 && typeId != Qt5UserType) {
1335 }
else if (typeId == 75 ) {
1336 typeId = Qt5SizePolicy;
1337 }
else if (typeId > 75 && typeId <= 86) {
1343 if (s.version() < QDataStream::Qt_6_0) {
1345 if (typeId == Qt5UserType) {
1346 typeId = QMetaType::User;
1347 }
else if (typeId >= Qt5FirstGuiType && typeId <= Qt5LastGuiType) {
1348 typeId += Qt6ToQt5GuiTypeDelta;
1349 }
else if (typeId == Qt5SizePolicy) {
1350 typeId = QMetaType::QSizePolicy;
1351 }
else if (typeId == Qt5RegExp) {
1352 typeId = QMetaType::fromName(
"QRegExp").rawId();
1356 qint8 is_null =
false;
1357 if (s.version() >= QDataStream::Qt_4_2)
1359 if (typeId == QMetaType::User) {
1362 typeId = QMetaType::fromName(name).rawId();
1363 if (typeId == QMetaType::UnknownType) {
1364 s.setStatus(QDataStream::ReadCorruptData);
1365 qWarning(
"QVariant::load: unknown user type with name %s.", name.constData());
1369 *
this = fromMetaType(QMetaType(typeId));
1370 d.is_null = is_null;
1373 if (s.version() < QDataStream::Qt_5_0) {
1383 void *data =
const_cast<
void *>(constData());
1384 if (!d.type().load(s, data)) {
1385 s.setStatus(QDataStream::ReadCorruptData);
1386 qWarning(
"QVariant::load: unable to load type %d.", d.type().rawId());
1391
1392
1393
1394
1395
1396void QVariant::save(QDataStream &s)
const
1398 quint32 typeId = d.type().rawId();
1399 bool saveAsUserType =
false;
1400 if (typeId >= QMetaType::User) {
1401 typeId = QMetaType::User;
1402 saveAsUserType =
true;
1404 if (s.version() < QDataStream::Qt_6_0) {
1406 if (typeId == QMetaType::User) {
1407 typeId = Qt5UserType;
1408 if (!strcmp(d.type().name(),
"QRegExp")) {
1411 }
else if (typeId > Qt5LastCoreType && typeId <= QMetaType::LastCoreType) {
1413 typeId = Qt5UserType;
1414 saveAsUserType =
true;
1415 }
else if (typeId >= QMetaType::FirstGuiType && typeId <= QMetaType::LastGuiType) {
1416 typeId -= Qt6ToQt5GuiTypeDelta;
1417 if (typeId > Qt5LastGuiType) {
1418 typeId = Qt5UserType;
1419 saveAsUserType =
true;
1421 }
else if (typeId == QMetaType::QSizePolicy) {
1422 typeId = Qt5SizePolicy;
1425 if (s.version() < QDataStream::Qt_4_0) {
1427 for (i = 0; i <= MapFromThreeCount - 1; ++i) {
1428 if (mapIdFromQt3ToCurrent[i] == typeId) {
1433 if (i >= MapFromThreeCount) {
1437 }
else if (s.version() < QDataStream::Qt_5_0) {
1438 if (typeId == Qt5UserType) {
1440 saveAsUserType =
true;
1441 }
else if (typeId >= 128 - 97 && typeId <= Qt5LastCoreType) {
1445 }
else if (typeId == Qt5SizePolicy) {
1447 }
else if (typeId >= Qt5KeySequence && typeId <= Qt5QQuaternion) {
1450 }
else if (typeId > Qt5QQuaternion || typeId == QMetaType::QUuid) {
1453 saveAsUserType =
true;
1456 const char *typeName =
nullptr;
1457 if (saveAsUserType) {
1458 if (s.version() < QDataStream::Qt_6_0)
1459 typeName = QtMetaTypePrivate::typedefNameForType(d.type().d_ptr);
1461 typeName = d.type().name();
1464 if (s.version() >= QDataStream::Qt_4_2)
1465 s << qint8(d.is_null);
1470 if (s.version() < QDataStream::Qt_5_0)
1475 if (!d.type().save(s, constData())) {
1476 qWarning(
"QVariant::save: unable to save type '%s' (type id: %d).\n",
1477 d.type().name(), d.type().rawId());
1478 Q_ASSERT_X(
false,
"QVariant::save",
"Invalid type to save");
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1508
1509
1510
1511
1512
1513QDataStream &operator<<(QDataStream &s,
const QVariant &p)
1520
1521
1522
1523
1524
1527
1528
1529
1530
1531
1535
1536
1537
1538
1539
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551QStringList QVariant::toStringList()
const
1553 return qvariant_cast<QStringList>(*
this);
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571QString QVariant::toString()
const
1573 return qvariant_cast<QString>(*
this);
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587QVariantMap QVariant::toMap()
const
1589 return qvariant_cast<QVariantMap>(*
this);
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603QVariantHash QVariant::toHash()
const
1605 return qvariant_cast<QVariantHash>(*
this);
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620QDate QVariant::toDate()
const
1622 return qvariant_cast<QDate>(*
this);
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637QTime QVariant::toTime()
const
1639 return qvariant_cast<QTime>(*
this);
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654QDateTime QVariant::toDateTime()
const
1656 return qvariant_cast<QDateTime>(*
this);
1660
1661
1662
1663
1664
1665
1666
1667
1668#if QT_CONFIG(easingcurve)
1669QEasingCurve QVariant::toEasingCurve()
const
1671 return qvariant_cast<QEasingCurve>(*
this);
1676
1677
1678
1679
1680
1681
1682
1683
1684QByteArray QVariant::toByteArray()
const
1686 return qvariant_cast<QByteArray>(*
this);
1690
1691
1692
1693
1694
1695
1696
1697
1698QPoint QVariant::toPoint()
const
1700 return qvariant_cast<QPoint>(*
this);
1704
1705
1706
1707
1708
1709
1710
1711QRect QVariant::toRect()
const
1713 return qvariant_cast<QRect>(*
this);
1717
1718
1719
1720
1721
1722
1723
1724QSize QVariant::toSize()
const
1726 return qvariant_cast<QSize>(*
this);
1730
1731
1732
1733
1734
1735
1736
1737QSizeF QVariant::toSizeF()
const
1739 return qvariant_cast<QSizeF>(*
this);
1743
1744
1745
1746
1747
1748
1749
1750
1751QRectF QVariant::toRectF()
const
1753 return qvariant_cast<QRectF>(*
this);
1757
1758
1759
1760
1761
1762
1763
1764QLineF QVariant::toLineF()
const
1766 return qvariant_cast<QLineF>(*
this);
1770
1771
1772
1773
1774
1775
1776
1777QLine QVariant::toLine()
const
1779 return qvariant_cast<QLine>(*
this);
1783
1784
1785
1786
1787
1788
1789
1790
1791QPointF QVariant::toPointF()
const
1793 return qvariant_cast<QPointF>(*
this);
1797
1798
1799
1800
1801
1802
1803
1804QUrl QVariant::toUrl()
const
1806 return qvariant_cast<QUrl>(*
this);
1810
1811
1812
1813
1814
1815
1816
1817QLocale QVariant::toLocale()
const
1819 return qvariant_cast<QLocale>(*
this);
1822#if QT_CONFIG(regularexpression)
1824
1825
1826
1827
1828
1829
1830
1831
1832QRegularExpression QVariant::toRegularExpression()
const
1834 return qvariant_cast<QRegularExpression>(*
this);
1838#if QT_CONFIG(itemmodel)
1840
1841
1842
1843
1844
1845
1846
1847QModelIndex QVariant::toModelIndex()
const
1849 return qvariant_cast<QModelIndex>(*
this);
1853
1854
1855
1856
1857
1858
1859
1860QPersistentModelIndex QVariant::toPersistentModelIndex()
const
1862 return qvariant_cast<QPersistentModelIndex>(*
this);
1867
1868
1869
1870
1871
1872
1873
1874
1875QUuid QVariant::toUuid()
const
1877 return qvariant_cast<QUuid>(*
this);
1881
1882
1883
1884
1885
1886
1887
1888QJsonValue QVariant::toJsonValue()
const
1890 return qvariant_cast<QJsonValue>(*
this);
1894
1895
1896
1897
1898
1899
1900
1901QJsonObject QVariant::toJsonObject()
const
1903 return qvariant_cast<QJsonObject>(*
this);
1907
1908
1909
1910
1911
1912
1913
1914QJsonArray QVariant::toJsonArray()
const
1916 return qvariant_cast<QJsonArray>(*
this);
1920
1921
1922
1923
1924
1925
1926
1927QJsonDocument QVariant::toJsonDocument()
const
1929 return qvariant_cast<QJsonDocument>(*
this);
1933
1934
1935
1936
1937
1938
1939
1940
1941QChar QVariant::toChar()
const
1943 return qvariant_cast<QChar>(*
this);
1947
1948
1949
1950
1951
1952QBitArray QVariant::toBitArray()
const
1954 return qvariant_cast<QBitArray>(*
this);
1957template <
typename T>
1968 bool success = QMetaType::convert(d.type(), d.storage(), t, &ret);
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991int QVariant::toInt(
bool *ok)
const
1993 return qNumVariantToHelper<
int>(d, ok);
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013uint QVariant::toUInt(
bool *ok)
const
2015 return qNumVariantToHelper<uint>(d, ok);
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030qlonglong QVariant::toLongLong(
bool *ok)
const
2032 return qNumVariantToHelper<qlonglong>(d, ok);
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047qulonglong QVariant::toULongLong(
bool *ok)
const
2049 return qNumVariantToHelper<qulonglong>(d, ok);
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064bool QVariant::toBool()
const
2066 auto boolType = QMetaType::fromType<
bool>();
2067 if (d.type() == boolType)
2068 return d.get<
bool>();
2071 QMetaType::convert(d.type(), constData(), boolType, &res);
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087double QVariant::toDouble(
bool *ok)
const
2089 return qNumVariantToHelper<
double>(d, ok);
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106float QVariant::toFloat(
bool *ok)
const
2108 return qNumVariantToHelper<
float>(d, ok);
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125qreal QVariant::toReal(
bool *ok)
const
2127 return qNumVariantToHelper<qreal>(d, ok);
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141QVariantList QVariant::toList()
const
2143 return qvariant_cast<QVariantList>(*
this);
2147
2148
2149
2150
2151
2152
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2215bool QVariant::convert(QMetaType targetType)
2217 if (d.type() == targetType)
2218 return targetType.isValid();
2220 QVariant oldValue = std::exchange(*
this, QVariant::fromMetaType(targetType));
2221 if (!oldValue.canConvert(targetType))
2225 if (oldValue.d.is_null && !oldValue.d.type().isSameType<std::nullptr_t>())
2228 bool ok = QMetaType::convert(oldValue.d.type(), oldValue.constData(), targetType, data());
2233#if QT_REMOVAL_QT7_DEPRECATED_SINCE(6
, 16
)
2235
2236
2237
2238
2239bool QVariant::convert(
int type,
void *ptr)
const
2241 return QMetaType::convert(d.type(), constData(), QMetaType(type), ptr);
2245
2246
2247bool QVariant::view(
int type,
void *ptr)
2249 return QMetaType::view(d.type(), data(), QMetaType(type), ptr);
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2303 static const qulonglong numericTypeBits =
2304 Q_UINT64_C(1) << QMetaType::QString |
2305 Q_UINT64_C(1) << QMetaType::Bool |
2306 Q_UINT64_C(1) << QMetaType::Double |
2307 Q_UINT64_C(1) << QMetaType::Float16 |
2308 Q_UINT64_C(1) << QMetaType::Float |
2309 Q_UINT64_C(1) << QMetaType::Char |
2310 Q_UINT64_C(1) << QMetaType::Char16 |
2311 Q_UINT64_C(1) << QMetaType::Char32 |
2312 Q_UINT64_C(1) << QMetaType::QChar |
2313 Q_UINT64_C(1) << QMetaType::SChar |
2314 Q_UINT64_C(1) << QMetaType::UChar |
2315 Q_UINT64_C(1) << QMetaType::Short |
2316 Q_UINT64_C(1) << QMetaType::UShort |
2317 Q_UINT64_C(1) << QMetaType::Int |
2318 Q_UINT64_C(1) << QMetaType::UInt |
2319 Q_UINT64_C(1) << QMetaType::Long |
2320 Q_UINT64_C(1) << QMetaType::ULong |
2321 Q_UINT64_C(1) << QMetaType::LongLong |
2322 Q_UINT64_C(1) << QMetaType::ULongLong;
2323 return tp < (CHAR_BIT *
sizeof numericTypeBits) ? numericTypeBits & (Q_UINT64_C(1) << tp) :
false;
2328 return tp == QMetaType::Double || tp == QMetaType::Float || tp == QMetaType::Float16;
2334 if (!iface1 || !iface2)
2339 bool isNumeric1 = qIsNumericType(iface1->typeId);
2340 bool isNumeric2 = qIsNumericType(iface2->typeId);
2343 if (isNumeric1 && isNumeric2)
2346 bool isEnum1 = iface1->flags & QMetaType::IsEnumeration;
2347 bool isEnum2 = iface2->flags & QMetaType::IsEnumeration;
2352 if (isEnum1 && isEnum2)
2353 return QMetaType(iface1) == QMetaType(iface2);
2356 if (isEnum1 && isNumeric2)
2358 if (isNumeric1 && isEnum2)
2372 uint t1 = iface1->typeId;
2373 uint t2 = iface2->typeId;
2375 if ((t1 == QMetaType::Bool && t2 == QMetaType::QString) ||
2376 (t2 == QMetaType::Bool && t1 == QMetaType::QString))
2377 return QMetaType::Bool;
2394 if (qIsFloatingPoint(t1) || qIsFloatingPoint(t2))
2395 return QMetaType::QReal;
2399 return tp == QMetaType::ULongLong || tp == QMetaType::ULong ||
2400 tp == QMetaType::UInt || tp == QMetaType::Char32 ||
2401 (iface->flags & QMetaType::IsUnsignedEnumeration && iface->size >=
sizeof(
int));
2403 bool isUnsigned1 = isUnsigned(t1, iface1);
2404 bool isUnsigned2 = isUnsigned(t2, iface2);
2408 if (isUnsigned1 && iface1->size >
sizeof(
int))
2409 return QMetaType::ULongLong;
2410 if (isUnsigned2 && iface2->size >
sizeof(
int))
2411 return QMetaType::ULongLong;
2414 if (iface1->size >
sizeof(
int) || iface2->size >
sizeof(
int))
2415 return QMetaType::LongLong;
2418 if (isUnsigned1 || isUnsigned2)
2419 return QMetaType::UInt;
2422 return QMetaType::Int;
2428 std::optional<qlonglong> l1 = qConvertToNumber(d1, promotedType == QMetaType::Bool);
2429 std::optional<qlonglong> l2 = qConvertToNumber(d2, promotedType == QMetaType::Bool);
2431 return QPartialOrdering::Unordered;
2432 if (promotedType == QMetaType::UInt)
2433 return Qt::compareThreeWay(uint(*l1), uint(*l2));
2434 if (promotedType == QMetaType::LongLong)
2435 return Qt::compareThreeWay(qlonglong(*l1), qlonglong(*l2));
2436 if (promotedType == QMetaType::ULongLong)
2437 return Qt::compareThreeWay(qulonglong(*l1), qulonglong(*l2));
2439 return Qt::compareThreeWay(
int(*l1),
int(*l2));
2444 uint promotedType = numericTypePromotion(d1->typeInterface(), d2->typeInterface());
2445 if (promotedType != QMetaType::QReal)
2446 return integralCompare(promotedType, d1, d2);
2449 const auto r1 = qConvertToRealNumber(d1);
2450 const auto r2 = qConvertToRealNumber(d2);
2452 return QPartialOrdering::Unordered;
2454 return Qt::compareThreeWay(*r1, *r2);
2459 if ((fromType.flags() & QMetaType::PointerToQObject)
2460 && (toType.flags() & QMetaType::PointerToQObject)) {
2461 const QMetaObject *f = fromType.metaObject();
2462 const QMetaObject *t = toType.metaObject();
2463 return f && t && (f->inherits(t) || t->inherits(f));
2470 return Qt::compareThreeWay(Qt::totally_ordered_wrapper(d1->get<QObject *>()),
2471 Qt::totally_ordered_wrapper(d2->get<QObject *>()));
2475
2476
2477bool QVariant::equals(
const QVariant &v)
const
2479 auto metatype = d.type();
2481 if (metatype != v.metaType()) {
2483 if (canBeNumericallyCompared(metatype.iface(), v.d.type().iface()))
2484 return numericCompare(&d, &v.d) == QPartialOrdering::Equivalent;
2486 if (qvCanConvertMetaObject(metatype, v.metaType()))
2487 return pointerCompare(&d, &v.d) == QPartialOrdering::Equivalent;
2492 if (!metatype.isValid())
2495 return metatype.equals(d.storage(), v.d.storage());
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520QPartialOrdering QVariant::compare(
const QVariant &lhs,
const QVariant &rhs)
2522 QMetaType t = lhs.d.type();
2523 if (t != rhs.d.type()) {
2525 if (canBeNumericallyCompared(lhs.d.type().iface(), rhs.d.type().iface()))
2526 return numericCompare(&lhs.d, &rhs.d);
2527 if (qvCanConvertMetaObject(lhs.metaType(), rhs.metaType()))
2528 return pointerCompare(&lhs.d, &rhs.d);
2529 return QPartialOrdering::Unordered;
2531 return t.compare(lhs.constData(), rhs.constData());
2535
2536
2537
2538
2539
2540
2541
2542
2545
2546
2547
2548
2549
2550
2551
2552
2553void *QVariant::data()
2558 return const_cast<
void *>(constData());
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608bool QVariant::isNull()
const
2610 if (d.is_null || !metaType().isValid())
2612 if (metaType().flags() & QMetaType::IsPointer)
2613 return d.get<
void *>() ==
nullptr;
2617#ifndef QT_NO_DEBUG_STREAM
2618QDebug QVariant::qdebugHelper(QDebug dbg)
const
2620 QDebugStateSaver saver(dbg);
2621 const uint typeId = d.type().rawId();
2622 dbg.nospace() <<
"QVariant(";
2623 if (typeId != QMetaType::UnknownType) {
2624 dbg << d.type().name() <<
", ";
2625 bool streamed = d.type().debugStream(dbg, d.storage());
2626 if (!streamed && canConvert<QString>())
2635QVariant QVariant::moveConstruct(QMetaType type,
void *data)
2638 var.d = QVariant::Private(type.d_ptr);
2639 customConstruct<ForceMove, NonNull>(type.d_ptr, &var.d, data);
2643QVariant QVariant::copyConstruct(QMetaType type,
const void *data)
2646 var.d = QVariant::Private(type.d_ptr);
2647 customConstruct<UseCopy, NonNull>(type.d_ptr, &var.d, data);
2651#if QT_DEPRECATED_SINCE(6
, 0
)
2653QT_WARNING_DISABLE_DEPRECATED
2655QDebug operator<<(QDebug dbg,
const QVariant::Type p)
2657 QDebugStateSaver saver(dbg);
2658 dbg.nospace() <<
"QVariant::"
2659 << (
int(p) !=
int(QMetaType::UnknownType)
2660 ? QMetaType(p).name()
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2684
2685
2686
2687
2690
2691
2692
2693
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2752
2753
2754
2755
2756
2757
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2772
2773
2774
2775
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2791
2792
2793
2794
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819QVariant QVariant::fromMetaType(QMetaType type,
const void *copy)
2822 type.registerType();
2823 const auto iface = type.iface();
2824 if (isValidMetaTypeForVariant(iface, copy)) {
2825 result.d = Private(iface);
2826 customConstruct(iface, &result.d, copy);
2832
2833
2834
2835
2836
2837
2838
2839
2840
2843
2844
2845
2846
2847
2848
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2883
2884
2885
2886
2887
2890
2891
2892
2893
2894
2897
2898
2899
2900
2901
2902
2905
2906
2907
2909
2910
2913
2914
2917
2918
2921
2922
2925
2926
2929
2930
2933
2934
2937
2938
2941
2942
2945
2946
2947
2950
2951
2952
2955
2956
2957const void *QtPrivate::QVariantTypeCoercer::convert(
const QVariant &value,
const QMetaType &type)
2959 if (type == QMetaType::fromType<QVariant>())
2962 if (type == value.metaType())
2963 return value.constData();
2965 if (value.canConvert(type)) {
2967 if (converted.convert(type))
2968 return converted.constData();
2975
2976
2977const void *QtPrivate::QVariantTypeCoercer::coerce(
const QVariant &value,
const QMetaType &type)
2979 if (
const void *result = convert(value, type))
2982 converted = QVariant(type);
2983 return converted.constData();
2986#if QT_DEPRECATED_SINCE(6
, 15
)
2988QT_WARNING_DISABLE_DEPRECATED
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3005
3006
3007
3008
3011
3012
3013
3014
3015
3018
3019
3020
3021
3022
3025
3026
3027
3028
3029
3032
3033
3034
3035
3038
3039
3040
3041
3042
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3058
3059
3060QVariantConstPointer::QVariantConstPointer(QVariant variant)
3061 : m_variant(std::move(variant))
3066
3067
3068QVariant QVariantConstPointer::operator*()
const
3074
3075
3076
3077const QVariant *QVariantConstPointer::operator->()
const
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3096
3097
3098
3099
3102
3103
3104
3105
3108
3109
3110
3111
3112
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3133
3134
3135
3136
3139
3140
3141
3142
3145
3146
3147
3148
3151
3152
3153
3154
3158
3159
3160
3161
3162
3163
3164
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3182
3183
3184
3185
3188
3189
3190
3191
3194
3195
3196
3197
3200
3201
3202
3203
3206
3207
3208
3209
3212
3213
3214
3215
3218
3219
3220
3221
3222
3223
3226
3227
3228
3229
3230
3231
3232
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3248
3249
3250
3251
3254
3255
3256
3257
3260
3261
3262
3263
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3279
3280
3281
3282
3285
3286
3287
3288
3291
3292
3293
3294
3297
3298
3299
3300
QDataStream & operator>>(QDataStream &s, QVariant &p)
\keyword 16-bit Floating Point Support\inmodule QtCore \inheaderfile QFloat16
constexpr int Qt6ToQt5GuiTypeDelta
static bool qIsFloatingPoint(uint tp)
static QPartialOrdering numericCompare(const QVariant::Private *d1, const QVariant::Private *d2)
constexpr int Qt5QQuaternion
static bool qIsNumericType(uint tp)
static bool canBeNumericallyCompared(const QtPrivate::QMetaTypeInterface *iface1, const QtPrivate::QMetaTypeInterface *iface2)
constexpr int Qt5KeySequence
static QPartialOrdering integralCompare(uint promotedType, const QVariant::Private *d1, const QVariant::Private *d2)
static const ushort mapIdFromQt3ToCurrent[MapFromThreeCount]
constexpr int Qt5LastCoreType
T qNumVariantToHelper(const QVariant::Private &d, bool *ok)
constexpr int Qt5FirstGuiType
static bool qvCanConvertMetaObject(QMetaType fromType, QMetaType toType)
constexpr int Qt5LastGuiType
static int numericTypePromotion(const QtPrivate::QMetaTypeInterface *iface1, const QtPrivate::QMetaTypeInterface *iface2)
constexpr int Qt5SizePolicy
#define MAKE_CTOR_BY_VALUE(...)
#define MAKE_CTOR_BY_REF(...)
constexpr int Qt5UserType
static QPartialOrdering pointerCompare(const QVariant::Private *d1, const QVariant::Private *d2)