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
492
493
494
495
496
497
498
499
501void QVariant::create(
int type,
const void *copy)
503 create(QMetaType(type), copy);
507
508
509
510
511
512void QVariant::create(QMetaType type,
const void *copy)
514 *
this = QVariant::fromMetaType(type, copy);
518
519
520
521
525 if (!d.is_shared || !d.data.shared->ref.deref())
530
531
532
533
534
536QVariant::QVariant(
const QVariant &p)
537 : d(clonePrivate(p.d))
542
543
544
545
546
547
548
549
550
551
552
553
554
557
558
559
560
561
562
563
564
565
569
570
571
572
573
574
575
576
577
580
581
582
583
584
585
586
587
589QVariant::QVariant(std::in_place_t, QMetaType type) : d(type.iface())
593 if (!Private::canUseInternalSpace(type.iface())) {
594 d.data.shared = PrivateShared::create(type.sizeOf(), type.alignOf());
600
601
602
603
604
605void *QVariant::prepareForEmplace(QMetaType type)
608
609
610
611
612
613 auto typeFits = [&] {
614 auto newIface = type.iface();
615 auto oldIface = d.typeInterface();
616 auto newSize = PrivateShared::computeAllocationSize(newIface->size, newIface->alignment);
617 auto oldSize = PrivateShared::computeAllocationSize(oldIface->size, oldIface->alignment);
618 return newSize <= oldSize;
620 if (Private::canUseInternalSpace(type.iface())) {
622 d.packedType = quintptr(type.iface()) >> 2;
624 }
else if (d.is_shared && isDetached() && typeFits()) {
625 QtMetaTypePrivate::destruct(d.typeInterface(), d.data.shared->data());
627 const auto ps = d.data.shared;
628 const auto align = type.alignOf();
629 ps->offset = PrivateShared::computeOffset(ps, align);
630 d.packedType = quintptr(type.iface()) >> 2;
634 QVariant newVariant(std::in_place, type);
637 return const_cast<
void *>(d.storage());
641
642
643
644
647
648
649
650
653
654
655
656
657
660
661
662
663
664
665
666
667
668
669
670
671
672
675
676
677
678
681
682
683
684
687
688
689
690
693
694
695
696
699
700
701
702
705
706
707
708
711
712
713
714
717
718
719
720
723
724
725
726
729
730
731
732
735
736
737
738
739
742
743
744
745
748
749
750
751
752
755
756
757
758
759
762
763
764
765
766
769
770
771
772
775
776
777
778
779
782
783
784
785
788
789
790
791
792
795
796
797
798
801
802
803
804
805
808
809
810
811
814
815
816
817
818
821
822
823
824
827
828
829
830
833
834
835
836
839
840
841
842
845
846
847
848
851
852
853
854
857
858
859
860
863
864
865
866
869
870
871
872
875
876
877
878
881
882
883
884
887
888
889
890
893
894
895
896
899
900
901
902
905
906
907
908
911
912
913
914
917
918
919
920
923
924
925
926
929
930
931
932
936
937
938
939
942
943
944
945
948
949
950
951
952
955
956
957
958
961
962
963
964
967
968
969
970
973
974
975
976
979
980
981
982
985
986
987
988
989
990
993
994
995
996
999
1000
1001
1002
1003
1004
1005
1006
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026QVariant::QVariant(QMetaType type,
const void *copy)
1027 : QVariant(fromMetaType(type, copy))
1031#define MAKE_CTOR_BY_VALUE(...)
1032 QVariant::QVariant(__VA_ARGS__ val)
1033 noexcept(QVariant::Private::CanUseInternalSpace<__VA_ARGS__>)
1034 : d{std::in_place, std::move(val)} {}
1035 static_assert(std::is_nothrow_copy_constructible_v<__VA_ARGS__>)
1038#define MAKE_CTOR_BY_REF(...)
1039 QVariant::QVariant(__VA_ARGS__ &&val) noexcept
1040 : d{std::in_place, std::move(val)} {}
1041 QVariant::QVariant(const __VA_ARGS__ &val) noexcept
1042 : d{std::in_place, val} {}
1043 static_assert(QVariant::Private::CanUseInternalSpace<__VA_ARGS__>);
1044 static_assert(std::is_nothrow_copy_constructible_v<__VA_ARGS__>);
1045 static_assert(std::is_nothrow_move_constructible_v<__VA_ARGS__>)
1068QVariant::QVariant(QLatin1StringView val) : QVariant(QString(val)) {}
1070#if QT_CONFIG(easingcurve)
1071QVariant::QVariant(
const QEasingCurve &val) : d{std::in_place, val} {}
1072QVariant::QVariant(QEasingCurve &&val)
noexcept : d{std::in_place, std::move(val)} {}
1073static_assert(QVariant::Private::CanUseInternalSpace<QEasingCurve>);
1085#if QT_CONFIG(regularexpression)
1086MAKE_CTOR_BY_REF(QRegularExpression);
1089QVariant::QVariant(
const QJsonValue &jsonValue)
noexcept(Private::FitsInInternalSize<
sizeof(CborValueStandIn)>)
1090 : d{std::in_place, jsonValue}
1091{
static_assert(
sizeof(CborValueStandIn) ==
sizeof(QJsonValue)); }
1092QVariant::QVariant(QJsonValue &&jsonValue)
noexcept(Private::FitsInInternalSize<
sizeof(CborValueStandIn)>)
1093 : d{std::in_place, std::move(jsonValue)} {}
1096QVariant::QVariant(
const QJsonDocument &jsonDocument) : d{std::in_place, jsonDocument} {}
1097QVariant::QVariant(QJsonDocument &&jsonDocument)
noexcept
1098 : d{std::in_place, jsonDocument} {}
1099static_assert(QVariant::Private::CanUseInternalSpace<QJsonDocument>);
1100#if QT_CONFIG(itemmodel)
1101QVariant::QVariant(
const QModelIndex &modelIndex)
noexcept(Private::FitsInInternalSize<8 + 2 *
sizeof(quintptr)>)
1102 : d{std::in_place, modelIndex} {}
1103QVariant::QVariant(
const QPersistentModelIndex &modelIndex)
1104 : d{std::in_place, modelIndex} {}
1105QVariant::QVariant(QPersistentModelIndex &&modelIndex)
noexcept
1106 : d{std::in_place, std::move(modelIndex)} {}
1107static_assert(QVariant::Private::CanUseInternalSpace<QPersistentModelIndex>);
1110#undef MAKE_CTOR_BY_REF
1111#undef MAKE_CTOR_BY_VALUE
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
1147
1150
1151
1152
1153
1154
1155
1156
1159
1160
1161
1162
1163
1166
1167
1168QVariant &QVariant::operator=(
const QVariant &variant)
1170 if (
this == &variant)
1174 d = clonePrivate(variant.d);
1179
1180
1181
1182
1185
1186
1187
1188
1190void QVariant::detach()
1192 if (!d.is_shared || d.data.shared->ref.loadRelaxed() == 1)
1195 Q_ASSERT(isValidMetaTypeForVariant(d.typeInterface(), constData()));
1196 Private dd(d.typeInterface());
1198 customConstruct<UseCopy, NonNull>(d.typeInterface(), &dd, constData());
1199 if (!d.data.shared->ref.deref())
1201 d.data.shared = dd.data.shared;
1205
1206
1207
1208
1211
1212
1213
1214
1215
1216
1217
1220
1221
1222
1223void QVariant::clear()
1225 if (!d.is_shared || !d.data.shared->ref.deref())
1231
1232
1233
1234
1235
1236
1237
1238
1241
1242
1243
1244
1245
1246
1247
1248
1249
1251#ifndef QT_NO_DATASTREAM
1255 QMetaType::UnknownType,
1256 QMetaType::QVariantMap,
1257 QMetaType::QVariantList,
1259 QMetaType::QStringList,
1266 QMetaType::QPalette,
1276 QMetaType::QPolygon,
1280 QMetaType::QSizePolicy,
1283 QMetaType::QDateTime,
1284 QMetaType::QByteArray,
1285 QMetaType::QBitArray,
1286#if QT_CONFIG(shortcut)
1287 QMetaType::QKeySequence,
1292 QMetaType::LongLong,
1293 QMetaType::ULongLong,
1294#if QT_CONFIG(easingcurve)
1295 QMetaType::QEasingCurve
1312
1313
1314
1315
1316
1317void QVariant::load(QDataStream &s)
1323 if (s.version() < QDataStream::Qt_4_0) {
1325 if (typeId >= MapFromThreeCount)
1327 typeId = mapIdFromQt3ToCurrent[typeId];
1328 }
else if (s.version() < QDataStream::Qt_5_0) {
1330 if (typeId == 127 ) {
1331 typeId = Qt5UserType;
1332 }
else if (typeId >= 128 && typeId != Qt5UserType) {
1336 }
else if (typeId == 75 ) {
1337 typeId = Qt5SizePolicy;
1338 }
else if (typeId > 75 && typeId <= 86) {
1344 if (s.version() < QDataStream::Qt_6_0) {
1346 if (typeId == Qt5UserType) {
1347 typeId = QMetaType::User;
1348 }
else if (typeId >= Qt5FirstGuiType && typeId <= Qt5LastGuiType) {
1349 typeId += Qt6ToQt5GuiTypeDelta;
1350 }
else if (typeId == Qt5SizePolicy) {
1351 typeId = QMetaType::QSizePolicy;
1352 }
else if (typeId == Qt5RegExp) {
1353 typeId = QMetaType::fromName(
"QRegExp").rawId();
1357 qint8 is_null =
false;
1358 if (s.version() >= QDataStream::Qt_4_2)
1360 if (typeId == QMetaType::User) {
1363 typeId = QMetaType::fromName(name).rawId();
1364 if (typeId == QMetaType::UnknownType) {
1365 s.setStatus(QDataStream::ReadCorruptData);
1366 qWarning(
"QVariant::load: unknown user type with name %s.", name.constData());
1370 *
this = fromMetaType(QMetaType(typeId));
1371 d.is_null = is_null;
1374 if (s.version() < QDataStream::Qt_5_0) {
1384 void *data =
const_cast<
void *>(constData());
1385 if (!d.type().load(s, data)) {
1386 s.setStatus(QDataStream::ReadCorruptData);
1387 qWarning(
"QVariant::load: unable to load type %d.", d.type().rawId());
1392
1393
1394
1395
1396
1397void QVariant::save(QDataStream &s)
const
1399 quint32 typeId = d.type().rawId();
1400 bool saveAsUserType =
false;
1401 if (typeId >= QMetaType::User) {
1402 typeId = QMetaType::User;
1403 saveAsUserType =
true;
1405 if (s.version() < QDataStream::Qt_6_0) {
1407 if (typeId == QMetaType::User) {
1408 typeId = Qt5UserType;
1409 if (!strcmp(d.type().name(),
"QRegExp")) {
1412 }
else if (typeId > Qt5LastCoreType && typeId <= QMetaType::LastCoreType) {
1414 typeId = Qt5UserType;
1415 saveAsUserType =
true;
1416 }
else if (typeId >= QMetaType::FirstGuiType && typeId <= QMetaType::LastGuiType) {
1417 typeId -= Qt6ToQt5GuiTypeDelta;
1418 if (typeId > Qt5LastGuiType) {
1419 typeId = Qt5UserType;
1420 saveAsUserType =
true;
1422 }
else if (typeId == QMetaType::QSizePolicy) {
1423 typeId = Qt5SizePolicy;
1426 if (s.version() < QDataStream::Qt_4_0) {
1428 for (i = 0; i <= MapFromThreeCount - 1; ++i) {
1429 if (mapIdFromQt3ToCurrent[i] == typeId) {
1434 if (i >= MapFromThreeCount) {
1438 }
else if (s.version() < QDataStream::Qt_5_0) {
1439 if (typeId == Qt5UserType) {
1441 saveAsUserType =
true;
1442 }
else if (typeId >= 128 - 97 && typeId <= Qt5LastCoreType) {
1446 }
else if (typeId == Qt5SizePolicy) {
1448 }
else if (typeId >= Qt5KeySequence && typeId <= Qt5QQuaternion) {
1451 }
else if (typeId > Qt5QQuaternion || typeId == QMetaType::QUuid) {
1454 saveAsUserType =
true;
1457 const char *typeName =
nullptr;
1458 if (saveAsUserType) {
1459 if (s.version() < QDataStream::Qt_6_0)
1460 typeName = QtMetaTypePrivate::typedefNameForType(d.type().d_ptr);
1462 typeName = d.type().name();
1465 if (s.version() >= QDataStream::Qt_4_2)
1466 s << qint8(d.is_null);
1471 if (s.version() < QDataStream::Qt_5_0)
1476 if (!d.type().save(s, constData())) {
1477 qWarning(
"QVariant::save: unable to save type '%s' (type id: %d).\n",
1478 d.type().name(), d.type().rawId());
1479 Q_ASSERT_X(
false,
"QVariant::save",
"Invalid type to save");
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1509
1510
1511
1512
1513
1514QDataStream &operator<<(QDataStream &s,
const QVariant &p)
1521
1522
1523
1524
1525
1528
1529
1530
1531
1532
1536
1537
1538
1539
1540
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552QStringList QVariant::toStringList()
const
1554 return qvariant_cast<QStringList>(*
this);
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572QString QVariant::toString()
const
1574 return qvariant_cast<QString>(*
this);
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588QVariantMap QVariant::toMap()
const
1590 return qvariant_cast<QVariantMap>(*
this);
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604QVariantHash QVariant::toHash()
const
1606 return qvariant_cast<QVariantHash>(*
this);
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621QDate QVariant::toDate()
const
1623 return qvariant_cast<QDate>(*
this);
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638QTime QVariant::toTime()
const
1640 return qvariant_cast<QTime>(*
this);
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655QDateTime QVariant::toDateTime()
const
1657 return qvariant_cast<QDateTime>(*
this);
1661
1662
1663
1664
1665
1666
1667
1668
1669#if QT_CONFIG(easingcurve)
1670QEasingCurve QVariant::toEasingCurve()
const
1672 return qvariant_cast<QEasingCurve>(*
this);
1677
1678
1679
1680
1681
1682
1683
1684
1685QByteArray QVariant::toByteArray()
const
1687 return qvariant_cast<QByteArray>(*
this);
1691
1692
1693
1694
1695
1696
1697
1698
1699QPoint QVariant::toPoint()
const
1701 return qvariant_cast<QPoint>(*
this);
1705
1706
1707
1708
1709
1710
1711
1712QRect QVariant::toRect()
const
1714 return qvariant_cast<QRect>(*
this);
1718
1719
1720
1721
1722
1723
1724
1725QSize QVariant::toSize()
const
1727 return qvariant_cast<QSize>(*
this);
1731
1732
1733
1734
1735
1736
1737
1738QSizeF QVariant::toSizeF()
const
1740 return qvariant_cast<QSizeF>(*
this);
1744
1745
1746
1747
1748
1749
1750
1751
1752QRectF QVariant::toRectF()
const
1754 return qvariant_cast<QRectF>(*
this);
1758
1759
1760
1761
1762
1763
1764
1765QLineF QVariant::toLineF()
const
1767 return qvariant_cast<QLineF>(*
this);
1771
1772
1773
1774
1775
1776
1777
1778QLine QVariant::toLine()
const
1780 return qvariant_cast<QLine>(*
this);
1784
1785
1786
1787
1788
1789
1790
1791
1792QPointF QVariant::toPointF()
const
1794 return qvariant_cast<QPointF>(*
this);
1798
1799
1800
1801
1802
1803
1804
1805QUrl QVariant::toUrl()
const
1807 return qvariant_cast<QUrl>(*
this);
1811
1812
1813
1814
1815
1816
1817
1818QLocale QVariant::toLocale()
const
1820 return qvariant_cast<QLocale>(*
this);
1823#if QT_CONFIG(regularexpression)
1825
1826
1827
1828
1829
1830
1831
1832
1833QRegularExpression QVariant::toRegularExpression()
const
1835 return qvariant_cast<QRegularExpression>(*
this);
1839#if QT_CONFIG(itemmodel)
1841
1842
1843
1844
1845
1846
1847
1848QModelIndex QVariant::toModelIndex()
const
1850 return qvariant_cast<QModelIndex>(*
this);
1854
1855
1856
1857
1858
1859
1860
1861QPersistentModelIndex QVariant::toPersistentModelIndex()
const
1863 return qvariant_cast<QPersistentModelIndex>(*
this);
1868
1869
1870
1871
1872
1873
1874
1875
1876QUuid QVariant::toUuid()
const
1878 return qvariant_cast<QUuid>(*
this);
1882
1883
1884
1885
1886
1887
1888
1889QJsonValue QVariant::toJsonValue()
const
1891 return qvariant_cast<QJsonValue>(*
this);
1895
1896
1897
1898
1899
1900
1901
1902QJsonObject QVariant::toJsonObject()
const
1904 return qvariant_cast<QJsonObject>(*
this);
1908
1909
1910
1911
1912
1913
1914
1915QJsonArray QVariant::toJsonArray()
const
1917 return qvariant_cast<QJsonArray>(*
this);
1921
1922
1923
1924
1925
1926
1927
1928QJsonDocument QVariant::toJsonDocument()
const
1930 return qvariant_cast<QJsonDocument>(*
this);
1934
1935
1936
1937
1938
1939
1940
1941
1942QChar QVariant::toChar()
const
1944 return qvariant_cast<QChar>(*
this);
1948
1949
1950
1951
1952
1953QBitArray QVariant::toBitArray()
const
1955 return qvariant_cast<QBitArray>(*
this);
1958template <
typename T>
1969 bool success = QMetaType::convert(d.type(), d.storage(), t, &ret);
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992int QVariant::toInt(
bool *ok)
const
1994 return qNumVariantToHelper<
int>(d, ok);
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014uint QVariant::toUInt(
bool *ok)
const
2016 return qNumVariantToHelper<uint>(d, ok);
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031qlonglong QVariant::toLongLong(
bool *ok)
const
2033 return qNumVariantToHelper<qlonglong>(d, ok);
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048qulonglong QVariant::toULongLong(
bool *ok)
const
2050 return qNumVariantToHelper<qulonglong>(d, ok);
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065bool QVariant::toBool()
const
2067 auto boolType = QMetaType::fromType<
bool>();
2068 if (d.type() == boolType)
2069 return d.get<
bool>();
2072 QMetaType::convert(d.type(), constData(), boolType, &res);
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088double QVariant::toDouble(
bool *ok)
const
2090 return qNumVariantToHelper<
double>(d, ok);
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107float QVariant::toFloat(
bool *ok)
const
2109 return qNumVariantToHelper<
float>(d, ok);
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126qreal QVariant::toReal(
bool *ok)
const
2128 return qNumVariantToHelper<qreal>(d, ok);
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142QVariantList QVariant::toList()
const
2144 return qvariant_cast<QVariantList>(*
this);
2148
2149
2150
2151
2152
2153
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2216bool QVariant::convert(QMetaType targetType)
2218 if (d.type() == targetType)
2219 return targetType.isValid();
2221 QVariant oldValue = std::exchange(*
this, QVariant::fromMetaType(targetType));
2222 if (!oldValue.canConvert(targetType))
2226 if (oldValue.d.is_null && oldValue.d.type().id() != QMetaType::Nullptr)
2229 bool ok = QMetaType::convert(oldValue.d.type(), oldValue.constData(), targetType, data());
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);
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2302 static const qulonglong numericTypeBits =
2303 Q_UINT64_C(1) << QMetaType::QString |
2304 Q_UINT64_C(1) << QMetaType::Bool |
2305 Q_UINT64_C(1) << QMetaType::Double |
2306 Q_UINT64_C(1) << QMetaType::Float16 |
2307 Q_UINT64_C(1) << QMetaType::Float |
2308 Q_UINT64_C(1) << QMetaType::Char |
2309 Q_UINT64_C(1) << QMetaType::Char16 |
2310 Q_UINT64_C(1) << QMetaType::Char32 |
2311 Q_UINT64_C(1) << QMetaType::SChar |
2312 Q_UINT64_C(1) << QMetaType::UChar |
2313 Q_UINT64_C(1) << QMetaType::Short |
2314 Q_UINT64_C(1) << QMetaType::UShort |
2315 Q_UINT64_C(1) << QMetaType::Int |
2316 Q_UINT64_C(1) << QMetaType::UInt |
2317 Q_UINT64_C(1) << QMetaType::Long |
2318 Q_UINT64_C(1) << QMetaType::ULong |
2319 Q_UINT64_C(1) << QMetaType::LongLong |
2320 Q_UINT64_C(1) << QMetaType::ULongLong;
2321 return tp < (CHAR_BIT *
sizeof numericTypeBits) ? numericTypeBits & (Q_UINT64_C(1) << tp) :
false;
2326 return tp == QMetaType::Double || tp == QMetaType::Float || tp == QMetaType::Float16;
2332 if (!iface1 || !iface2)
2337 bool isNumeric1 = qIsNumericType(iface1->typeId);
2338 bool isNumeric2 = qIsNumericType(iface2->typeId);
2341 if (isNumeric1 && isNumeric2)
2344 bool isEnum1 = iface1->flags & QMetaType::IsEnumeration;
2345 bool isEnum2 = iface2->flags & QMetaType::IsEnumeration;
2350 if (isEnum1 && isEnum2)
2351 return QMetaType(iface1) == QMetaType(iface2);
2354 if (isEnum1 && isNumeric2)
2356 if (isNumeric1 && isEnum2)
2370 uint t1 = iface1->typeId;
2371 uint t2 = iface2->typeId;
2373 if ((t1 == QMetaType::Bool && t2 == QMetaType::QString) ||
2374 (t2 == QMetaType::Bool && t1 == QMetaType::QString))
2375 return QMetaType::Bool;
2392 if (qIsFloatingPoint(t1) || qIsFloatingPoint(t2))
2393 return QMetaType::QReal;
2397 return tp == QMetaType::ULongLong || tp == QMetaType::ULong ||
2398 tp == QMetaType::UInt || tp == QMetaType::Char32 ||
2399 (iface->flags & QMetaType::IsUnsignedEnumeration && iface->size >=
sizeof(
int));
2401 bool isUnsigned1 = isUnsigned(t1, iface1);
2402 bool isUnsigned2 = isUnsigned(t2, iface2);
2406 if (isUnsigned1 && iface1->size >
sizeof(
int))
2407 return QMetaType::ULongLong;
2408 if (isUnsigned2 && iface2->size >
sizeof(
int))
2409 return QMetaType::ULongLong;
2412 if (iface1->size >
sizeof(
int) || iface2->size >
sizeof(
int))
2413 return QMetaType::LongLong;
2416 if (isUnsigned1 || isUnsigned2)
2417 return QMetaType::UInt;
2420 return QMetaType::Int;
2426 std::optional<qlonglong> l1 = qConvertToNumber(d1, promotedType == QMetaType::Bool);
2427 std::optional<qlonglong> l2 = qConvertToNumber(d2, promotedType == QMetaType::Bool);
2429 return QPartialOrdering::Unordered;
2430 if (promotedType == QMetaType::UInt)
2431 return Qt::compareThreeWay(uint(*l1), uint(*l2));
2432 if (promotedType == QMetaType::LongLong)
2433 return Qt::compareThreeWay(qlonglong(*l1), qlonglong(*l2));
2434 if (promotedType == QMetaType::ULongLong)
2435 return Qt::compareThreeWay(qulonglong(*l1), qulonglong(*l2));
2437 return Qt::compareThreeWay(
int(*l1),
int(*l2));
2442 uint promotedType = numericTypePromotion(d1->typeInterface(), d2->typeInterface());
2443 if (promotedType != QMetaType::QReal)
2444 return integralCompare(promotedType, d1, d2);
2447 const auto r1 = qConvertToRealNumber(d1);
2448 const auto r2 = qConvertToRealNumber(d2);
2450 return QPartialOrdering::Unordered;
2452 return Qt::compareThreeWay(*r1, *r2);
2457 if ((fromType.flags() & QMetaType::PointerToQObject)
2458 && (toType.flags() & QMetaType::PointerToQObject)) {
2459 const QMetaObject *f = fromType.metaObject();
2460 const QMetaObject *t = toType.metaObject();
2461 return f && t && (f->inherits(t) || t->inherits(f));
2468 return Qt::compareThreeWay(Qt::totally_ordered_wrapper(d1->get<QObject *>()),
2469 Qt::totally_ordered_wrapper(d2->get<QObject *>()));
2473
2474
2475bool QVariant::equals(
const QVariant &v)
const
2477 auto metatype = d.type();
2479 if (metatype != v.metaType()) {
2481 if (canBeNumericallyCompared(metatype.iface(), v.d.type().iface()))
2482 return numericCompare(&d, &v.d) == QPartialOrdering::Equivalent;
2484 if (qvCanConvertMetaObject(metatype, v.metaType()))
2485 return pointerCompare(&d, &v.d) == QPartialOrdering::Equivalent;
2490 if (!metatype.isValid())
2493 return metatype.equals(d.storage(), v.d.storage());
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518QPartialOrdering QVariant::compare(
const QVariant &lhs,
const QVariant &rhs)
2520 QMetaType t = lhs.d.type();
2521 if (t != rhs.d.type()) {
2523 if (canBeNumericallyCompared(lhs.d.type().iface(), rhs.d.type().iface()))
2524 return numericCompare(&lhs.d, &rhs.d);
2525 if (qvCanConvertMetaObject(lhs.metaType(), rhs.metaType()))
2526 return pointerCompare(&lhs.d, &rhs.d);
2527 return QPartialOrdering::Unordered;
2529 return t.compare(lhs.constData(), rhs.constData());
2533
2534
2535
2536
2537
2538
2539
2540
2543
2544
2545
2546
2547
2548
2549
2550
2551void *QVariant::data()
2556 return const_cast<
void *>(constData());
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606bool QVariant::isNull()
const
2608 if (d.is_null || !metaType().isValid())
2610 if (metaType().flags() & QMetaType::IsPointer)
2611 return d.get<
void *>() ==
nullptr;
2615#ifndef QT_NO_DEBUG_STREAM
2616QDebug QVariant::qdebugHelper(QDebug dbg)
const
2618 QDebugStateSaver saver(dbg);
2619 const uint typeId = d.type().rawId();
2620 dbg.nospace() <<
"QVariant(";
2621 if (typeId != QMetaType::UnknownType) {
2622 dbg << d.type().name() <<
", ";
2623 bool streamed = d.type().debugStream(dbg, d.storage());
2624 if (!streamed && canConvert<QString>())
2633QVariant QVariant::moveConstruct(QMetaType type,
void *data)
2636 var.d = QVariant::Private(type.d_ptr);
2637 customConstruct<ForceMove, NonNull>(type.d_ptr, &var.d, data);
2641QVariant QVariant::copyConstruct(QMetaType type,
const void *data)
2644 var.d = QVariant::Private(type.d_ptr);
2645 customConstruct<UseCopy, NonNull>(type.d_ptr, &var.d, data);
2649#if QT_DEPRECATED_SINCE(6
, 0
)
2651QT_WARNING_DISABLE_DEPRECATED
2653QDebug operator<<(QDebug dbg,
const QVariant::Type p)
2655 QDebugStateSaver saver(dbg);
2656 dbg.nospace() <<
"QVariant::"
2657 << (
int(p) !=
int(QMetaType::UnknownType)
2658 ? QMetaType(p).name()
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2682
2683
2684
2685
2688
2689
2690
2691
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2750
2751
2752
2753
2754
2755
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2770
2771
2772
2773
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2789
2790
2791
2792
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817QVariant QVariant::fromMetaType(QMetaType type,
const void *copy)
2820 type.registerType();
2821 const auto iface = type.iface();
2822 if (isValidMetaTypeForVariant(iface, copy)) {
2823 result.d = Private(iface);
2824 customConstruct(iface, &result.d, copy);
2830
2831
2832
2833
2834
2835
2836
2837
2838
2841
2842
2843
2844
2845
2846
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2881
2882
2883
2884
2885
2888
2889
2890
2891
2892
2895
2896
2897
2898
2899
2900
2903
2904
2905
2907
2908
2911
2912
2915
2916
2919
2920
2923
2924
2927
2928
2931
2932
2935
2936
2939
2940
2943
2944
2945
2948
2949
2950
2953
2954
2955const void *QtPrivate::QVariantTypeCoercer::convert(
const QVariant &value,
const QMetaType &type)
2957 if (type == QMetaType::fromType<QVariant>())
2960 if (type == value.metaType())
2961 return value.constData();
2963 if (value.canConvert(type)) {
2965 if (converted.convert(type))
2966 return converted.constData();
2973
2974
2975const void *QtPrivate::QVariantTypeCoercer::coerce(
const QVariant &value,
const QMetaType &type)
2977 if (
const void *result = convert(value, type))
2980 converted = QVariant(type);
2981 return converted.constData();
2984#if QT_DEPRECATED_SINCE(6
, 15
)
2986QT_WARNING_DISABLE_DEPRECATED
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3003
3004
3005
3006
3009
3010
3011
3012
3013
3016
3017
3018
3019
3020
3023
3024
3025
3026
3027
3030
3031
3032
3033
3036
3037
3038
3039
3040
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3056
3057
3058QVariantConstPointer::QVariantConstPointer(QVariant variant)
3059 : m_variant(std::move(variant))
3064
3065
3066QVariant QVariantConstPointer::operator*()
const
3072
3073
3074
3075const QVariant *QVariantConstPointer::operator->()
const
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3094
3095
3096
3097
3100
3101
3102
3103
3106
3107
3108
3109
3110
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3131
3132
3133
3134
3137
3138
3139
3140
3143
3144
3145
3146
3149
3150
3151
3152
3156
3157
3158
3159
3160
3161
3162
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3180
3181
3182
3183
3186
3187
3188
3189
3192
3193
3194
3195
3198
3199
3200
3201
3204
3205
3206
3207
3210
3211
3212
3213
3216
3217
3218
3219
3220
3221
3224
3225
3226
3227
3228
3229
3230
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3246
3247
3248
3249
3252
3253
3254
3255
3258
3259
3260
3261
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3277
3278
3279
3280
3283
3284
3285
3286
3289
3290
3291
3292
3295
3296
3297
3298
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)