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"
54#define BFLOAT16_ENABLED 0
56using namespace Qt::StringLiterals;
60static qlonglong qMetaTypeNumberBySize(
const QVariant::Private *d)
62 switch (d->typeInterface()->size) {
64 return d->get<
signed char>();
66 return d->get<
short>();
70 return d->get<qlonglong>();
72 Q_UNREACHABLE_RETURN(0);
75static qlonglong qMetaTypeNumber(
const QVariant::Private *d)
77 switch (d->typeInterface()->typeId.loadRelaxed()) {
80 case QMetaType::LongLong:
81 return d->get<qlonglong>();
83 case QMetaType::SChar:
84 return d->get<
signed char>();
85 case QMetaType::Short:
86 return d->get<
short>();
88 return d->get<
long>();
89 case QMetaType::Float16:
92 case QMetaType::BFloat16:
93 return qRound64(
float(d->get<std::bfloat16_t>()));
95 case QMetaType::Float:
96 return qRound64(d->get<
float>());
97 case QMetaType::Double:
98 return qRound64(d->get<
double>());
99 case QMetaType::QJsonValue:
100 return d->get<QJsonValue>().toDouble();
101 case QMetaType::QCborValue:
102 return d->get<QCborValue>().toInteger();
104 Q_UNREACHABLE_RETURN(0);
107static qulonglong qMetaTypeUNumber(
const QVariant::Private *d)
109 if constexpr (QSysInfo::ByteOrder == QSysInfo::LittleEndian) {
112 return d->get<qulonglong>();
114 switch (d->typeInterface()->size) {
116 return d->get<
unsigned char>();
118 return d->get<
unsigned short>();
120 return d->get<
unsigned int>();
122 return d->get<qulonglong>();
124 Q_UNREACHABLE_RETURN(0);
127static std::optional<qlonglong> qConvertToNumber(
const QVariant::Private *d,
bool allowStringToBool =
false)
130 switch (d->typeInterface()->typeId.loadRelaxed()) {
131 case QMetaType::QString: {
132 const QString &s = d->get<QString>();
133 if (qlonglong l = s.toLongLong(&ok); ok)
135 if (allowStringToBool) {
136 if (s ==
"false"_L1 || s ==
"0"_L1)
138 if (s ==
"true"_L1 || s ==
"1"_L1)
143 case QMetaType::QChar:
144 return d->get<QChar>().unicode();
145 case QMetaType::QByteArray:
146 if (qlonglong l = d->get<
QByteArray>().toLongLong(&ok); ok)
149 case QMetaType::Bool:
150 return qlonglong(d->get<
bool>());
151 case QMetaType::QCborValue:
152 if (!d->get<QCborValue>().isInteger() && !d->get<QCborValue>().isDouble())
154 return qMetaTypeNumber(d);
155 case QMetaType::QJsonValue:
156 if (!d->get<QJsonValue>().isDouble())
159 case QMetaType::Double:
161 case QMetaType::Char:
162 case QMetaType::SChar:
163 case QMetaType::Short:
164 case QMetaType::Long:
165 case QMetaType::Float:
166 case QMetaType::Float16:
168 case QMetaType::BFloat16:
170 case QMetaType::LongLong:
171 return qMetaTypeNumber(d);
172 case QMetaType::ULongLong:
173 case QMetaType::UInt:
174 case QMetaType::UChar:
175 case QMetaType::Char16:
176 case QMetaType::Char32:
177 case QMetaType::UShort:
178 case QMetaType::ULong:
179 return qlonglong(qMetaTypeUNumber(d));
180 case QMetaType::QCborSimpleType:
184 if (d->typeInterface()->flags & QMetaType::IsUnsignedEnumeration)
185 return qMetaTypeUNumber(d);
186 if (d->typeInterface()->flags & QMetaType::IsEnumeration)
187 return qMetaTypeNumberBySize(d);
192static std::optional<
double> qConvertToRealNumber(
const QVariant::Private *d)
195 switch (d->typeInterface()->typeId.loadRelaxed()) {
196 case QMetaType::QString:
197 if (
double r = d->get<QString>().toDouble(&ok); ok)
200 case QMetaType::Double:
201 return d->get<
double>();
202 case QMetaType::Float:
203 return double(d->get<
float>());
204 case QMetaType::Float16:
207 case QMetaType::BFloat16:
208 return qreal(d->get<std::bfloat16_t>());
210 case QMetaType::ULongLong:
211 case QMetaType::UInt:
212 case QMetaType::UChar:
213 case QMetaType::Char16:
214 case QMetaType::Char32:
215 case QMetaType::UShort:
216 case QMetaType::ULong:
217 return double(qMetaTypeUNumber(d));
218 case QMetaType::QCborValue:
219 return d->get<QCborValue>().toDouble();
220 case QMetaType::QJsonValue:
221 return d->get<QJsonValue>().toDouble();
224 if (std::optional<qlonglong> l = qConvertToNumber(d))
233 if (!iface || iface->size == 0)
236 Q_ASSERT(!isInterfaceFor<
void>(iface));
240 qWarning(
"QVariant: Provided metatype for '%s' does not support destruction and "
241 "copy construction", iface
->name);
247 qWarning(
"QVariant: Cannot create type '%s' without a default constructor", iface
->name);
254enum CustomConstructMoveOptions {
260enum CustomConstructNullabilityOption {
267template <CustomConstructMoveOptions moveOption = UseCopy, CustomConstructNullabilityOption nullability = MaybeNull>
269 std::conditional_t<moveOption == ForceMove,
void *,
const void *> copy)
273 Q_ASSERT(iface->size);
274 Q_ASSERT(!isInterfaceFor<
void>(iface));
278 if constexpr (moveOption == ForceMove)
280 if constexpr (nullability == NonNull)
281 Q_ASSERT(copy !=
nullptr);
286 d->is_null = !copy QT6_ONLY(|| isInterfaceFor<std::nullptr_t>(iface));
288 if (QVariant::Private::canUseInternalSpace(iface)) {
289 d->is_shared =
false;
292 if constexpr (moveOption == ForceMove && nullability == NonNull)
293 moveConstruct(iface, d->data.data, copy);
295 construct(iface, d->data.data, copy);
297 d->data.shared = customConstructShared(iface->size, iface->alignment, [=](
void *where) {
298 if constexpr (moveOption == ForceMove && nullability == NonNull)
299 moveConstruct(iface, where, copy);
301 construct(iface, where, copy);
307static void customClear(QVariant::Private *d)
316 QVariant::PrivateShared::free(d->data.shared);
320static QVariant::Private clonePrivate(
const QVariant::Private &other)
322 QVariant::Private d = other;
324 d.data.shared->ref.ref();
326 if (Q_LIKELY(d.canUseInternalSpace(iface))) {
334 d.data.shared = QVariant::PrivateShared::create(iface->size, iface->alignment);
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
495
496
497
498
499
500
501
504
505
506
507
508
509
512
513
514
515
517#if QT_REMOVAL_QT7_DEPRECATED_SINCE(6
, 16
)
519
520
521
522
523
524
525
526
527void QVariant::create(
int type,
const void *copy)
529 *
this = QVariant::fromMetaType(QMetaType(type), copy);
533
534
535
536void QVariant::create(QMetaType type,
const void *copy)
538 *
this = QVariant::fromMetaType(type, copy);
543
544
545
546
550 if (!d.is_shared || !d.data.shared->ref.deref())
555
556
557
558
559
561QVariant::QVariant(
const QVariant &p)
562 : d(clonePrivate(p.d))
567
568
569
570
571
572
573
574
575
576
577
578
579
582
583
584
585
586
587
588
589
590
594
595
596
597
598
599
600
601
602
605
606
607
608
609
610
611
612
614QVariant::QVariant(std::in_place_t, QMetaType type) : d(type.iface())
618 if (!Private::canUseInternalSpace(type.iface())) {
619 d.data.shared = PrivateShared::create(type.sizeOf(), type.alignOf());
625
626
627
628
629
630void *QVariant::prepareForEmplace(QMetaType type)
633
634
635
636
637
638 auto typeFits = [&] {
639 auto newIface = type.iface();
640 auto oldIface = d.typeInterface();
641 auto newSize = PrivateShared::computeAllocationSize(newIface->size, newIface->alignment);
642 auto oldSize = PrivateShared::computeAllocationSize(oldIface->size, oldIface->alignment);
643 return newSize <= oldSize;
645 if (Private::canUseInternalSpace(type.iface())) {
647 d.packedType = quintptr(type.iface()) >> 2;
649 }
else if (d.is_shared && isDetached() && typeFits()) {
650 QtMetaTypePrivate::destruct(d.typeInterface(), d.data.shared->data());
652 const auto ps = d.data.shared;
653 const auto align = type.alignOf();
654 ps->offset = PrivateShared::computeOffset(ps, align);
655 d.packedType = quintptr(type.iface()) >> 2;
659 QVariant newVariant(std::in_place, type);
662 return const_cast<
void *>(d.storage());
666
667
668
669
672
673
674
675
678
679
680
681
682
685
686
687
688
689
690
691
692
693
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
742
743
744
745
748
749
750
751
754
755
756
757
760
761
762
763
764
767
768
769
770
773
774
775
776
777
780
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
830
833
834
835
836
839
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
936
937
938
939
942
943
944
945
948
949
950
951
954
955
956
957
961
962
963
964
967
968
969
970
973
974
975
976
977
980
981
982
983
986
987
988
989
992
993
994
995
998
999
1000
1001
1004
1005
1006
1007
1010
1011
1012
1013
1014
1015
1018
1019
1020
1021
1024
1025
1026
1027
1028
1029
1030
1031
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051QVariant::QVariant(QMetaType type,
const void *copy)
1052 : QVariant(fromMetaType(type, copy))
1056#define MAKE_CTOR_BY_VALUE(...)
1057 QVariant::QVariant(__VA_ARGS__ val)
1058 noexcept(QVariant::Private::CanUseInternalSpace<__VA_ARGS__>)
1059 : d{std::in_place, std::move(val)} {}
1060 static_assert(std::is_nothrow_copy_constructible_v<__VA_ARGS__>)
1063#define MAKE_CTOR_BY_REF(...)
1064 QVariant::QVariant(__VA_ARGS__ &&val) noexcept
1065 : d{std::in_place, std::move(val)} {}
1066 QVariant::QVariant(const __VA_ARGS__ &val) noexcept
1067 : d{std::in_place, val} {}
1068 static_assert(QVariant::Private::CanUseInternalSpace<__VA_ARGS__>);
1069 static_assert(std::is_nothrow_copy_constructible_v<__VA_ARGS__>);
1070 static_assert(std::is_nothrow_move_constructible_v<__VA_ARGS__>)
1093QVariant::QVariant(QLatin1StringView val) : QVariant(QString(val)) {}
1095#if QT_CONFIG(easingcurve)
1096QVariant::QVariant(
const QEasingCurve &val) : d{std::in_place, val} {}
1097QVariant::QVariant(QEasingCurve &&val)
noexcept : d{std::in_place, std::move(val)} {}
1098static_assert(QVariant::Private::CanUseInternalSpace<QEasingCurve>);
1110#if QT_CONFIG(regularexpression)
1111MAKE_CTOR_BY_REF(QRegularExpression);
1114QVariant::QVariant(
const QJsonValue &jsonValue)
noexcept(Private::FitsInInternalSize<
sizeof(CborValueStandIn)>)
1115 : d{std::in_place, jsonValue}
1116{
static_assert(
sizeof(CborValueStandIn) ==
sizeof(QJsonValue)); }
1117QVariant::QVariant(QJsonValue &&jsonValue)
noexcept(Private::FitsInInternalSize<
sizeof(CborValueStandIn)>)
1118 : d{std::in_place, std::move(jsonValue)} {}
1121QVariant::QVariant(
const QJsonDocument &jsonDocument) : d{std::in_place, jsonDocument} {}
1122QVariant::QVariant(QJsonDocument &&jsonDocument)
noexcept
1123 : d{std::in_place, jsonDocument} {}
1124static_assert(QVariant::Private::CanUseInternalSpace<QJsonDocument>);
1125#if QT_CONFIG(itemmodel)
1126QVariant::QVariant(
const QModelIndex &modelIndex)
noexcept(Private::FitsInInternalSize<8 + 2 *
sizeof(quintptr)>)
1127 : d{std::in_place, modelIndex} {}
1128QVariant::QVariant(
const QPersistentModelIndex &modelIndex)
1129 : d{std::in_place, modelIndex} {}
1130QVariant::QVariant(QPersistentModelIndex &&modelIndex)
noexcept
1131 : d{std::in_place, std::move(modelIndex)} {}
1132static_assert(QVariant::Private::CanUseInternalSpace<QPersistentModelIndex>);
1135#undef MAKE_CTOR_BY_REF
1136#undef MAKE_CTOR_BY_VALUE
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1175
1176
1177
1178
1179
1180
1181
1184
1185
1186
1187
1188
1191
1192
1193QVariant &QVariant::operator=(
const QVariant &variant)
1195 if (
this == &variant)
1199 d = clonePrivate(variant.d);
1204
1205
1206
1207
1210
1211
1212
1213
1215void QVariant::detach()
1217 if (!d.is_shared || d.data.shared->ref.loadRelaxed() == 1)
1220 Q_ASSERT(isValidMetaTypeForVariant(d.typeInterface(), constData()));
1221 Private dd(d.typeInterface());
1223 customConstruct<UseCopy, NonNull>(d.typeInterface(), &dd, constData());
1224 if (!d.data.shared->ref.deref())
1226 d.data.shared = dd.data.shared;
1230
1231
1232
1233
1236
1237
1238
1239
1240
1241
1242
1245
1246
1247
1248void QVariant::clear()
1250 if (!d.is_shared || !d.data.shared->ref.deref())
1256
1257
1258
1259
1260
1261
1262
1263
1266
1267
1268
1269
1270
1271
1272
1273
1274
1276#ifndef QT_NO_DATASTREAM
1280 QMetaType::UnknownType,
1281 QMetaType::QVariantMap,
1282 QMetaType::QVariantList,
1284 QMetaType::QStringList,
1291 QMetaType::QPalette,
1301 QMetaType::QPolygon,
1305 QMetaType::QSizePolicy,
1308 QMetaType::QDateTime,
1309 QMetaType::QByteArray,
1310 QMetaType::QBitArray,
1311#if QT_CONFIG(shortcut)
1312 QMetaType::QKeySequence,
1317 QMetaType::LongLong,
1318 QMetaType::ULongLong,
1319#if QT_CONFIG(easingcurve)
1320 QMetaType::QEasingCurve
1337
1338
1339
1340
1341
1342void QVariant::load(QDataStream &s)
1348 if (s.version() < QDataStream::Qt_4_0) {
1350 if (typeId >= MapFromThreeCount)
1352 typeId = mapIdFromQt3ToCurrent[typeId];
1353 }
else if (s.version() < QDataStream::Qt_5_0) {
1355 if (typeId == 127 ) {
1356 typeId = Qt5UserType;
1357 }
else if (typeId >= 128 && typeId != Qt5UserType) {
1361 }
else if (typeId == 75 ) {
1362 typeId = Qt5SizePolicy;
1363 }
else if (typeId > 75 && typeId <= 86) {
1369 if (s.version() < QDataStream::Qt_6_0) {
1371 if (typeId == Qt5UserType) {
1372 typeId = QMetaType::User;
1373 }
else if (typeId >= Qt5FirstGuiType && typeId <= Qt5LastGuiType) {
1374 typeId += Qt6ToQt5GuiTypeDelta;
1375 }
else if (typeId == Qt5SizePolicy) {
1376 typeId = QMetaType::QSizePolicy;
1377 }
else if (typeId == Qt5RegExp) {
1378 typeId = QMetaType::fromName(
"QRegExp").rawId();
1382 qint8 is_null =
false;
1383 if (s.version() >= QDataStream::Qt_4_2)
1385 if (typeId == QMetaType::User) {
1388 typeId = QMetaType::fromName(name).rawId();
1389 if (typeId == QMetaType::UnknownType) {
1390 s.setStatus(QDataStream::ReadCorruptData);
1391 qWarning(
"QVariant::load: unknown user type with name %s.", name.constData());
1395 *
this = fromMetaType(QMetaType(typeId));
1396 d.is_null = is_null;
1399 if (s.version() < QDataStream::Qt_5_0) {
1409 void *data =
const_cast<
void *>(constData());
1410 if (!d.type().load(s, data)) {
1411 s.setStatus(QDataStream::ReadCorruptData);
1412 qWarning(
"QVariant::load: unable to load type %d.", d.type().rawId());
1417
1418
1419
1420
1421
1422void QVariant::save(QDataStream &s)
const
1424 quint32 typeId = d.type().rawId();
1425 bool saveAsUserType =
false;
1426 if (typeId >= QMetaType::User) {
1427 typeId = QMetaType::User;
1428 saveAsUserType =
true;
1430 if (s.version() < QDataStream::Qt_6_0) {
1432 if (typeId == QMetaType::User) {
1433 typeId = Qt5UserType;
1434 if (!strcmp(d.type().name(),
"QRegExp")) {
1437 }
else if (typeId > Qt5LastCoreType && typeId <= QMetaType::LastCoreType) {
1439 typeId = Qt5UserType;
1440 saveAsUserType =
true;
1441 }
else if (typeId >= QMetaType::FirstGuiType && typeId <= QMetaType::LastGuiType) {
1442 typeId -= Qt6ToQt5GuiTypeDelta;
1443 if (typeId > Qt5LastGuiType) {
1444 typeId = Qt5UserType;
1445 saveAsUserType =
true;
1447 }
else if (typeId == QMetaType::QSizePolicy) {
1448 typeId = Qt5SizePolicy;
1451 if (s.version() < QDataStream::Qt_4_0) {
1453 for (i = 0; i <= MapFromThreeCount - 1; ++i) {
1454 if (mapIdFromQt3ToCurrent[i] == typeId) {
1459 if (i >= MapFromThreeCount) {
1463 }
else if (s.version() < QDataStream::Qt_5_0) {
1464 if (typeId == Qt5UserType) {
1466 saveAsUserType =
true;
1467 }
else if (typeId >= 128 - 97 && typeId <= Qt5LastCoreType) {
1471 }
else if (typeId == Qt5SizePolicy) {
1473 }
else if (typeId >= Qt5KeySequence && typeId <= Qt5QQuaternion) {
1476 }
else if (typeId > Qt5QQuaternion || typeId == QMetaType::QUuid) {
1479 saveAsUserType =
true;
1482 const char *typeName =
nullptr;
1483 if (saveAsUserType) {
1484 if (s.version() < QDataStream::Qt_6_0)
1485 typeName = QtMetaTypePrivate::typedefNameForType(d.type().d_ptr);
1487 typeName = d.type().name();
1490 if (s.version() >= QDataStream::Qt_4_2)
1491 s << qint8(d.is_null);
1496 if (s.version() < QDataStream::Qt_5_0)
1501 if (!d.type().save(s, constData())) {
1502 qWarning(
"QVariant::save: unable to save type '%s' (type id: %d).\n",
1503 d.type().name(), d.type().rawId());
1504 Q_ASSERT_X(
false,
"QVariant::save",
"Invalid type to save");
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1534
1535
1536
1537
1538
1539QDataStream &operator<<(QDataStream &s,
const QVariant &p)
1546
1547
1548
1549
1550
1553
1554
1555
1556
1557
1561
1562
1563
1564
1565
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577QStringList QVariant::toStringList()
const
1579 return qvariant_cast<QStringList>(*
this);
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597QString QVariant::toString()
const
1599 return qvariant_cast<QString>(*
this);
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613QVariantMap QVariant::toMap()
const
1615 return qvariant_cast<QVariantMap>(*
this);
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629QVariantHash QVariant::toHash()
const
1631 return qvariant_cast<QVariantHash>(*
this);
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646QDate QVariant::toDate()
const
1648 return qvariant_cast<QDate>(*
this);
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663QTime QVariant::toTime()
const
1665 return qvariant_cast<QTime>(*
this);
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680QDateTime QVariant::toDateTime()
const
1682 return qvariant_cast<QDateTime>(*
this);
1686
1687
1688
1689
1690
1691
1692
1693
1694#if QT_CONFIG(easingcurve)
1695QEasingCurve QVariant::toEasingCurve()
const
1697 return qvariant_cast<QEasingCurve>(*
this);
1702
1703
1704
1705
1706
1707
1708
1709
1710QByteArray QVariant::toByteArray()
const
1712 return qvariant_cast<QByteArray>(*
this);
1716
1717
1718
1719
1720
1721
1722
1723
1724QPoint QVariant::toPoint()
const
1726 return qvariant_cast<QPoint>(*
this);
1730
1731
1732
1733
1734
1735
1736
1737QRect QVariant::toRect()
const
1739 return qvariant_cast<QRect>(*
this);
1743
1744
1745
1746
1747
1748
1749
1750QSize QVariant::toSize()
const
1752 return qvariant_cast<QSize>(*
this);
1756
1757
1758
1759
1760
1761
1762
1763QSizeF QVariant::toSizeF()
const
1765 return qvariant_cast<QSizeF>(*
this);
1769
1770
1771
1772
1773
1774
1775
1776
1777QRectF QVariant::toRectF()
const
1779 return qvariant_cast<QRectF>(*
this);
1783
1784
1785
1786
1787
1788
1789
1790QLineF QVariant::toLineF()
const
1792 return qvariant_cast<QLineF>(*
this);
1796
1797
1798
1799
1800
1801
1802
1803QLine QVariant::toLine()
const
1805 return qvariant_cast<QLine>(*
this);
1809
1810
1811
1812
1813
1814
1815
1816
1817QPointF QVariant::toPointF()
const
1819 return qvariant_cast<QPointF>(*
this);
1823
1824
1825
1826
1827
1828
1829
1830QUrl QVariant::toUrl()
const
1832 return qvariant_cast<QUrl>(*
this);
1836
1837
1838
1839
1840
1841
1842
1843QLocale QVariant::toLocale()
const
1845 return qvariant_cast<QLocale>(*
this);
1848#if QT_CONFIG(regularexpression)
1850
1851
1852
1853
1854
1855
1856
1857
1858QRegularExpression QVariant::toRegularExpression()
const
1860 return qvariant_cast<QRegularExpression>(*
this);
1864#if QT_CONFIG(itemmodel)
1866
1867
1868
1869
1870
1871
1872
1873QModelIndex QVariant::toModelIndex()
const
1875 return qvariant_cast<QModelIndex>(*
this);
1879
1880
1881
1882
1883
1884
1885
1886QPersistentModelIndex QVariant::toPersistentModelIndex()
const
1888 return qvariant_cast<QPersistentModelIndex>(*
this);
1893
1894
1895
1896
1897
1898
1899
1900
1901QUuid QVariant::toUuid()
const
1903 return qvariant_cast<QUuid>(*
this);
1907
1908
1909
1910
1911
1912
1913
1914QJsonValue QVariant::toJsonValue()
const
1916 return qvariant_cast<QJsonValue>(*
this);
1920
1921
1922
1923
1924
1925
1926
1927QJsonObject QVariant::toJsonObject()
const
1929 return qvariant_cast<QJsonObject>(*
this);
1933
1934
1935
1936
1937
1938
1939
1940QJsonArray QVariant::toJsonArray()
const
1942 return qvariant_cast<QJsonArray>(*
this);
1946
1947
1948
1949
1950
1951
1952
1953QJsonDocument QVariant::toJsonDocument()
const
1955 return qvariant_cast<QJsonDocument>(*
this);
1959
1960
1961
1962
1963
1964
1965
1966
1967QChar QVariant::toChar()
const
1969 return qvariant_cast<QChar>(*
this);
1973
1974
1975
1976
1977
1978QBitArray QVariant::toBitArray()
const
1980 return qvariant_cast<QBitArray>(*
this);
1983template <
typename T>
1994 bool success = QMetaType::convert(d.type(), d.storage(), t, &ret);
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017int QVariant::toInt(
bool *ok)
const
2019 return qNumVariantToHelper<
int>(d, ok);
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039uint QVariant::toUInt(
bool *ok)
const
2041 return qNumVariantToHelper<uint>(d, ok);
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056qlonglong QVariant::toLongLong(
bool *ok)
const
2058 return qNumVariantToHelper<qlonglong>(d, ok);
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073qulonglong QVariant::toULongLong(
bool *ok)
const
2075 return qNumVariantToHelper<qulonglong>(d, ok);
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090bool QVariant::toBool()
const
2092 auto boolType = QMetaType::fromType<
bool>();
2093 if (d.type() == boolType)
2094 return d.get<
bool>();
2097 QMetaType::convert(d.type(), constData(), boolType, &res);
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113double QVariant::toDouble(
bool *ok)
const
2115 return qNumVariantToHelper<
double>(d, ok);
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132float QVariant::toFloat(
bool *ok)
const
2134 return qNumVariantToHelper<
float>(d, ok);
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151qreal QVariant::toReal(
bool *ok)
const
2153 return qNumVariantToHelper<qreal>(d, ok);
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167QVariantList QVariant::toList()
const
2169 return qvariant_cast<QVariantList>(*
this);
2173
2174
2175
2176
2177
2178
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2241bool QVariant::convert(QMetaType targetType)
2243 if (d.type() == targetType)
2244 return targetType.isValid();
2246 QVariant oldValue = std::exchange(*
this, QVariant::fromMetaType(targetType));
2247 if (!oldValue.canConvert(targetType))
2251 if (oldValue.d.is_null && !oldValue.d.type().isSameType<std::nullptr_t>())
2254 bool ok = QMetaType::convert(oldValue.d.type(), oldValue.constData(), targetType, data());
2259#if QT_REMOVAL_QT7_DEPRECATED_SINCE(6
, 16
)
2261
2262
2263
2264
2265bool QVariant::convert(
int type,
void *ptr)
const
2267 return QMetaType::convert(d.type(), constData(), QMetaType(type), ptr);
2271
2272
2273bool QVariant::view(
int type,
void *ptr)
2275 return QMetaType::view(d.type(), data(), QMetaType(type), ptr);
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2329 static const qulonglong numericTypeBits =
2330 Q_UINT64_C(1) << QMetaType::QString |
2331 Q_UINT64_C(1) << QMetaType::Bool |
2332 Q_UINT64_C(1) << QMetaType::Double |
2334 Q_UINT64_C(1) << QMetaType::BFloat16 |
2336 Q_UINT64_C(1) << QMetaType::Float16 |
2337 Q_UINT64_C(1) << QMetaType::Float |
2338 Q_UINT64_C(1) << QMetaType::Char |
2339 Q_UINT64_C(1) << QMetaType::Char16 |
2340 Q_UINT64_C(1) << QMetaType::Char32 |
2341 Q_UINT64_C(1) << QMetaType::QChar |
2342 Q_UINT64_C(1) << QMetaType::SChar |
2343 Q_UINT64_C(1) << QMetaType::UChar |
2344 Q_UINT64_C(1) << QMetaType::Short |
2345 Q_UINT64_C(1) << QMetaType::UShort |
2346 Q_UINT64_C(1) << QMetaType::Int |
2347 Q_UINT64_C(1) << QMetaType::UInt |
2348 Q_UINT64_C(1) << QMetaType::Long |
2349 Q_UINT64_C(1) << QMetaType::ULong |
2350 Q_UINT64_C(1) << QMetaType::LongLong |
2351 Q_UINT64_C(1) << QMetaType::ULongLong;
2352 return tp < (CHAR_BIT *
sizeof numericTypeBits) ? numericTypeBits & (Q_UINT64_C(1) << tp) :
false;
2358 if (tp == QMetaType::BFloat16)
2361 return tp == QMetaType::Double || tp == QMetaType::Float || tp == QMetaType::Float16;
2367 if (!iface1 || !iface2)
2372 bool isNumeric1 = qIsNumericType(iface1->typeId.loadRelaxed());
2373 bool isNumeric2 = qIsNumericType(iface2->typeId.loadRelaxed());
2376 if (isNumeric1 && isNumeric2)
2379 bool isEnum1 = iface1->flags & QMetaType::IsEnumeration;
2380 bool isEnum2 = iface2->flags & QMetaType::IsEnumeration;
2385 if (isEnum1 && isEnum2)
2386 return QMetaType(iface1) == QMetaType(iface2);
2389 if (isEnum1 && isNumeric2)
2391 if (isNumeric1 && isEnum2)
2405 uint t1 = iface1->typeId.loadRelaxed();
2406 uint t2 = iface2->typeId.loadRelaxed();
2408 if ((t1 == QMetaType::Bool && t2 == QMetaType::QString) ||
2409 (t2 == QMetaType::Bool && t1 == QMetaType::QString))
2410 return QMetaType::Bool;
2427 if (qIsFloatingPoint(t1) || qIsFloatingPoint(t2))
2428 return QMetaType::QReal;
2432 return tp == QMetaType::ULongLong || tp == QMetaType::ULong ||
2433 tp == QMetaType::UInt || tp == QMetaType::Char32 ||
2434 (iface->flags & QMetaType::IsUnsignedEnumeration && iface->size >=
sizeof(
int));
2436 bool isUnsigned1 = isUnsigned(t1, iface1);
2437 bool isUnsigned2 = isUnsigned(t2, iface2);
2441 if (isUnsigned1 && iface1->size >
sizeof(
int))
2442 return QMetaType::ULongLong;
2443 if (isUnsigned2 && iface2->size >
sizeof(
int))
2444 return QMetaType::ULongLong;
2447 if (iface1->size >
sizeof(
int) || iface2->size >
sizeof(
int))
2448 return QMetaType::LongLong;
2451 if (isUnsigned1 || isUnsigned2)
2452 return QMetaType::UInt;
2455 return QMetaType::Int;
2461 std::optional<qlonglong> l1 = qConvertToNumber(d1, promotedType == QMetaType::Bool);
2462 std::optional<qlonglong> l2 = qConvertToNumber(d2, promotedType == QMetaType::Bool);
2464 return QPartialOrdering::Unordered;
2465 if (promotedType == QMetaType::UInt)
2466 return Qt::compareThreeWay(uint(*l1), uint(*l2));
2467 if (promotedType == QMetaType::LongLong)
2468 return Qt::compareThreeWay(qlonglong(*l1), qlonglong(*l2));
2469 if (promotedType == QMetaType::ULongLong)
2470 return Qt::compareThreeWay(qulonglong(*l1), qulonglong(*l2));
2472 return Qt::compareThreeWay(
int(*l1),
int(*l2));
2477 uint promotedType = numericTypePromotion(d1->typeInterface(), d2->typeInterface());
2478 if (promotedType != QMetaType::QReal)
2479 return integralCompare(promotedType, d1, d2);
2482 const auto r1 = qConvertToRealNumber(d1);
2483 const auto r2 = qConvertToRealNumber(d2);
2485 return QPartialOrdering::Unordered;
2487 return Qt::compareThreeWay(*r1, *r2);
2492 if ((fromType.flags() & QMetaType::PointerToQObject)
2493 && (toType.flags() & QMetaType::PointerToQObject)) {
2494 const QMetaObject *f = fromType.metaObject();
2495 const QMetaObject *t = toType.metaObject();
2496 return f && t && (f->inherits(t) || t->inherits(f));
2503 return Qt::compareThreeWay(Qt::totally_ordered_wrapper(d1->get<QObject *>()),
2504 Qt::totally_ordered_wrapper(d2->get<QObject *>()));
2508
2509
2510bool QVariant::equals(
const QVariant &v)
const
2512 auto metatype = d.type();
2514 if (metatype != v.metaType()) {
2516 if (canBeNumericallyCompared(metatype.iface(), v.d.type().iface()))
2517 return numericCompare(&d, &v.d) == QPartialOrdering::Equivalent;
2519 if (qvCanConvertMetaObject(metatype, v.metaType()))
2520 return pointerCompare(&d, &v.d) == QPartialOrdering::Equivalent;
2525 if (!metatype.isValid())
2528 return metatype.equals(d.storage(), v.d.storage());
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553QPartialOrdering QVariant::compare(
const QVariant &lhs,
const QVariant &rhs)
2555 QMetaType t = lhs.d.type();
2556 if (t != rhs.d.type()) {
2558 if (canBeNumericallyCompared(lhs.d.type().iface(), rhs.d.type().iface()))
2559 return numericCompare(&lhs.d, &rhs.d);
2560 if (qvCanConvertMetaObject(lhs.metaType(), rhs.metaType()))
2561 return pointerCompare(&lhs.d, &rhs.d);
2562 return QPartialOrdering::Unordered;
2564 return t.compare(lhs.constData(), rhs.constData());
2568
2569
2570
2571
2572
2573
2574
2575
2578
2579
2580
2581
2582
2583
2584
2585
2586void *QVariant::data()
2591 return const_cast<
void *>(constData());
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641bool QVariant::isNull()
const
2643 if (d.is_null || !metaType().isValid())
2645 if (metaType().flags() & QMetaType::IsPointer)
2646 return d.get<
void *>() ==
nullptr;
2650#ifndef QT_NO_DEBUG_STREAM
2651QDebug QVariant::qdebugHelper(QDebug dbg)
const
2653 QDebugStateSaver saver(dbg);
2654 const uint typeId = d.type().rawId();
2655 dbg.nospace() <<
"QVariant(";
2656 if (typeId != QMetaType::UnknownType) {
2657 dbg << d.type().name() <<
", ";
2658 bool streamed = d.type().debugStream(dbg, d.storage());
2659 if (!streamed && canConvert<QString>())
2668QVariant QVariant::moveConstruct(QMetaType type,
void *data)
2671 var.d = QVariant::Private(type.d_ptr);
2672 customConstruct<ForceMove, NonNull>(type.d_ptr, &var.d, data);
2676QVariant QVariant::copyConstruct(QMetaType type,
const void *data)
2679 var.d = QVariant::Private(type.d_ptr);
2680 customConstruct<UseCopy, NonNull>(type.d_ptr, &var.d, data);
2684#if QT_DEPRECATED_SINCE(6
, 0
)
2686QT_WARNING_DISABLE_DEPRECATED
2688QDebug operator<<(QDebug dbg,
const QVariant::Type p)
2690 QDebugStateSaver saver(dbg);
2691 dbg.nospace() <<
"QVariant::"
2692 << (
int(p) !=
int(QMetaType::UnknownType)
2693 ? QMetaType(p).name()
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2717
2718
2719
2720
2723
2724
2725
2726
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2785
2786
2787
2788
2789
2790
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2805
2806
2807
2808
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2824
2825
2826
2827
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852QVariant QVariant::fromMetaType(QMetaType type,
const void *copy)
2855 type.registerType();
2856 const auto iface = type.iface();
2857 if (isValidMetaTypeForVariant(iface, copy)) {
2858 result.d = Private(iface);
2859 customConstruct(iface, &result.d, copy);
2865
2866
2867
2868
2869
2870
2871
2872
2873
2876
2877
2878
2879
2880
2881
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2916
2917
2918
2919
2920
2923
2924
2925
2926
2927
2930
2931
2932
2933
2934
2935
2938
2939
2940
2942
2943
2946
2947
2950
2951
2954
2955
2958
2959
2962
2963
2966
2967
2970
2971
2974
2975
2978
2979
2980
2983
2984
2985
2988
2989
2990const void *QtPrivate::QVariantTypeCoercer::convert(
const QVariant &value,
const QMetaType &type)
2992 if (type == QMetaType::fromType<QVariant>())
2995 if (type == value.metaType())
2996 return value.constData();
2998 if (value.canConvert(type)) {
3000 if (converted.convert(type))
3001 return converted.constData();
3008
3009
3010const void *QtPrivate::QVariantTypeCoercer::coerce(
const QVariant &value,
const QMetaType &type)
3012 if (
const void *result = convert(value, type))
3015 converted = QVariant(type);
3016 return converted.constData();
3019#if QT_DEPRECATED_SINCE(6
, 15
)
3021QT_WARNING_DISABLE_DEPRECATED
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3038
3039
3040
3041
3044
3045
3046
3047
3048
3051
3052
3053
3054
3055
3058
3059
3060
3061
3062
3065
3066
3067
3068
3071
3072
3073
3074
3075
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3091
3092
3093QVariantConstPointer::QVariantConstPointer(QVariant variant)
3094 : m_variant(std::move(variant))
3099
3100
3101QVariant QVariantConstPointer::operator*()
const
3107
3108
3109
3110const QVariant *QVariantConstPointer::operator->()
const
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3129
3130
3131
3132
3135
3136
3137
3138
3141
3142
3143
3144
3145
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3166
3167
3168
3169
3172
3173
3174
3175
3178
3179
3180
3181
3184
3185
3186
3187
3191
3192
3193
3194
3195
3196
3197
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3215
3216
3217
3218
3221
3222
3223
3224
3227
3228
3229
3230
3233
3234
3235
3236
3239
3240
3241
3242
3245
3246
3247
3248
3251
3252
3253
3254
3255
3256
3259
3260
3261
3262
3263
3264
3265
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3281
3282
3283
3284
3287
3288
3289
3290
3293
3294
3295
3296
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3312
3313
3314
3315
3318
3319
3320
3321
3324
3325
3326
3327
3330
3331
3332
3333
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)