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
510
511
512void QVariant::create(QMetaType type,
const void *copy)
514 *
this = QVariant::fromMetaType(type, copy);
519
520
521
522
526 if (!d.is_shared || !d.data.shared->ref.deref())
531
532
533
534
535
537QVariant::QVariant(
const QVariant &p)
538 : d(clonePrivate(p.d))
543
544
545
546
547
548
549
550
551
552
553
554
555
558
559
560
561
562
563
564
565
566
570
571
572
573
574
575
576
577
578
581
582
583
584
585
586
587
588
590QVariant::QVariant(std::in_place_t, QMetaType type) : d(type.iface())
594 if (!Private::canUseInternalSpace(type.iface())) {
595 d.data.shared = PrivateShared::create(type.sizeOf(), type.alignOf());
601
602
603
604
605
606void *QVariant::prepareForEmplace(QMetaType type)
609
610
611
612
613
614 auto typeFits = [&] {
615 auto newIface = type.iface();
616 auto oldIface = d.typeInterface();
617 auto newSize = PrivateShared::computeAllocationSize(newIface->size, newIface->alignment);
618 auto oldSize = PrivateShared::computeAllocationSize(oldIface->size, oldIface->alignment);
619 return newSize <= oldSize;
621 if (Private::canUseInternalSpace(type.iface())) {
623 d.packedType = quintptr(type.iface()) >> 2;
625 }
else if (d.is_shared && isDetached() && typeFits()) {
626 QtMetaTypePrivate::destruct(d.typeInterface(), d.data.shared->data());
628 const auto ps = d.data.shared;
629 const auto align = type.alignOf();
630 ps->offset = PrivateShared::computeOffset(ps, align);
631 d.packedType = quintptr(type.iface()) >> 2;
635 QVariant newVariant(std::in_place, type);
638 return const_cast<
void *>(d.storage());
642
643
644
645
648
649
650
651
654
655
656
657
658
661
662
663
664
665
666
667
668
669
670
671
672
673
676
677
678
679
682
683
684
685
688
689
690
691
694
695
696
697
700
701
702
703
706
707
708
709
712
713
714
715
718
719
720
721
724
725
726
727
730
731
732
733
736
737
738
739
740
743
744
745
746
749
750
751
752
753
756
757
758
759
760
763
764
765
766
767
770
771
772
773
776
777
778
779
780
783
784
785
786
789
790
791
792
793
796
797
798
799
802
803
804
805
806
809
810
811
812
815
816
817
818
819
822
823
824
825
828
829
830
831
834
835
836
837
840
841
842
843
846
847
848
849
852
853
854
855
858
859
860
861
864
865
866
867
870
871
872
873
876
877
878
879
882
883
884
885
888
889
890
891
894
895
896
897
900
901
902
903
906
907
908
909
912
913
914
915
918
919
920
921
924
925
926
927
930
931
932
933
937
938
939
940
943
944
945
946
949
950
951
952
953
956
957
958
959
962
963
964
965
968
969
970
971
974
975
976
977
980
981
982
983
986
987
988
989
990
991
994
995
996
997
1000
1001
1002
1003
1004
1005
1006
1007
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027QVariant::QVariant(QMetaType type,
const void *copy)
1028 : QVariant(fromMetaType(type, copy))
1032#define MAKE_CTOR_BY_VALUE(...)
1033 QVariant::QVariant(__VA_ARGS__ val)
1034 noexcept(QVariant::Private::CanUseInternalSpace<__VA_ARGS__>)
1035 : d{std::in_place, std::move(val)} {}
1036 static_assert(std::is_nothrow_copy_constructible_v<__VA_ARGS__>)
1039#define MAKE_CTOR_BY_REF(...)
1040 QVariant::QVariant(__VA_ARGS__ &&val) noexcept
1041 : d{std::in_place, std::move(val)} {}
1042 QVariant::QVariant(const __VA_ARGS__ &val) noexcept
1043 : d{std::in_place, val} {}
1044 static_assert(QVariant::Private::CanUseInternalSpace<__VA_ARGS__>);
1045 static_assert(std::is_nothrow_copy_constructible_v<__VA_ARGS__>);
1046 static_assert(std::is_nothrow_move_constructible_v<__VA_ARGS__>)
1069QVariant::QVariant(QLatin1StringView val) : QVariant(QString(val)) {}
1071#if QT_CONFIG(easingcurve)
1072QVariant::QVariant(
const QEasingCurve &val) : d{std::in_place, val} {}
1073QVariant::QVariant(QEasingCurve &&val)
noexcept : d{std::in_place, std::move(val)} {}
1074static_assert(QVariant::Private::CanUseInternalSpace<QEasingCurve>);
1086#if QT_CONFIG(regularexpression)
1087MAKE_CTOR_BY_REF(QRegularExpression);
1090QVariant::QVariant(
const QJsonValue &jsonValue)
noexcept(Private::FitsInInternalSize<
sizeof(CborValueStandIn)>)
1091 : d{std::in_place, jsonValue}
1092{
static_assert(
sizeof(CborValueStandIn) ==
sizeof(QJsonValue)); }
1093QVariant::QVariant(QJsonValue &&jsonValue)
noexcept(Private::FitsInInternalSize<
sizeof(CborValueStandIn)>)
1094 : d{std::in_place, std::move(jsonValue)} {}
1097QVariant::QVariant(
const QJsonDocument &jsonDocument) : d{std::in_place, jsonDocument} {}
1098QVariant::QVariant(QJsonDocument &&jsonDocument)
noexcept
1099 : d{std::in_place, jsonDocument} {}
1100static_assert(QVariant::Private::CanUseInternalSpace<QJsonDocument>);
1101#if QT_CONFIG(itemmodel)
1102QVariant::QVariant(
const QModelIndex &modelIndex)
noexcept(Private::FitsInInternalSize<8 + 2 *
sizeof(quintptr)>)
1103 : d{std::in_place, modelIndex} {}
1104QVariant::QVariant(
const QPersistentModelIndex &modelIndex)
1105 : d{std::in_place, modelIndex} {}
1106QVariant::QVariant(QPersistentModelIndex &&modelIndex)
noexcept
1107 : d{std::in_place, std::move(modelIndex)} {}
1108static_assert(QVariant::Private::CanUseInternalSpace<QPersistentModelIndex>);
1111#undef MAKE_CTOR_BY_REF
1112#undef MAKE_CTOR_BY_VALUE
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
1147
1148
1151
1152
1153
1154
1155
1156
1157
1160
1161
1162
1163
1164
1167
1168
1169QVariant &QVariant::operator=(
const QVariant &variant)
1171 if (
this == &variant)
1175 d = clonePrivate(variant.d);
1180
1181
1182
1183
1186
1187
1188
1189
1191void QVariant::detach()
1193 if (!d.is_shared || d.data.shared->ref.loadRelaxed() == 1)
1196 Q_ASSERT(isValidMetaTypeForVariant(d.typeInterface(), constData()));
1197 Private dd(d.typeInterface());
1199 customConstruct<UseCopy, NonNull>(d.typeInterface(), &dd, constData());
1200 if (!d.data.shared->ref.deref())
1202 d.data.shared = dd.data.shared;
1206
1207
1208
1209
1212
1213
1214
1215
1216
1217
1218
1221
1222
1223
1224void QVariant::clear()
1226 if (!d.is_shared || !d.data.shared->ref.deref())
1232
1233
1234
1235
1236
1237
1238
1239
1242
1243
1244
1245
1246
1247
1248
1249
1250
1252#ifndef QT_NO_DATASTREAM
1256 QMetaType::UnknownType,
1257 QMetaType::QVariantMap,
1258 QMetaType::QVariantList,
1260 QMetaType::QStringList,
1267 QMetaType::QPalette,
1277 QMetaType::QPolygon,
1281 QMetaType::QSizePolicy,
1284 QMetaType::QDateTime,
1285 QMetaType::QByteArray,
1286 QMetaType::QBitArray,
1287#if QT_CONFIG(shortcut)
1288 QMetaType::QKeySequence,
1293 QMetaType::LongLong,
1294 QMetaType::ULongLong,
1295#if QT_CONFIG(easingcurve)
1296 QMetaType::QEasingCurve
1313
1314
1315
1316
1317
1318void QVariant::load(QDataStream &s)
1324 if (s.version() < QDataStream::Qt_4_0) {
1326 if (typeId >= MapFromThreeCount)
1328 typeId = mapIdFromQt3ToCurrent[typeId];
1329 }
else if (s.version() < QDataStream::Qt_5_0) {
1331 if (typeId == 127 ) {
1332 typeId = Qt5UserType;
1333 }
else if (typeId >= 128 && typeId != Qt5UserType) {
1337 }
else if (typeId == 75 ) {
1338 typeId = Qt5SizePolicy;
1339 }
else if (typeId > 75 && typeId <= 86) {
1345 if (s.version() < QDataStream::Qt_6_0) {
1347 if (typeId == Qt5UserType) {
1348 typeId = QMetaType::User;
1349 }
else if (typeId >= Qt5FirstGuiType && typeId <= Qt5LastGuiType) {
1350 typeId += Qt6ToQt5GuiTypeDelta;
1351 }
else if (typeId == Qt5SizePolicy) {
1352 typeId = QMetaType::QSizePolicy;
1353 }
else if (typeId == Qt5RegExp) {
1354 typeId = QMetaType::fromName(
"QRegExp").rawId();
1358 qint8 is_null =
false;
1359 if (s.version() >= QDataStream::Qt_4_2)
1361 if (typeId == QMetaType::User) {
1364 typeId = QMetaType::fromName(name).rawId();
1365 if (typeId == QMetaType::UnknownType) {
1366 s.setStatus(QDataStream::ReadCorruptData);
1367 qWarning(
"QVariant::load: unknown user type with name %s.", name.constData());
1371 *
this = fromMetaType(QMetaType(typeId));
1372 d.is_null = is_null;
1375 if (s.version() < QDataStream::Qt_5_0) {
1385 void *data =
const_cast<
void *>(constData());
1386 if (!d.type().load(s, data)) {
1387 s.setStatus(QDataStream::ReadCorruptData);
1388 qWarning(
"QVariant::load: unable to load type %d.", d.type().rawId());
1393
1394
1395
1396
1397
1398void QVariant::save(QDataStream &s)
const
1400 quint32 typeId = d.type().rawId();
1401 bool saveAsUserType =
false;
1402 if (typeId >= QMetaType::User) {
1403 typeId = QMetaType::User;
1404 saveAsUserType =
true;
1406 if (s.version() < QDataStream::Qt_6_0) {
1408 if (typeId == QMetaType::User) {
1409 typeId = Qt5UserType;
1410 if (!strcmp(d.type().name(),
"QRegExp")) {
1413 }
else if (typeId > Qt5LastCoreType && typeId <= QMetaType::LastCoreType) {
1415 typeId = Qt5UserType;
1416 saveAsUserType =
true;
1417 }
else if (typeId >= QMetaType::FirstGuiType && typeId <= QMetaType::LastGuiType) {
1418 typeId -= Qt6ToQt5GuiTypeDelta;
1419 if (typeId > Qt5LastGuiType) {
1420 typeId = Qt5UserType;
1421 saveAsUserType =
true;
1423 }
else if (typeId == QMetaType::QSizePolicy) {
1424 typeId = Qt5SizePolicy;
1427 if (s.version() < QDataStream::Qt_4_0) {
1429 for (i = 0; i <= MapFromThreeCount - 1; ++i) {
1430 if (mapIdFromQt3ToCurrent[i] == typeId) {
1435 if (i >= MapFromThreeCount) {
1439 }
else if (s.version() < QDataStream::Qt_5_0) {
1440 if (typeId == Qt5UserType) {
1442 saveAsUserType =
true;
1443 }
else if (typeId >= 128 - 97 && typeId <= Qt5LastCoreType) {
1447 }
else if (typeId == Qt5SizePolicy) {
1449 }
else if (typeId >= Qt5KeySequence && typeId <= Qt5QQuaternion) {
1452 }
else if (typeId > Qt5QQuaternion || typeId == QMetaType::QUuid) {
1455 saveAsUserType =
true;
1458 const char *typeName =
nullptr;
1459 if (saveAsUserType) {
1460 if (s.version() < QDataStream::Qt_6_0)
1461 typeName = QtMetaTypePrivate::typedefNameForType(d.type().d_ptr);
1463 typeName = d.type().name();
1466 if (s.version() >= QDataStream::Qt_4_2)
1467 s << qint8(d.is_null);
1472 if (s.version() < QDataStream::Qt_5_0)
1477 if (!d.type().save(s, constData())) {
1478 qWarning(
"QVariant::save: unable to save type '%s' (type id: %d).\n",
1479 d.type().name(), d.type().rawId());
1480 Q_ASSERT_X(
false,
"QVariant::save",
"Invalid type to save");
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1510
1511
1512
1513
1514
1515QDataStream &operator<<(QDataStream &s,
const QVariant &p)
1522
1523
1524
1525
1526
1529
1530
1531
1532
1533
1537
1538
1539
1540
1541
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553QStringList QVariant::toStringList()
const
1555 return qvariant_cast<QStringList>(*
this);
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573QString QVariant::toString()
const
1575 return qvariant_cast<QString>(*
this);
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589QVariantMap QVariant::toMap()
const
1591 return qvariant_cast<QVariantMap>(*
this);
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605QVariantHash QVariant::toHash()
const
1607 return qvariant_cast<QVariantHash>(*
this);
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622QDate QVariant::toDate()
const
1624 return qvariant_cast<QDate>(*
this);
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639QTime QVariant::toTime()
const
1641 return qvariant_cast<QTime>(*
this);
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656QDateTime QVariant::toDateTime()
const
1658 return qvariant_cast<QDateTime>(*
this);
1662
1663
1664
1665
1666
1667
1668
1669
1670#if QT_CONFIG(easingcurve)
1671QEasingCurve QVariant::toEasingCurve()
const
1673 return qvariant_cast<QEasingCurve>(*
this);
1678
1679
1680
1681
1682
1683
1684
1685
1686QByteArray QVariant::toByteArray()
const
1688 return qvariant_cast<QByteArray>(*
this);
1692
1693
1694
1695
1696
1697
1698
1699
1700QPoint QVariant::toPoint()
const
1702 return qvariant_cast<QPoint>(*
this);
1706
1707
1708
1709
1710
1711
1712
1713QRect QVariant::toRect()
const
1715 return qvariant_cast<QRect>(*
this);
1719
1720
1721
1722
1723
1724
1725
1726QSize QVariant::toSize()
const
1728 return qvariant_cast<QSize>(*
this);
1732
1733
1734
1735
1736
1737
1738
1739QSizeF QVariant::toSizeF()
const
1741 return qvariant_cast<QSizeF>(*
this);
1745
1746
1747
1748
1749
1750
1751
1752
1753QRectF QVariant::toRectF()
const
1755 return qvariant_cast<QRectF>(*
this);
1759
1760
1761
1762
1763
1764
1765
1766QLineF QVariant::toLineF()
const
1768 return qvariant_cast<QLineF>(*
this);
1772
1773
1774
1775
1776
1777
1778
1779QLine QVariant::toLine()
const
1781 return qvariant_cast<QLine>(*
this);
1785
1786
1787
1788
1789
1790
1791
1792
1793QPointF QVariant::toPointF()
const
1795 return qvariant_cast<QPointF>(*
this);
1799
1800
1801
1802
1803
1804
1805
1806QUrl QVariant::toUrl()
const
1808 return qvariant_cast<QUrl>(*
this);
1812
1813
1814
1815
1816
1817
1818
1819QLocale QVariant::toLocale()
const
1821 return qvariant_cast<QLocale>(*
this);
1824#if QT_CONFIG(regularexpression)
1826
1827
1828
1829
1830
1831
1832
1833
1834QRegularExpression QVariant::toRegularExpression()
const
1836 return qvariant_cast<QRegularExpression>(*
this);
1840#if QT_CONFIG(itemmodel)
1842
1843
1844
1845
1846
1847
1848
1849QModelIndex QVariant::toModelIndex()
const
1851 return qvariant_cast<QModelIndex>(*
this);
1855
1856
1857
1858
1859
1860
1861
1862QPersistentModelIndex QVariant::toPersistentModelIndex()
const
1864 return qvariant_cast<QPersistentModelIndex>(*
this);
1869
1870
1871
1872
1873
1874
1875
1876
1877QUuid QVariant::toUuid()
const
1879 return qvariant_cast<QUuid>(*
this);
1883
1884
1885
1886
1887
1888
1889
1890QJsonValue QVariant::toJsonValue()
const
1892 return qvariant_cast<QJsonValue>(*
this);
1896
1897
1898
1899
1900
1901
1902
1903QJsonObject QVariant::toJsonObject()
const
1905 return qvariant_cast<QJsonObject>(*
this);
1909
1910
1911
1912
1913
1914
1915
1916QJsonArray QVariant::toJsonArray()
const
1918 return qvariant_cast<QJsonArray>(*
this);
1922
1923
1924
1925
1926
1927
1928
1929QJsonDocument QVariant::toJsonDocument()
const
1931 return qvariant_cast<QJsonDocument>(*
this);
1935
1936
1937
1938
1939
1940
1941
1942
1943QChar QVariant::toChar()
const
1945 return qvariant_cast<QChar>(*
this);
1949
1950
1951
1952
1953
1954QBitArray QVariant::toBitArray()
const
1956 return qvariant_cast<QBitArray>(*
this);
1959template <
typename T>
1970 bool success = QMetaType::convert(d.type(), d.storage(), t, &ret);
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993int QVariant::toInt(
bool *ok)
const
1995 return qNumVariantToHelper<
int>(d, ok);
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015uint QVariant::toUInt(
bool *ok)
const
2017 return qNumVariantToHelper<uint>(d, ok);
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032qlonglong QVariant::toLongLong(
bool *ok)
const
2034 return qNumVariantToHelper<qlonglong>(d, ok);
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049qulonglong QVariant::toULongLong(
bool *ok)
const
2051 return qNumVariantToHelper<qulonglong>(d, ok);
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066bool QVariant::toBool()
const
2068 auto boolType = QMetaType::fromType<
bool>();
2069 if (d.type() == boolType)
2070 return d.get<
bool>();
2073 QMetaType::convert(d.type(), constData(), boolType, &res);
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089double QVariant::toDouble(
bool *ok)
const
2091 return qNumVariantToHelper<
double>(d, ok);
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108float QVariant::toFloat(
bool *ok)
const
2110 return qNumVariantToHelper<
float>(d, ok);
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127qreal QVariant::toReal(
bool *ok)
const
2129 return qNumVariantToHelper<qreal>(d, ok);
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143QVariantList QVariant::toList()
const
2145 return qvariant_cast<QVariantList>(*
this);
2149
2150
2151
2152
2153
2154
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2217bool QVariant::convert(QMetaType targetType)
2219 if (d.type() == targetType)
2220 return targetType.isValid();
2222 QVariant oldValue = std::exchange(*
this, QVariant::fromMetaType(targetType));
2223 if (!oldValue.canConvert(targetType))
2227 if (oldValue.d.is_null && oldValue.d.type().id() != QMetaType::Nullptr)
2230 bool ok = QMetaType::convert(oldValue.d.type(), oldValue.constData(), targetType, data());
2235#if QT_REMOVAL_QT7_DEPRECATED_SINCE(6
, 16
)
2237
2238
2239
2240
2241bool QVariant::convert(
int type,
void *ptr)
const
2243 return QMetaType::convert(d.type(), constData(), QMetaType(type), ptr);
2247
2248
2249bool QVariant::view(
int type,
void *ptr)
2251 return QMetaType::view(d.type(), data(), QMetaType(type), ptr);
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2305 static const qulonglong numericTypeBits =
2306 Q_UINT64_C(1) << QMetaType::QString |
2307 Q_UINT64_C(1) << QMetaType::Bool |
2308 Q_UINT64_C(1) << QMetaType::Double |
2309 Q_UINT64_C(1) << QMetaType::Float16 |
2310 Q_UINT64_C(1) << QMetaType::Float |
2311 Q_UINT64_C(1) << QMetaType::Char |
2312 Q_UINT64_C(1) << QMetaType::Char16 |
2313 Q_UINT64_C(1) << QMetaType::Char32 |
2314 Q_UINT64_C(1) << QMetaType::SChar |
2315 Q_UINT64_C(1) << QMetaType::UChar |
2316 Q_UINT64_C(1) << QMetaType::Short |
2317 Q_UINT64_C(1) << QMetaType::UShort |
2318 Q_UINT64_C(1) << QMetaType::Int |
2319 Q_UINT64_C(1) << QMetaType::UInt |
2320 Q_UINT64_C(1) << QMetaType::Long |
2321 Q_UINT64_C(1) << QMetaType::ULong |
2322 Q_UINT64_C(1) << QMetaType::LongLong |
2323 Q_UINT64_C(1) << QMetaType::ULongLong;
2324 return tp < (CHAR_BIT *
sizeof numericTypeBits) ? numericTypeBits & (Q_UINT64_C(1) << tp) :
false;
2329 return tp == QMetaType::Double || tp == QMetaType::Float || tp == QMetaType::Float16;
2335 if (!iface1 || !iface2)
2340 bool isNumeric1 = qIsNumericType(iface1->typeId);
2341 bool isNumeric2 = qIsNumericType(iface2->typeId);
2344 if (isNumeric1 && isNumeric2)
2347 bool isEnum1 = iface1->flags & QMetaType::IsEnumeration;
2348 bool isEnum2 = iface2->flags & QMetaType::IsEnumeration;
2353 if (isEnum1 && isEnum2)
2354 return QMetaType(iface1) == QMetaType(iface2);
2357 if (isEnum1 && isNumeric2)
2359 if (isNumeric1 && isEnum2)
2373 uint t1 = iface1->typeId;
2374 uint t2 = iface2->typeId;
2376 if ((t1 == QMetaType::Bool && t2 == QMetaType::QString) ||
2377 (t2 == QMetaType::Bool && t1 == QMetaType::QString))
2378 return QMetaType::Bool;
2395 if (qIsFloatingPoint(t1) || qIsFloatingPoint(t2))
2396 return QMetaType::QReal;
2400 return tp == QMetaType::ULongLong || tp == QMetaType::ULong ||
2401 tp == QMetaType::UInt || tp == QMetaType::Char32 ||
2402 (iface->flags & QMetaType::IsUnsignedEnumeration && iface->size >=
sizeof(
int));
2404 bool isUnsigned1 = isUnsigned(t1, iface1);
2405 bool isUnsigned2 = isUnsigned(t2, iface2);
2409 if (isUnsigned1 && iface1->size >
sizeof(
int))
2410 return QMetaType::ULongLong;
2411 if (isUnsigned2 && iface2->size >
sizeof(
int))
2412 return QMetaType::ULongLong;
2415 if (iface1->size >
sizeof(
int) || iface2->size >
sizeof(
int))
2416 return QMetaType::LongLong;
2419 if (isUnsigned1 || isUnsigned2)
2420 return QMetaType::UInt;
2423 return QMetaType::Int;
2429 std::optional<qlonglong> l1 = qConvertToNumber(d1, promotedType == QMetaType::Bool);
2430 std::optional<qlonglong> l2 = qConvertToNumber(d2, promotedType == QMetaType::Bool);
2432 return QPartialOrdering::Unordered;
2433 if (promotedType == QMetaType::UInt)
2434 return Qt::compareThreeWay(uint(*l1), uint(*l2));
2435 if (promotedType == QMetaType::LongLong)
2436 return Qt::compareThreeWay(qlonglong(*l1), qlonglong(*l2));
2437 if (promotedType == QMetaType::ULongLong)
2438 return Qt::compareThreeWay(qulonglong(*l1), qulonglong(*l2));
2440 return Qt::compareThreeWay(
int(*l1),
int(*l2));
2445 uint promotedType = numericTypePromotion(d1->typeInterface(), d2->typeInterface());
2446 if (promotedType != QMetaType::QReal)
2447 return integralCompare(promotedType, d1, d2);
2450 const auto r1 = qConvertToRealNumber(d1);
2451 const auto r2 = qConvertToRealNumber(d2);
2453 return QPartialOrdering::Unordered;
2455 return Qt::compareThreeWay(*r1, *r2);
2460 if ((fromType.flags() & QMetaType::PointerToQObject)
2461 && (toType.flags() & QMetaType::PointerToQObject)) {
2462 const QMetaObject *f = fromType.metaObject();
2463 const QMetaObject *t = toType.metaObject();
2464 return f && t && (f->inherits(t) || t->inherits(f));
2471 return Qt::compareThreeWay(Qt::totally_ordered_wrapper(d1->get<QObject *>()),
2472 Qt::totally_ordered_wrapper(d2->get<QObject *>()));
2476
2477
2478bool QVariant::equals(
const QVariant &v)
const
2480 auto metatype = d.type();
2482 if (metatype != v.metaType()) {
2484 if (canBeNumericallyCompared(metatype.iface(), v.d.type().iface()))
2485 return numericCompare(&d, &v.d) == QPartialOrdering::Equivalent;
2487 if (qvCanConvertMetaObject(metatype, v.metaType()))
2488 return pointerCompare(&d, &v.d) == QPartialOrdering::Equivalent;
2493 if (!metatype.isValid())
2496 return metatype.equals(d.storage(), v.d.storage());
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521QPartialOrdering QVariant::compare(
const QVariant &lhs,
const QVariant &rhs)
2523 QMetaType t = lhs.d.type();
2524 if (t != rhs.d.type()) {
2526 if (canBeNumericallyCompared(lhs.d.type().iface(), rhs.d.type().iface()))
2527 return numericCompare(&lhs.d, &rhs.d);
2528 if (qvCanConvertMetaObject(lhs.metaType(), rhs.metaType()))
2529 return pointerCompare(&lhs.d, &rhs.d);
2530 return QPartialOrdering::Unordered;
2532 return t.compare(lhs.constData(), rhs.constData());
2536
2537
2538
2539
2540
2541
2542
2543
2546
2547
2548
2549
2550
2551
2552
2553
2554void *QVariant::data()
2559 return const_cast<
void *>(constData());
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609bool QVariant::isNull()
const
2611 if (d.is_null || !metaType().isValid())
2613 if (metaType().flags() & QMetaType::IsPointer)
2614 return d.get<
void *>() ==
nullptr;
2618#ifndef QT_NO_DEBUG_STREAM
2619QDebug QVariant::qdebugHelper(QDebug dbg)
const
2621 QDebugStateSaver saver(dbg);
2622 const uint typeId = d.type().rawId();
2623 dbg.nospace() <<
"QVariant(";
2624 if (typeId != QMetaType::UnknownType) {
2625 dbg << d.type().name() <<
", ";
2626 bool streamed = d.type().debugStream(dbg, d.storage());
2627 if (!streamed && canConvert<QString>())
2636QVariant QVariant::moveConstruct(QMetaType type,
void *data)
2639 var.d = QVariant::Private(type.d_ptr);
2640 customConstruct<ForceMove, NonNull>(type.d_ptr, &var.d, data);
2644QVariant QVariant::copyConstruct(QMetaType type,
const void *data)
2647 var.d = QVariant::Private(type.d_ptr);
2648 customConstruct<UseCopy, NonNull>(type.d_ptr, &var.d, data);
2652#if QT_DEPRECATED_SINCE(6
, 0
)
2654QT_WARNING_DISABLE_DEPRECATED
2656QDebug operator<<(QDebug dbg,
const QVariant::Type p)
2658 QDebugStateSaver saver(dbg);
2659 dbg.nospace() <<
"QVariant::"
2660 << (
int(p) !=
int(QMetaType::UnknownType)
2661 ? QMetaType(p).name()
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2685
2686
2687
2688
2691
2692
2693
2694
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
2722
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2753
2754
2755
2756
2757
2758
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2773
2774
2775
2776
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2792
2793
2794
2795
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820QVariant QVariant::fromMetaType(QMetaType type,
const void *copy)
2823 type.registerType();
2824 const auto iface = type.iface();
2825 if (isValidMetaTypeForVariant(iface, copy)) {
2826 result.d = Private(iface);
2827 customConstruct(iface, &result.d, copy);
2833
2834
2835
2836
2837
2838
2839
2840
2841
2844
2845
2846
2847
2848
2849
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2884
2885
2886
2887
2888
2891
2892
2893
2894
2895
2898
2899
2900
2901
2902
2903
2906
2907
2908
2910
2911
2914
2915
2918
2919
2922
2923
2926
2927
2930
2931
2934
2935
2938
2939
2942
2943
2946
2947
2948
2951
2952
2953
2956
2957
2958const void *QtPrivate::QVariantTypeCoercer::convert(
const QVariant &value,
const QMetaType &type)
2960 if (type == QMetaType::fromType<QVariant>())
2963 if (type == value.metaType())
2964 return value.constData();
2966 if (value.canConvert(type)) {
2968 if (converted.convert(type))
2969 return converted.constData();
2976
2977
2978const void *QtPrivate::QVariantTypeCoercer::coerce(
const QVariant &value,
const QMetaType &type)
2980 if (
const void *result = convert(value, type))
2983 converted = QVariant(type);
2984 return converted.constData();
2987#if QT_DEPRECATED_SINCE(6
, 15
)
2989QT_WARNING_DISABLE_DEPRECATED
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3006
3007
3008
3009
3012
3013
3014
3015
3016
3019
3020
3021
3022
3023
3026
3027
3028
3029
3030
3033
3034
3035
3036
3039
3040
3041
3042
3043
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3059
3060
3061QVariantConstPointer::QVariantConstPointer(QVariant variant)
3062 : m_variant(std::move(variant))
3067
3068
3069QVariant QVariantConstPointer::operator*()
const
3075
3076
3077
3078const QVariant *QVariantConstPointer::operator->()
const
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3097
3098
3099
3100
3103
3104
3105
3106
3109
3110
3111
3112
3113
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3134
3135
3136
3137
3140
3141
3142
3143
3146
3147
3148
3149
3152
3153
3154
3155
3159
3160
3161
3162
3163
3164
3165
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3183
3184
3185
3186
3189
3190
3191
3192
3195
3196
3197
3198
3201
3202
3203
3204
3207
3208
3209
3210
3213
3214
3215
3216
3219
3220
3221
3222
3223
3224
3227
3228
3229
3230
3231
3232
3233
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3249
3250
3251
3252
3255
3256
3257
3258
3261
3262
3263
3264
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3280
3281
3282
3283
3286
3287
3288
3289
3292
3293
3294
3295
3298
3299
3300
3301
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)