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
1473QVariantHash QVariant::toHash()
const
1475 return qvariant_cast<QVariantHash>(*
this);
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490QDate QVariant::toDate()
const
1492 return qvariant_cast<QDate>(*
this);
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507QTime QVariant::toTime()
const
1509 return qvariant_cast<QTime>(*
this);
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524QDateTime QVariant::toDateTime()
const
1526 return qvariant_cast<QDateTime>(*
this);
1530
1531
1532
1533
1534
1535
1536
1537
1538#if QT_CONFIG(easingcurve)
1539QEasingCurve QVariant::toEasingCurve()
const
1541 return qvariant_cast<QEasingCurve>(*
this);
1546
1547
1548
1549
1550
1551
1552
1553
1554QByteArray QVariant::toByteArray()
const
1556 return qvariant_cast<QByteArray>(*
this);
1560
1561
1562
1563
1564
1565
1566
1567
1568QPoint QVariant::toPoint()
const
1570 return qvariant_cast<QPoint>(*
this);
1574
1575
1576
1577
1578
1579
1580
1581QRect QVariant::toRect()
const
1583 return qvariant_cast<QRect>(*
this);
1587
1588
1589
1590
1591
1592
1593
1594QSize QVariant::toSize()
const
1596 return qvariant_cast<QSize>(*
this);
1600
1601
1602
1603
1604
1605
1606
1607QSizeF QVariant::toSizeF()
const
1609 return qvariant_cast<QSizeF>(*
this);
1613
1614
1615
1616
1617
1618
1619
1620
1621QRectF QVariant::toRectF()
const
1623 return qvariant_cast<QRectF>(*
this);
1627
1628
1629
1630
1631
1632
1633
1634QLineF QVariant::toLineF()
const
1636 return qvariant_cast<QLineF>(*
this);
1640
1641
1642
1643
1644
1645
1646
1647QLine QVariant::toLine()
const
1649 return qvariant_cast<QLine>(*
this);
1653
1654
1655
1656
1657
1658
1659
1660
1661QPointF QVariant::toPointF()
const
1663 return qvariant_cast<QPointF>(*
this);
1667
1668
1669
1670
1671
1672
1673
1674QUrl QVariant::toUrl()
const
1676 return qvariant_cast<QUrl>(*
this);
1680
1681
1682
1683
1684
1685
1686
1687QLocale QVariant::toLocale()
const
1689 return qvariant_cast<QLocale>(*
this);
1692#if QT_CONFIG(regularexpression)
1694
1695
1696
1697
1698
1699
1700
1701
1702QRegularExpression QVariant::toRegularExpression()
const
1704 return qvariant_cast<QRegularExpression>(*
this);
1708#if QT_CONFIG(itemmodel)
1710
1711
1712
1713
1714
1715
1716
1717QModelIndex QVariant::toModelIndex()
const
1719 return qvariant_cast<QModelIndex>(*
this);
1723
1724
1725
1726
1727
1728
1729
1730QPersistentModelIndex QVariant::toPersistentModelIndex()
const
1732 return qvariant_cast<QPersistentModelIndex>(*
this);
1737
1738
1739
1740
1741
1742
1743
1744
1745QUuid QVariant::toUuid()
const
1747 return qvariant_cast<QUuid>(*
this);
1751
1752
1753
1754
1755
1756
1757
1758QJsonValue QVariant::toJsonValue()
const
1760 return qvariant_cast<QJsonValue>(*
this);
1764
1765
1766
1767
1768
1769
1770
1771QJsonObject QVariant::toJsonObject()
const
1773 return qvariant_cast<QJsonObject>(*
this);
1777
1778
1779
1780
1781
1782
1783
1784QJsonArray QVariant::toJsonArray()
const
1786 return qvariant_cast<QJsonArray>(*
this);
1790
1791
1792
1793
1794
1795
1796
1797QJsonDocument QVariant::toJsonDocument()
const
1799 return qvariant_cast<QJsonDocument>(*
this);
1803
1804
1805
1806
1807
1808
1809
1810
1811QChar QVariant::toChar()
const
1813 return qvariant_cast<QChar>(*
this);
1817
1818
1819
1820
1821
1822QBitArray QVariant::toBitArray()
const
1824 return qvariant_cast<QBitArray>(*
this);
1827template <
typename T>
1838 bool success = QMetaType::convert(d.type(), d.storage(), t, &ret);
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861int QVariant::toInt(
bool *ok)
const
1863 return qNumVariantToHelper<
int>(d, ok);
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883uint QVariant::toUInt(
bool *ok)
const
1885 return qNumVariantToHelper<uint>(d, ok);
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900qlonglong QVariant::toLongLong(
bool *ok)
const
1902 return qNumVariantToHelper<qlonglong>(d, ok);
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917qulonglong QVariant::toULongLong(
bool *ok)
const
1919 return qNumVariantToHelper<qulonglong>(d, ok);
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934bool QVariant::toBool()
const
1936 auto boolType = QMetaType::fromType<
bool>();
1937 if (d.type() == boolType)
1938 return d.get<
bool>();
1941 QMetaType::convert(d.type(), constData(), boolType, &res);
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957double QVariant::toDouble(
bool *ok)
const
1959 return qNumVariantToHelper<
double>(d, ok);
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976float QVariant::toFloat(
bool *ok)
const
1978 return qNumVariantToHelper<
float>(d, ok);
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995qreal QVariant::toReal(
bool *ok)
const
1997 return qNumVariantToHelper<qreal>(d, ok);
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011QVariantList QVariant::toList()
const
2013 return qvariant_cast<QVariantList>(*
this);
2017
2018
2019
2020
2021
2022
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2085bool QVariant::convert(QMetaType targetType)
2087 if (d.type() == targetType)
2088 return targetType.isValid();
2090 QVariant oldValue = *
this;
2093 create(targetType,
nullptr);
2094 if (!oldValue.canConvert(targetType))
2098 if (oldValue.d.is_null && oldValue.d.type().id() != QMetaType::Nullptr)
2101 bool ok = QMetaType::convert(oldValue.d.type(), oldValue.constData(), targetType, data());
2107
2108
2109
2110
2111bool QVariant::convert(
int type,
void *ptr)
const
2113 return QMetaType::convert(d.type(), constData(), QMetaType(type), ptr);
2117
2118
2119bool QVariant::view(
int type,
void *ptr)
2121 return QMetaType::view(d.type(), data(), QMetaType(type), ptr);
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2174 static const qulonglong numericTypeBits =
2175 Q_UINT64_C(1) << QMetaType::QString |
2176 Q_UINT64_C(1) << QMetaType::Bool |
2177 Q_UINT64_C(1) << QMetaType::Double |
2178 Q_UINT64_C(1) << QMetaType::Float16 |
2179 Q_UINT64_C(1) << QMetaType::Float |
2180 Q_UINT64_C(1) << QMetaType::Char |
2181 Q_UINT64_C(1) << QMetaType::Char16 |
2182 Q_UINT64_C(1) << QMetaType::Char32 |
2183 Q_UINT64_C(1) << QMetaType::SChar |
2184 Q_UINT64_C(1) << QMetaType::UChar |
2185 Q_UINT64_C(1) << QMetaType::Short |
2186 Q_UINT64_C(1) << QMetaType::UShort |
2187 Q_UINT64_C(1) << QMetaType::Int |
2188 Q_UINT64_C(1) << QMetaType::UInt |
2189 Q_UINT64_C(1) << QMetaType::Long |
2190 Q_UINT64_C(1) << QMetaType::ULong |
2191 Q_UINT64_C(1) << QMetaType::LongLong |
2192 Q_UINT64_C(1) << QMetaType::ULongLong;
2193 return tp < (CHAR_BIT *
sizeof numericTypeBits) ? numericTypeBits & (Q_UINT64_C(1) << tp) :
false;
2198 return tp == QMetaType::Double || tp == QMetaType::Float || tp == QMetaType::Float16;
2204 if (!iface1 || !iface2)
2209 bool isNumeric1 = qIsNumericType(iface1->typeId);
2210 bool isNumeric2 = qIsNumericType(iface2->typeId);
2213 if (isNumeric1 && isNumeric2)
2216 bool isEnum1 = iface1->flags & QMetaType::IsEnumeration;
2217 bool isEnum2 = iface2->flags & QMetaType::IsEnumeration;
2222 if (isEnum1 && isEnum2)
2223 return QMetaType(iface1) == QMetaType(iface2);
2226 if (isEnum1 && isNumeric2)
2228 if (isNumeric1 && isEnum2)
2242 uint t1 = iface1->typeId;
2243 uint t2 = iface2->typeId;
2245 if ((t1 == QMetaType::Bool && t2 == QMetaType::QString) ||
2246 (t2 == QMetaType::Bool && t1 == QMetaType::QString))
2247 return QMetaType::Bool;
2264 if (qIsFloatingPoint(t1) || qIsFloatingPoint(t2))
2265 return QMetaType::QReal;
2267 auto isUnsigned = [](uint tp) {
2269 return tp == QMetaType::ULongLong || tp == QMetaType::ULong ||
2270 tp == QMetaType::UInt || tp == QMetaType::Char32;
2272 bool isUnsigned1 = isUnsigned(t1);
2273 bool isUnsigned2 = isUnsigned(t2);
2277 if (isUnsigned1 && iface1->size >
sizeof(
int))
2278 return QMetaType::ULongLong;
2279 if (isUnsigned2 && iface2->size >
sizeof(
int))
2280 return QMetaType::ULongLong;
2283 if (iface1->size >
sizeof(
int) || iface2->size >
sizeof(
int))
2284 return QMetaType::LongLong;
2287 if (isUnsigned1 || isUnsigned2)
2288 return QMetaType::UInt;
2291 return QMetaType::Int;
2297 std::optional<qlonglong> l1 = qConvertToNumber(d1, promotedType == QMetaType::Bool);
2298 std::optional<qlonglong> l2 = qConvertToNumber(d2, promotedType == QMetaType::Bool);
2300 return QPartialOrdering::Unordered;
2301 if (promotedType == QMetaType::UInt)
2302 return Qt::compareThreeWay(uint(*l1), uint(*l2));
2303 if (promotedType == QMetaType::LongLong)
2304 return Qt::compareThreeWay(qlonglong(*l1), qlonglong(*l2));
2305 if (promotedType == QMetaType::ULongLong)
2306 return Qt::compareThreeWay(qulonglong(*l1), qulonglong(*l2));
2308 return Qt::compareThreeWay(
int(*l1),
int(*l2));
2313 uint promotedType = numericTypePromotion(d1->typeInterface(), d2->typeInterface());
2314 if (promotedType != QMetaType::QReal)
2315 return integralCompare(promotedType, d1, d2);
2318 const auto r1 = qConvertToRealNumber(d1);
2319 const auto r2 = qConvertToRealNumber(d2);
2321 return QPartialOrdering::Unordered;
2323 return Qt::compareThreeWay(*r1, *r2);
2328 if ((fromType.flags() & QMetaType::PointerToQObject)
2329 && (toType.flags() & QMetaType::PointerToQObject)) {
2330 const QMetaObject *f = fromType.metaObject();
2331 const QMetaObject *t = toType.metaObject();
2332 return f && t && (f->inherits(t) || t->inherits(f));
2339 return Qt::compareThreeWay(Qt::totally_ordered_wrapper(d1->get<QObject *>()),
2340 Qt::totally_ordered_wrapper(d2->get<QObject *>()));
2344
2345
2346bool QVariant::equals(
const QVariant &v)
const
2348 auto metatype = d.type();
2350 if (metatype != v.metaType()) {
2352 if (canBeNumericallyCompared(metatype.iface(), v.d.type().iface()))
2353 return numericCompare(&d, &v.d) == QPartialOrdering::Equivalent;
2355 if (qvCanConvertMetaObject(metatype, v.metaType()))
2356 return pointerCompare(&d, &v.d) == QPartialOrdering::Equivalent;
2361 if (!metatype.isValid())
2364 return metatype.equals(d.storage(), v.d.storage());
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389QPartialOrdering QVariant::compare(
const QVariant &lhs,
const QVariant &rhs)
2391 QMetaType t = lhs.d.type();
2392 if (t != rhs.d.type()) {
2394 if (canBeNumericallyCompared(lhs.d.type().iface(), rhs.d.type().iface()))
2395 return numericCompare(&lhs.d, &rhs.d);
2396 if (qvCanConvertMetaObject(lhs.metaType(), rhs.metaType()))
2397 return pointerCompare(&lhs.d, &rhs.d);
2398 return QPartialOrdering::Unordered;
2400 return t.compare(lhs.constData(), rhs.constData());
2404
2405
2406
2407
2408
2409
2410
2411
2414
2415
2416
2417
2418
2419
2420
2421
2422void *QVariant::data()
2427 return const_cast<
void *>(constData());
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477bool QVariant::isNull()
const
2479 if (d.is_null || !metaType().isValid())
2481 if (metaType().flags() & QMetaType::IsPointer)
2482 return d.get<
void *>() ==
nullptr;
2486#ifndef QT_NO_DEBUG_STREAM
2487QDebug QVariant::qdebugHelper(QDebug dbg)
const
2489 QDebugStateSaver saver(dbg);
2490 const uint typeId = d.type().id();
2491 dbg.nospace() <<
"QVariant(";
2492 if (typeId != QMetaType::UnknownType) {
2493 dbg << d.type().name() <<
", ";
2494 bool streamed = d.type().debugStream(dbg, d.storage());
2495 if (!streamed && canConvert<QString>())
2504QVariant QVariant::moveConstruct(QMetaType type,
void *data)
2507 var.d = QVariant::Private(type.d_ptr);
2508 customConstruct<ForceMove, NonNull>(type.d_ptr, &var.d, data);
2512QVariant QVariant::copyConstruct(QMetaType type,
const void *data)
2515 var.d = QVariant::Private(type.d_ptr);
2516 customConstruct<UseCopy, NonNull>(type.d_ptr, &var.d, data);
2520#if QT_DEPRECATED_SINCE(6
, 0
)
2522QT_WARNING_DISABLE_DEPRECATED
2524QDebug operator<<(QDebug dbg,
const QVariant::Type p)
2526 QDebugStateSaver saver(dbg);
2527 dbg.nospace() <<
"QVariant::"
2528 << (
int(p) !=
int(QMetaType::UnknownType)
2529 ? QMetaType(p).name()
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2553
2554
2555
2556
2559
2560
2561
2562
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2621
2622
2623
2624
2625
2626
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2641
2642
2643
2644
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2660
2661
2662
2663
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688QVariant QVariant::fromMetaType(QMetaType type,
const void *copy)
2691 type.registerType();
2692 const auto iface = type.iface();
2693 if (isValidMetaTypeForVariant(iface, copy)) {
2694 result.d = Private(iface);
2695 customConstruct(iface, &result.d, copy);
2701
2702
2703
2704
2705
2706
2707
2708
2709
2712
2713
2714
2715
2716
2717
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2752
2753
2754
2755
2756
2759
2760
2761
2762
2763
2766
2767
2768
2769
2770
2771
2774
2775
2776
2778
2779
2782
2783
2786
2787
2790
2791
2794
2795
2798
2799
2802
2803
2806
2807
2810
2811
2814
2815
2816
2819
2820
2821
2824
2825
2826const void *QtPrivate::QVariantTypeCoercer::convert(
const QVariant &value,
const QMetaType &type)
2828 if (type == QMetaType::fromType<QVariant>())
2831 if (type == value.metaType())
2832 return value.constData();
2834 if (value.canConvert(type)) {
2836 if (converted.convert(type))
2837 return converted.constData();
2844
2845
2846const void *QtPrivate::QVariantTypeCoercer::coerce(
const QVariant &value,
const QMetaType &type)
2848 if (
const void *result = convert(value, type))
2851 converted = QVariant(type);
2852 return converted.constData();
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2870
2871
2872
2873
2876
2877
2878
2879
2880
2883
2884
2885
2886
2887
2890
2891
2892
2893
2894
2897
2898
2899
2900
2903
2904
2905
2906
2907
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2922
2923
2924QVariantConstPointer::QVariantConstPointer(QVariant variant)
2925 : m_variant(std::move(variant))
2930
2931
2932QVariant QVariantConstPointer::operator*()
const
2938
2939
2940
2941const QVariant *QVariantConstPointer::operator->()
const
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2959
2960
2961
2962
2965
2966
2967
2968
2971
2972
2973
2974
2975
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)