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));
160 if (d->typeInterface()->flags & QMetaType::IsEnumeration
161 || d->typeInterface()->typeId == QMetaType::QCborSimpleType)
162 return qMetaTypeNumberBySize(d);
167static std::optional<
double> qConvertToRealNumber(
const QVariant::Private *d)
170 switch (d->typeInterface()->typeId) {
171 case QMetaType::QString:
172 if (
double r = d->get<QString>().toDouble(&ok); ok)
175 case QMetaType::Double:
176 return d->get<
double>();
177 case QMetaType::Float:
178 return double(d->get<
float>());
179 case QMetaType::Float16:
181 case QMetaType::ULongLong:
182 case QMetaType::UInt:
183 case QMetaType::UChar:
184 case QMetaType::Char16:
185 case QMetaType::Char32:
186 case QMetaType::UShort:
187 case QMetaType::ULong:
188 return double(qMetaTypeUNumber(d));
189 case QMetaType::QCborValue:
190 return d->get<QCborValue>().toDouble();
191 case QMetaType::QJsonValue:
192 return d->get<QJsonValue>().toDouble();
195 if (std::optional<qlonglong> l = qConvertToNumber(d))
204 if (!iface || iface->size == 0)
207 Q_ASSERT(!isInterfaceFor<
void>(iface));
211 qWarning(
"QVariant: Provided metatype for '%s' does not support destruction and "
212 "copy construction", iface
->name);
218 qWarning(
"QVariant: Cannot create type '%s' without a default constructor", iface
->name);
225enum CustomConstructMoveOptions {
231enum CustomConstructNullabilityOption {
238template <CustomConstructMoveOptions moveOption = UseCopy, CustomConstructNullabilityOption nullability = MaybeNull>
240 std::conditional_t<moveOption == ForceMove,
void *,
const void *> copy)
244 Q_ASSERT(iface->size);
245 Q_ASSERT(!isInterfaceFor<
void>(iface));
249 if constexpr (moveOption == ForceMove)
251 if constexpr (nullability == NonNull)
252 Q_ASSERT(copy !=
nullptr);
257 d->is_null = !copy QT6_ONLY(|| isInterfaceFor<std::nullptr_t>(iface));
259 if (QVariant::Private::canUseInternalSpace(iface)) {
260 d->is_shared =
false;
263 if constexpr (moveOption == ForceMove && nullability == NonNull)
264 moveConstruct(iface, d->data.data, copy);
266 construct(iface, d->data.data, copy);
268 d->data.shared = customConstructShared(iface->size, iface->alignment, [=](
void *where) {
269 if constexpr (moveOption == ForceMove && nullability == NonNull)
270 moveConstruct(iface, where, copy);
272 construct(iface, where, copy);
278static void customClear(QVariant::Private *d)
287 QVariant::PrivateShared::free(d->data.shared);
291static QVariant::Private clonePrivate(
const QVariant::Private &other)
293 QVariant::Private d = other;
295 d.data.shared->ref.ref();
297 if (Q_LIKELY(d.canUseInternalSpace(iface))) {
305 d.data.shared = QVariant::PrivateShared::create(iface->size, iface->alignment);
316
317
318
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
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
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
466
467
468
469
470
471
472
475
476
477
478
479
480
483
484
485
486
489
490
491
492
493
494
495
496
498void QVariant::create(
int type,
const void *copy)
500 create(QMetaType(type), copy);
504
505
506
507
508
509void QVariant::create(QMetaType type,
const void *copy)
511 *
this = QVariant::fromMetaType(type, copy);
515
516
517
518
522 if (!d.is_shared || !d.data.shared->ref.deref())
527
528
529
530
531
533QVariant::QVariant(
const QVariant &p)
534 : d(clonePrivate(p.d))
539
540
541
542
543
544
545
546
547
548
549
550
551
554
555
556
557
558
559
560
561
562
566
567
568
569
570
571
572
573
574
577
578
579
580
581
582
583
584
586QVariant::QVariant(std::in_place_t, QMetaType type) : d(type.iface())
590 if (!Private::canUseInternalSpace(type.iface())) {
591 d.data.shared = PrivateShared::create(type.sizeOf(), type.alignOf());
597
598
599
600
601
602void *QVariant::prepareForEmplace(QMetaType type)
605
606
607
608
609
610 auto typeFits = [&] {
611 auto newIface = type.iface();
612 auto oldIface = d.typeInterface();
613 auto newSize = PrivateShared::computeAllocationSize(newIface->size, newIface->alignment);
614 auto oldSize = PrivateShared::computeAllocationSize(oldIface->size, oldIface->alignment);
615 return newSize <= oldSize;
617 if (Private::canUseInternalSpace(type.iface())) {
619 d.packedType = quintptr(type.iface()) >> 2;
621 }
else if (d.is_shared && isDetached() && typeFits()) {
622 QtMetaTypePrivate::destruct(d.typeInterface(), d.data.shared->data());
624 const auto ps = d.data.shared;
625 const auto align = type.alignOf();
626 ps->offset = PrivateShared::computeOffset(ps, align);
627 d.packedType = quintptr(type.iface()) >> 2;
631 QVariant newVariant(std::in_place, type);
634 return const_cast<
void *>(d.storage());
638
639
640
641
644
645
646
647
648
651
652
653
654
655
656
657
658
659
660
661
662
663
666
667
668
669
672
673
674
675
678
679
680
681
684
685
686
687
690
691
692
693
696
697
698
699
702
703
704
705
706
709
710
711
712
713
716
717
718
719
720
723
724
725
726
727
730
731
732
733
734
737
738
739
740
741
744
745
746
747
748
751
752
753
754
755
758
759
760
761
764
765
766
767
770
771
772
773
776
777
778
779
782
783
784
785
788
789
790
791
794
795
796
797
800
801
802
803
806
807
808
809
812
813
814
815
818
819
820
821
824
825
826
827
830
831
832
833
836
837
838
839
842
843
844
845
849
850
851
852
855
856
857
858
861
862
863
864
865
868
869
870
871
874
875
876
877
880
881
882
883
886
887
888
889
890
891
894
895
896
897
898
899
900
901
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921QVariant::QVariant(QMetaType type,
const void *copy)
922 : QVariant(fromMetaType(type, copy))
926QVariant::QVariant(
int val)
noexcept : d(std::piecewise_construct_t{}, val) {}
927QVariant::QVariant(uint val)
noexcept : d(std::piecewise_construct_t{}, val) {}
928QVariant::QVariant(qlonglong val)
noexcept : d(std::piecewise_construct_t{}, val) {}
929QVariant::QVariant(qulonglong val)
noexcept : d(std::piecewise_construct_t{}, val) {}
930QVariant::QVariant(
bool val)
noexcept : d(std::piecewise_construct_t{}, val) {}
931QVariant::QVariant(
double val)
noexcept : d(std::piecewise_construct_t{}, val) {}
932QVariant::QVariant(
float val)
noexcept : d(std::piecewise_construct_t{}, val) {}
934QVariant::QVariant(
const QByteArray &val)
noexcept : d(std::piecewise_construct_t{}, val) {}
935QVariant::QVariant(
const QBitArray &val)
noexcept : d(std::piecewise_construct_t{}, val) {}
936QVariant::QVariant(
const QString &val)
noexcept : d(std::piecewise_construct_t{}, val) {}
937QVariant::QVariant(QChar val)
noexcept : d(std::piecewise_construct_t{}, val) {}
938QVariant::QVariant(
const QStringList &val)
noexcept : d(std::piecewise_construct_t{}, val) {}
940QVariant::QVariant(QDate val)
noexcept : d(std::piecewise_construct_t{}, val) {}
941QVariant::QVariant(QTime val)
noexcept : d(std::piecewise_construct_t{}, val) {}
942QVariant::QVariant(
const QDateTime &val)
noexcept : d(std::piecewise_construct_t{}, val) {}
944QVariant::QVariant(
const QList<QVariant> &list)
noexcept : d(std::piecewise_construct_t{}, list) {}
945QVariant::QVariant(
const QMap<QString, QVariant> &map)
noexcept : d(std::piecewise_construct_t{}, map) {}
946QVariant::QVariant(
const QHash<QString, QVariant> &hash)
noexcept : d(std::piecewise_construct_t{}, hash) {}
948QVariant::QVariant(QLatin1StringView val) : QVariant(QString(val)) {}
950#if QT_CONFIG(easingcurve)
951QVariant::QVariant(
const QEasingCurve &val) : d(std::piecewise_construct_t{}, val) {}
953QVariant::QVariant(QPoint pt)
noexcept
954 : d(std::piecewise_construct_t{}, pt) {}
955QVariant::QVariant(QPointF pt)
noexcept(Private::FitsInInternalSize<
sizeof(qreal) * 2>)
956 : d(std::piecewise_construct_t{}, pt) {}
957QVariant::QVariant(QRect r)
noexcept(Private::FitsInInternalSize<
sizeof(
int) * 4>)
958 : d(std::piecewise_construct_t{}, r) {}
959QVariant::QVariant(QRectF r)
noexcept(Private::FitsInInternalSize<
sizeof(qreal) * 4>)
960 : d(std::piecewise_construct_t{}, r) {}
961QVariant::QVariant(QLine l)
noexcept(Private::FitsInInternalSize<
sizeof(
int) * 4>)
962 : d(std::piecewise_construct_t{}, l) {}
963QVariant::QVariant(QLineF l)
noexcept(Private::FitsInInternalSize<
sizeof(qreal) * 4>)
964 : d(std::piecewise_construct_t{}, l) {}
965QVariant::QVariant(QSize s)
noexcept
966 : d(std::piecewise_construct_t{}, s) {}
967QVariant::QVariant(QSizeF s)
noexcept(Private::FitsInInternalSize<
sizeof(qreal) * 2>)
968 : d(std::piecewise_construct_t{}, s) {}
969QVariant::QVariant(
const QUrl &u)
noexcept : d(std::piecewise_construct_t{}, u) {}
970QVariant::QVariant(
const QLocale &l)
noexcept : d(std::piecewise_construct_t{}, l) {}
971#if QT_CONFIG(regularexpression)
972QVariant::QVariant(
const QRegularExpression &re)
noexcept : d(std::piecewise_construct_t{}, re) {}
974QVariant::QVariant(QUuid uuid)
noexcept(Private::FitsInInternalSize<16>) : d(std::piecewise_construct_t{}, uuid) {}
975QVariant::QVariant(
const QJsonValue &jsonValue)
noexcept(Private::FitsInInternalSize<
sizeof(CborValueStandIn)>)
976 : d(std::piecewise_construct_t{}, jsonValue)
977{
static_assert(
sizeof(CborValueStandIn) ==
sizeof(QJsonValue)); }
978QVariant::QVariant(
const QJsonObject &jsonObject)
noexcept : d(std::piecewise_construct_t{}, jsonObject) {}
979QVariant::QVariant(
const QJsonArray &jsonArray)
noexcept : d(std::piecewise_construct_t{}, jsonArray) {}
980QVariant::QVariant(
const QJsonDocument &jsonDocument) : d(std::piecewise_construct_t{}, jsonDocument) {}
981#if QT_CONFIG(itemmodel)
982QVariant::QVariant(
const QModelIndex &modelIndex)
noexcept(Private::FitsInInternalSize<8 + 2 *
sizeof(quintptr)>)
983 : d(std::piecewise_construct_t{}, modelIndex) {}
984QVariant::QVariant(
const QPersistentModelIndex &modelIndex) : d(std::piecewise_construct_t{}, modelIndex) {}
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1024
1025
1026
1027
1028
1029
1030
1033
1034
1035
1036
1037
1040
1041
1042QVariant &QVariant::operator=(
const QVariant &variant)
1044 if (
this == &variant)
1048 d = clonePrivate(variant.d);
1053
1054
1055
1056
1059
1060
1061
1062
1064void QVariant::detach()
1066 if (!d.is_shared || d.data.shared->ref.loadRelaxed() == 1)
1069 Q_ASSERT(isValidMetaTypeForVariant(d.typeInterface(), constData()));
1070 Private dd(d.typeInterface());
1072 customConstruct<UseCopy, NonNull>(d.typeInterface(), &dd, constData());
1073 if (!d.data.shared->ref.deref())
1075 d.data.shared = dd.data.shared;
1079
1080
1081
1082
1085
1086
1087
1088
1089
1090
1091
1094
1095
1096
1097void QVariant::clear()
1099 if (!d.is_shared || !d.data.shared->ref.deref())
1105
1106
1107
1108
1109
1110
1111
1112
1115
1116
1117
1118
1119
1120
1121
1122
1123
1125#ifndef QT_NO_DATASTREAM
1129 QMetaType::UnknownType,
1130 QMetaType::QVariantMap,
1131 QMetaType::QVariantList,
1133 QMetaType::QStringList,
1140 QMetaType::QPalette,
1150 QMetaType::QPolygon,
1154 QMetaType::QSizePolicy,
1157 QMetaType::QDateTime,
1158 QMetaType::QByteArray,
1159 QMetaType::QBitArray,
1160#if QT_CONFIG(shortcut)
1161 QMetaType::QKeySequence,
1166 QMetaType::LongLong,
1167 QMetaType::ULongLong,
1168#if QT_CONFIG(easingcurve)
1169 QMetaType::QEasingCurve
1186
1187
1188
1189
1190
1191void QVariant::load(QDataStream &s)
1197 if (s.version() < QDataStream::Qt_4_0) {
1199 if (typeId >= MapFromThreeCount)
1201 typeId = mapIdFromQt3ToCurrent[typeId];
1202 }
else if (s.version() < QDataStream::Qt_5_0) {
1204 if (typeId == 127 ) {
1205 typeId = Qt5UserType;
1206 }
else if (typeId >= 128 && typeId != Qt5UserType) {
1210 }
else if (typeId == 75 ) {
1211 typeId = Qt5SizePolicy;
1212 }
else if (typeId > 75 && typeId <= 86) {
1218 if (s.version() < QDataStream::Qt_6_0) {
1220 if (typeId == Qt5UserType) {
1221 typeId = QMetaType::User;
1222 }
else if (typeId >= Qt5FirstGuiType && typeId <= Qt5LastGuiType) {
1223 typeId += Qt6ToQt5GuiTypeDelta;
1224 }
else if (typeId == Qt5SizePolicy) {
1225 typeId = QMetaType::QSizePolicy;
1226 }
else if (typeId == Qt5RegExp) {
1227 typeId = QMetaType::fromName(
"QRegExp").id();
1231 qint8 is_null =
false;
1232 if (s.version() >= QDataStream::Qt_4_2)
1234 if (typeId == QMetaType::User) {
1237 typeId = QMetaType::fromName(name).id();
1238 if (typeId == QMetaType::UnknownType) {
1239 s.setStatus(QDataStream::ReadCorruptData);
1240 qWarning(
"QVariant::load: unknown user type with name %s.", name.constData());
1244 create(typeId,
nullptr);
1245 d.is_null = is_null;
1248 if (s.version() < QDataStream::Qt_5_0) {
1258 void *data =
const_cast<
void *>(constData());
1259 if (!d.type().load(s, data)) {
1260 s.setStatus(QDataStream::ReadCorruptData);
1261 qWarning(
"QVariant::load: unable to load type %d.", d.type().id());
1266
1267
1268
1269
1270
1271void QVariant::save(QDataStream &s)
const
1273 quint32 typeId = d.type().id();
1274 bool saveAsUserType =
false;
1275 if (typeId >= QMetaType::User) {
1276 typeId = QMetaType::User;
1277 saveAsUserType =
true;
1279 if (s.version() < QDataStream::Qt_6_0) {
1281 if (typeId == QMetaType::User) {
1282 typeId = Qt5UserType;
1283 if (!strcmp(d.type().name(),
"QRegExp")) {
1286 }
else if (typeId > Qt5LastCoreType && typeId <= QMetaType::LastCoreType) {
1288 typeId = Qt5UserType;
1289 saveAsUserType =
true;
1290 }
else if (typeId >= QMetaType::FirstGuiType && typeId <= QMetaType::LastGuiType) {
1291 typeId -= Qt6ToQt5GuiTypeDelta;
1292 if (typeId > Qt5LastGuiType) {
1293 typeId = Qt5UserType;
1294 saveAsUserType =
true;
1296 }
else if (typeId == QMetaType::QSizePolicy) {
1297 typeId = Qt5SizePolicy;
1300 if (s.version() < QDataStream::Qt_4_0) {
1302 for (i = 0; i <= MapFromThreeCount - 1; ++i) {
1303 if (mapIdFromQt3ToCurrent[i] == typeId) {
1308 if (i >= MapFromThreeCount) {
1312 }
else if (s.version() < QDataStream::Qt_5_0) {
1313 if (typeId == Qt5UserType) {
1315 saveAsUserType =
true;
1316 }
else if (typeId >= 128 - 97 && typeId <= Qt5LastCoreType) {
1320 }
else if (typeId == Qt5SizePolicy) {
1322 }
else if (typeId >= Qt5KeySequence && typeId <= Qt5QQuaternion) {
1325 }
else if (typeId > Qt5QQuaternion || typeId == QMetaType::QUuid) {
1328 saveAsUserType =
true;
1331 const char *typeName =
nullptr;
1332 if (saveAsUserType) {
1333 if (s.version() < QDataStream::Qt_6_0)
1334 typeName = QtMetaTypePrivate::typedefNameForType(d.type().d_ptr);
1336 typeName = d.type().name();
1339 if (s.version() >= QDataStream::Qt_4_2)
1340 s << qint8(d.is_null);
1345 if (s.version() < QDataStream::Qt_5_0)
1350 if (!d.type().save(s, constData())) {
1351 qWarning(
"QVariant::save: unable to save type '%s' (type id: %d).\n",
1352 d.type().name(), d.type().id());
1353 Q_ASSERT_X(
false,
"QVariant::save",
"Invalid type to save");
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1383
1384
1385
1386
1387
1388QDataStream &operator<<(QDataStream &s,
const QVariant &p)
1395
1396
1397
1398
1399
1402
1403
1404
1405
1406
1410
1411
1412
1413
1414
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426QStringList QVariant::toStringList()
const
1428 return qvariant_cast<QStringList>(*
this);
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446QString QVariant::toString()
const
1448 return qvariant_cast<QString>(*
this);
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462QVariantMap QVariant::toMap()
const
1464 return qvariant_cast<QVariantMap>(*
this);
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478QVariantHash QVariant::toHash()
const
1480 return qvariant_cast<QVariantHash>(*
this);
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495QDate QVariant::toDate()
const
1497 return qvariant_cast<QDate>(*
this);
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512QTime QVariant::toTime()
const
1514 return qvariant_cast<QTime>(*
this);
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529QDateTime QVariant::toDateTime()
const
1531 return qvariant_cast<QDateTime>(*
this);
1535
1536
1537
1538
1539
1540
1541
1542
1543#if QT_CONFIG(easingcurve)
1544QEasingCurve QVariant::toEasingCurve()
const
1546 return qvariant_cast<QEasingCurve>(*
this);
1551
1552
1553
1554
1555
1556
1557
1558
1559QByteArray QVariant::toByteArray()
const
1561 return qvariant_cast<QByteArray>(*
this);
1565
1566
1567
1568
1569
1570
1571
1572
1573QPoint QVariant::toPoint()
const
1575 return qvariant_cast<QPoint>(*
this);
1579
1580
1581
1582
1583
1584
1585
1586QRect QVariant::toRect()
const
1588 return qvariant_cast<QRect>(*
this);
1592
1593
1594
1595
1596
1597
1598
1599QSize QVariant::toSize()
const
1601 return qvariant_cast<QSize>(*
this);
1605
1606
1607
1608
1609
1610
1611
1612QSizeF QVariant::toSizeF()
const
1614 return qvariant_cast<QSizeF>(*
this);
1618
1619
1620
1621
1622
1623
1624
1625
1626QRectF QVariant::toRectF()
const
1628 return qvariant_cast<QRectF>(*
this);
1632
1633
1634
1635
1636
1637
1638
1639QLineF QVariant::toLineF()
const
1641 return qvariant_cast<QLineF>(*
this);
1645
1646
1647
1648
1649
1650
1651
1652QLine QVariant::toLine()
const
1654 return qvariant_cast<QLine>(*
this);
1658
1659
1660
1661
1662
1663
1664
1665
1666QPointF QVariant::toPointF()
const
1668 return qvariant_cast<QPointF>(*
this);
1672
1673
1674
1675
1676
1677
1678
1679QUrl QVariant::toUrl()
const
1681 return qvariant_cast<QUrl>(*
this);
1685
1686
1687
1688
1689
1690
1691
1692QLocale QVariant::toLocale()
const
1694 return qvariant_cast<QLocale>(*
this);
1697#if QT_CONFIG(regularexpression)
1699
1700
1701
1702
1703
1704
1705
1706
1707QRegularExpression QVariant::toRegularExpression()
const
1709 return qvariant_cast<QRegularExpression>(*
this);
1713#if QT_CONFIG(itemmodel)
1715
1716
1717
1718
1719
1720
1721
1722QModelIndex QVariant::toModelIndex()
const
1724 return qvariant_cast<QModelIndex>(*
this);
1728
1729
1730
1731
1732
1733
1734
1735QPersistentModelIndex QVariant::toPersistentModelIndex()
const
1737 return qvariant_cast<QPersistentModelIndex>(*
this);
1742
1743
1744
1745
1746
1747
1748
1749
1750QUuid QVariant::toUuid()
const
1752 return qvariant_cast<QUuid>(*
this);
1756
1757
1758
1759
1760
1761
1762
1763QJsonValue QVariant::toJsonValue()
const
1765 return qvariant_cast<QJsonValue>(*
this);
1769
1770
1771
1772
1773
1774
1775
1776QJsonObject QVariant::toJsonObject()
const
1778 return qvariant_cast<QJsonObject>(*
this);
1782
1783
1784
1785
1786
1787
1788
1789QJsonArray QVariant::toJsonArray()
const
1791 return qvariant_cast<QJsonArray>(*
this);
1795
1796
1797
1798
1799
1800
1801
1802QJsonDocument QVariant::toJsonDocument()
const
1804 return qvariant_cast<QJsonDocument>(*
this);
1808
1809
1810
1811
1812
1813
1814
1815
1816QChar QVariant::toChar()
const
1818 return qvariant_cast<QChar>(*
this);
1822
1823
1824
1825
1826
1827QBitArray QVariant::toBitArray()
const
1829 return qvariant_cast<QBitArray>(*
this);
1832template <
typename T>
1843 bool success = QMetaType::convert(d.type(), d.storage(), t, &ret);
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866int QVariant::toInt(
bool *ok)
const
1868 return qNumVariantToHelper<
int>(d, ok);
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888uint QVariant::toUInt(
bool *ok)
const
1890 return qNumVariantToHelper<uint>(d, ok);
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905qlonglong QVariant::toLongLong(
bool *ok)
const
1907 return qNumVariantToHelper<qlonglong>(d, ok);
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922qulonglong QVariant::toULongLong(
bool *ok)
const
1924 return qNumVariantToHelper<qulonglong>(d, ok);
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939bool QVariant::toBool()
const
1941 auto boolType = QMetaType::fromType<
bool>();
1942 if (d.type() == boolType)
1943 return d.get<
bool>();
1946 QMetaType::convert(d.type(), constData(), boolType, &res);
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962double QVariant::toDouble(
bool *ok)
const
1964 return qNumVariantToHelper<
double>(d, ok);
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981float QVariant::toFloat(
bool *ok)
const
1983 return qNumVariantToHelper<
float>(d, ok);
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000qreal QVariant::toReal(
bool *ok)
const
2002 return qNumVariantToHelper<qreal>(d, ok);
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016QVariantList QVariant::toList()
const
2018 return qvariant_cast<QVariantList>(*
this);
2022
2023
2024
2025
2026
2027
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2090bool QVariant::convert(QMetaType targetType)
2092 if (d.type() == targetType)
2093 return targetType.isValid();
2095 QVariant oldValue = *
this;
2098 create(targetType,
nullptr);
2099 if (!oldValue.canConvert(targetType))
2103 if (oldValue.d.is_null && oldValue.d.type().id() != QMetaType::Nullptr)
2106 bool ok = QMetaType::convert(oldValue.d.type(), oldValue.constData(), targetType, data());
2112
2113
2114
2115
2116bool QVariant::convert(
int type,
void *ptr)
const
2118 return QMetaType::convert(d.type(), constData(), QMetaType(type), ptr);
2122
2123
2124bool QVariant::view(
int type,
void *ptr)
2126 return QMetaType::view(d.type(), data(), QMetaType(type), ptr);
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2179 static const qulonglong numericTypeBits =
2180 Q_UINT64_C(1) << QMetaType::QString |
2181 Q_UINT64_C(1) << QMetaType::Bool |
2182 Q_UINT64_C(1) << QMetaType::Double |
2183 Q_UINT64_C(1) << QMetaType::Float16 |
2184 Q_UINT64_C(1) << QMetaType::Float |
2185 Q_UINT64_C(1) << QMetaType::Char |
2186 Q_UINT64_C(1) << QMetaType::Char16 |
2187 Q_UINT64_C(1) << QMetaType::Char32 |
2188 Q_UINT64_C(1) << QMetaType::SChar |
2189 Q_UINT64_C(1) << QMetaType::UChar |
2190 Q_UINT64_C(1) << QMetaType::Short |
2191 Q_UINT64_C(1) << QMetaType::UShort |
2192 Q_UINT64_C(1) << QMetaType::Int |
2193 Q_UINT64_C(1) << QMetaType::UInt |
2194 Q_UINT64_C(1) << QMetaType::Long |
2195 Q_UINT64_C(1) << QMetaType::ULong |
2196 Q_UINT64_C(1) << QMetaType::LongLong |
2197 Q_UINT64_C(1) << QMetaType::ULongLong;
2198 return tp < (CHAR_BIT *
sizeof numericTypeBits) ? numericTypeBits & (Q_UINT64_C(1) << tp) :
false;
2203 return tp == QMetaType::Double || tp == QMetaType::Float || tp == QMetaType::Float16;
2209 if (!iface1 || !iface2)
2214 bool isNumeric1 = qIsNumericType(iface1->typeId);
2215 bool isNumeric2 = qIsNumericType(iface2->typeId);
2218 if (isNumeric1 && isNumeric2)
2221 bool isEnum1 = iface1->flags & QMetaType::IsEnumeration;
2222 bool isEnum2 = iface2->flags & QMetaType::IsEnumeration;
2227 if (isEnum1 && isEnum2)
2228 return QMetaType(iface1) == QMetaType(iface2);
2231 if (isEnum1 && isNumeric2)
2233 if (isNumeric1 && isEnum2)
2247 uint t1 = iface1->typeId;
2248 uint t2 = iface2->typeId;
2250 if ((t1 == QMetaType::Bool && t2 == QMetaType::QString) ||
2251 (t2 == QMetaType::Bool && t1 == QMetaType::QString))
2252 return QMetaType::Bool;
2269 if (qIsFloatingPoint(t1) || qIsFloatingPoint(t2))
2270 return QMetaType::QReal;
2272 auto isUnsigned = [](uint tp) {
2274 return tp == QMetaType::ULongLong || tp == QMetaType::ULong ||
2275 tp == QMetaType::UInt || tp == QMetaType::Char32;
2277 bool isUnsigned1 = isUnsigned(t1);
2278 bool isUnsigned2 = isUnsigned(t2);
2282 if (isUnsigned1 && iface1->size >
sizeof(
int))
2283 return QMetaType::ULongLong;
2284 if (isUnsigned2 && iface2->size >
sizeof(
int))
2285 return QMetaType::ULongLong;
2288 if (iface1->size >
sizeof(
int) || iface2->size >
sizeof(
int))
2289 return QMetaType::LongLong;
2292 if (isUnsigned1 || isUnsigned2)
2293 return QMetaType::UInt;
2296 return QMetaType::Int;
2302 std::optional<qlonglong> l1 = qConvertToNumber(d1, promotedType == QMetaType::Bool);
2303 std::optional<qlonglong> l2 = qConvertToNumber(d2, promotedType == QMetaType::Bool);
2305 return QPartialOrdering::Unordered;
2306 if (promotedType == QMetaType::UInt)
2307 return Qt::compareThreeWay(uint(*l1), uint(*l2));
2308 if (promotedType == QMetaType::LongLong)
2309 return Qt::compareThreeWay(qlonglong(*l1), qlonglong(*l2));
2310 if (promotedType == QMetaType::ULongLong)
2311 return Qt::compareThreeWay(qulonglong(*l1), qulonglong(*l2));
2313 return Qt::compareThreeWay(
int(*l1),
int(*l2));
2318 uint promotedType = numericTypePromotion(d1->typeInterface(), d2->typeInterface());
2319 if (promotedType != QMetaType::QReal)
2320 return integralCompare(promotedType, d1, d2);
2323 const auto r1 = qConvertToRealNumber(d1);
2324 const auto r2 = qConvertToRealNumber(d2);
2326 return QPartialOrdering::Unordered;
2328 return Qt::compareThreeWay(*r1, *r2);
2333 if ((fromType.flags() & QMetaType::PointerToQObject)
2334 && (toType.flags() & QMetaType::PointerToQObject)) {
2335 const QMetaObject *f = fromType.metaObject();
2336 const QMetaObject *t = toType.metaObject();
2337 return f && t && (f->inherits(t) || t->inherits(f));
2344 return Qt::compareThreeWay(Qt::totally_ordered_wrapper(d1->get<QObject *>()),
2345 Qt::totally_ordered_wrapper(d2->get<QObject *>()));
2349
2350
2351bool QVariant::equals(
const QVariant &v)
const
2353 auto metatype = d.type();
2355 if (metatype != v.metaType()) {
2357 if (canBeNumericallyCompared(metatype.iface(), v.d.type().iface()))
2358 return numericCompare(&d, &v.d) == QPartialOrdering::Equivalent;
2360 if (qvCanConvertMetaObject(metatype, v.metaType()))
2361 return pointerCompare(&d, &v.d) == QPartialOrdering::Equivalent;
2366 if (!metatype.isValid())
2369 return metatype.equals(d.storage(), v.d.storage());
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394QPartialOrdering QVariant::compare(
const QVariant &lhs,
const QVariant &rhs)
2396 QMetaType t = lhs.d.type();
2397 if (t != rhs.d.type()) {
2399 if (canBeNumericallyCompared(lhs.d.type().iface(), rhs.d.type().iface()))
2400 return numericCompare(&lhs.d, &rhs.d);
2401 if (qvCanConvertMetaObject(lhs.metaType(), rhs.metaType()))
2402 return pointerCompare(&lhs.d, &rhs.d);
2403 return QPartialOrdering::Unordered;
2405 return t.compare(lhs.constData(), rhs.constData());
2409
2410
2411
2412
2413
2414
2415
2416
2419
2420
2421
2422
2423
2424
2425
2426
2427void *QVariant::data()
2432 return const_cast<
void *>(constData());
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482bool QVariant::isNull()
const
2484 if (d.is_null || !metaType().isValid())
2486 if (metaType().flags() & QMetaType::IsPointer)
2487 return d.get<
void *>() ==
nullptr;
2491#ifndef QT_NO_DEBUG_STREAM
2492QDebug QVariant::qdebugHelper(QDebug dbg)
const
2494 QDebugStateSaver saver(dbg);
2495 const uint typeId = d.type().id();
2496 dbg.nospace() <<
"QVariant(";
2497 if (typeId != QMetaType::UnknownType) {
2498 dbg << d.type().name() <<
", ";
2499 bool streamed = d.type().debugStream(dbg, d.storage());
2500 if (!streamed && canConvert<QString>())
2509QVariant QVariant::moveConstruct(QMetaType type,
void *data)
2512 var.d = QVariant::Private(type.d_ptr);
2513 customConstruct<ForceMove, NonNull>(type.d_ptr, &var.d, data);
2517QVariant QVariant::copyConstruct(QMetaType type,
const void *data)
2520 var.d = QVariant::Private(type.d_ptr);
2521 customConstruct<UseCopy, NonNull>(type.d_ptr, &var.d, data);
2525#if QT_DEPRECATED_SINCE(6
, 0
)
2527QT_WARNING_DISABLE_DEPRECATED
2529QDebug operator<<(QDebug dbg,
const QVariant::Type p)
2531 QDebugStateSaver saver(dbg);
2532 dbg.nospace() <<
"QVariant::"
2533 << (
int(p) !=
int(QMetaType::UnknownType)
2534 ? QMetaType(p).name()
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2558
2559
2560
2561
2564
2565
2566
2567
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2626
2627
2628
2629
2630
2631
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2646
2647
2648
2649
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2665
2666
2667
2668
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693QVariant QVariant::fromMetaType(QMetaType type,
const void *copy)
2696 type.registerType();
2697 const auto iface = type.iface();
2698 if (isValidMetaTypeForVariant(iface, copy)) {
2699 result.d = Private(iface);
2700 customConstruct(iface, &result.d, copy);
2706
2707
2708
2709
2710
2711
2712
2713
2714
2717
2718
2719
2720
2721
2722
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2757
2758
2759
2760
2761
2764
2765
2766
2767
2768
2771
2772
2773
2774
2775
2776
2779
2780
2781
2783
2784
2787
2788
2791
2792
2795
2796
2799
2800
2803
2804
2807
2808
2811
2812
2815
2816
2819
2820
2821
2824
2825
2826
2829
2830
2831const void *QtPrivate::QVariantTypeCoercer::convert(
const QVariant &value,
const QMetaType &type)
2833 if (type == QMetaType::fromType<QVariant>())
2836 if (type == value.metaType())
2837 return value.constData();
2839 if (value.canConvert(type)) {
2841 if (converted.convert(type))
2842 return converted.constData();
2849
2850
2851const void *QtPrivate::QVariantTypeCoercer::coerce(
const QVariant &value,
const QMetaType &type)
2853 if (
const void *result = convert(value, type))
2856 converted = QVariant(type);
2857 return converted.constData();
2860#if QT_DEPRECATED_SINCE(6
, 15
)
2862QT_WARNING_DISABLE_DEPRECATED
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2879
2880
2881
2882
2885
2886
2887
2888
2889
2892
2893
2894
2895
2896
2899
2900
2901
2902
2903
2906
2907
2908
2909
2912
2913
2914
2915
2916
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2932
2933
2934QVariantConstPointer::QVariantConstPointer(QVariant variant)
2935 : m_variant(std::move(variant))
2940
2941
2942QVariant QVariantConstPointer::operator*()
const
2948
2949
2950
2951const QVariant *QVariantConstPointer::operator->()
const
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2970
2971
2972
2973
2976
2977
2978
2979
2982
2983
2984
2985
2986
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3007
3008
3009
3010
3013
3014
3015
3016
3019
3020
3021
3022
3025
3026
3027
3028
3032
3033
3034
3035
3036
3037
3038
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3056
3057
3058
3059
3062
3063
3064
3065
3068
3069
3070
3071
3074
3075
3076
3077
3080
3081
3082
3083
3086
3087
3088
3089
3092
3093
3094
3095
3096
3097
3100
3101
3102
3103
3104
3105
3106
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3122
3123
3124
3125
3128
3129
3130
3131
3134
3135
3136
3137
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3153
3154
3155
3156
3159
3160
3161
3162
3165
3166
3167
3168
3171
3172
3173
3174
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
constexpr int Qt5UserType
static QPartialOrdering pointerCompare(const QVariant::Private *d1, const QVariant::Private *d2)