9#include "private/qlocale_p.h"
12#if QT_CONFIG(itemmodel)
13#include "qabstractitemmodel.h"
24#if QT_CONFIG(easingcurve)
25#include "qeasingcurve.h"
38#if QT_CONFIG(regularexpression)
39#include "qregularexpression.h"
53using namespace Qt::StringLiterals;
57static qlonglong qMetaTypeNumberBySize(
const QVariant::Private *d)
59 switch (d->typeInterface()->size) {
61 return d->get<
signed char>();
63 return d->get<
short>();
67 return d->get<qlonglong>();
69 Q_UNREACHABLE_RETURN(0);
72static qlonglong qMetaTypeNumber(
const QVariant::Private *d)
74 switch (d->typeInterface()->typeId) {
76 case QMetaType::LongLong:
78 case QMetaType::SChar:
79 case QMetaType::Short:
81 return qMetaTypeNumberBySize(d);
82 case QMetaType::Float:
83 return qRound64(d->get<
float>());
84 case QMetaType::Double:
85 return qRound64(d->get<
double>());
86 case QMetaType::QJsonValue:
87 return d->get<QJsonValue>().toDouble();
88 case QMetaType::QCborValue:
89 return d->get<QCborValue>().toInteger();
91 Q_UNREACHABLE_RETURN(0);
94static qulonglong qMetaTypeUNumber(
const QVariant::Private *d)
96 switch (d->typeInterface()->size) {
98 return d->get<
unsigned char>();
100 return d->get<
unsigned short>();
102 return d->get<
unsigned int>();
104 return d->get<qulonglong>();
106 Q_UNREACHABLE_RETURN(0);
109static std::optional<qlonglong> qConvertToNumber(
const QVariant::Private *d,
bool allowStringToBool =
false)
112 switch (d->typeInterface()->typeId) {
113 case QMetaType::QString: {
114 const QString &s = d->get<QString>();
115 if (qlonglong l = s.toLongLong(&ok); ok)
117 if (allowStringToBool) {
118 if (s ==
"false"_L1 || s ==
"0"_L1)
120 if (s ==
"true"_L1 || s ==
"1"_L1)
125 case QMetaType::QChar:
126 return d->get<QChar>().unicode();
127 case QMetaType::QByteArray:
128 if (qlonglong l = d->get<
QByteArray>().toLongLong(&ok); ok)
131 case QMetaType::Bool:
132 return qlonglong(d->get<
bool>());
133 case QMetaType::QCborValue:
134 if (!d->get<QCborValue>().isInteger() && !d->get<QCborValue>().isDouble())
136 return qMetaTypeNumber(d);
137 case QMetaType::QJsonValue:
138 if (!d->get<QJsonValue>().isDouble())
141 case QMetaType::Double:
143 case QMetaType::Char:
144 case QMetaType::SChar:
145 case QMetaType::Short:
146 case QMetaType::Long:
147 case QMetaType::Float:
148 case QMetaType::LongLong:
149 return qMetaTypeNumber(d);
150 case QMetaType::ULongLong:
151 case QMetaType::UInt:
152 case QMetaType::UChar:
153 case QMetaType::Char16:
154 case QMetaType::Char32:
155 case QMetaType::UShort:
156 case QMetaType::ULong:
157 return qlonglong(qMetaTypeUNumber(d));
158 case QMetaType::QCborSimpleType:
162 if (d->typeInterface()->flags & QMetaType::IsUnsignedEnumeration)
163 return qMetaTypeUNumber(d);
164 if (d->typeInterface()->flags & QMetaType::IsEnumeration)
165 return qMetaTypeNumberBySize(d);
170static std::optional<
double> qConvertToRealNumber(
const QVariant::Private *d)
173 switch (d->typeInterface()->typeId) {
174 case QMetaType::QString:
175 if (
double r = d->get<QString>().toDouble(&ok); ok)
178 case QMetaType::Double:
179 return d->get<
double>();
180 case QMetaType::Float:
181 return double(d->get<
float>());
182 case QMetaType::Float16:
184 case QMetaType::ULongLong:
185 case QMetaType::UInt:
186 case QMetaType::UChar:
187 case QMetaType::Char16:
188 case QMetaType::Char32:
189 case QMetaType::UShort:
190 case QMetaType::ULong:
191 return double(qMetaTypeUNumber(d));
192 case QMetaType::QCborValue:
193 return d->get<QCborValue>().toDouble();
194 case QMetaType::QJsonValue:
195 return d->get<QJsonValue>().toDouble();
198 if (std::optional<qlonglong> l = qConvertToNumber(d))
207 if (!iface || iface->size == 0)
210 Q_ASSERT(!isInterfaceFor<
void>(iface));
214 qWarning(
"QVariant: Provided metatype for '%s' does not support destruction and "
215 "copy construction", iface
->name);
221 qWarning(
"QVariant: Cannot create type '%s' without a default constructor", iface
->name);
228enum CustomConstructMoveOptions {
234enum CustomConstructNullabilityOption {
241template <CustomConstructMoveOptions moveOption = UseCopy, CustomConstructNullabilityOption nullability = MaybeNull>
243 std::conditional_t<moveOption == ForceMove,
void *,
const void *> copy)
247 Q_ASSERT(iface->size);
248 Q_ASSERT(!isInterfaceFor<
void>(iface));
252 if constexpr (moveOption == ForceMove)
254 if constexpr (nullability == NonNull)
255 Q_ASSERT(copy !=
nullptr);
260 d->is_null = !copy QT6_ONLY(|| isInterfaceFor<std::nullptr_t>(iface));
262 if (QVariant::Private::canUseInternalSpace(iface)) {
263 d->is_shared =
false;
266 if constexpr (moveOption == ForceMove && nullability == NonNull)
267 moveConstruct(iface, d->data.data, copy);
269 construct(iface, d->data.data, copy);
271 d->data.shared = customConstructShared(iface->size, iface->alignment, [=](
void *where) {
272 if constexpr (moveOption == ForceMove && nullability == NonNull)
273 moveConstruct(iface, where, copy);
275 construct(iface, where, copy);
281static void customClear(QVariant::Private *d)
290 QVariant::PrivateShared::free(d->data.shared);
294static QVariant::Private clonePrivate(
const QVariant::Private &other)
296 QVariant::Private d = other;
298 d.data.shared->ref.ref();
300 if (Q_LIKELY(d.canUseInternalSpace(iface))) {
308 d.data.shared = QVariant::PrivateShared::create(iface->size, iface->alignment);
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
469
470
471
472
473
474
475
478
479
480
481
482
483
486
487
488
489
492
493
494
495
496
497
498
499
501void QVariant::create(
int type,
const void *copy)
503 create(QMetaType(type), copy);
507
508
509
510
511
512void QVariant::create(QMetaType type,
const void *copy)
514 *
this = QVariant::fromMetaType(type, copy);
518
519
520
521
525 if (!d.is_shared || !d.data.shared->ref.deref())
530
531
532
533
534
536QVariant::QVariant(
const QVariant &p)
537 : d(clonePrivate(p.d))
542
543
544
545
546
547
548
549
550
551
552
553
554
557
558
559
560
561
562
563
564
565
569
570
571
572
573
574
575
576
577
580
581
582
583
584
585
586
587
589QVariant::QVariant(std::in_place_t, QMetaType type) : d(type.iface())
593 if (!Private::canUseInternalSpace(type.iface())) {
594 d.data.shared = PrivateShared::create(type.sizeOf(), type.alignOf());
600
601
602
603
604
605void *QVariant::prepareForEmplace(QMetaType type)
608
609
610
611
612
613 auto typeFits = [&] {
614 auto newIface = type.iface();
615 auto oldIface = d.typeInterface();
616 auto newSize = PrivateShared::computeAllocationSize(newIface->size, newIface->alignment);
617 auto oldSize = PrivateShared::computeAllocationSize(oldIface->size, oldIface->alignment);
618 return newSize <= oldSize;
620 if (Private::canUseInternalSpace(type.iface())) {
622 d.packedType = quintptr(type.iface()) >> 2;
624 }
else if (d.is_shared && isDetached() && typeFits()) {
625 QtMetaTypePrivate::destruct(d.typeInterface(), d.data.shared->data());
627 const auto ps = d.data.shared;
628 const auto align = type.alignOf();
629 ps->offset = PrivateShared::computeOffset(ps, align);
630 d.packedType = quintptr(type.iface()) >> 2;
634 QVariant newVariant(std::in_place, type);
637 return const_cast<
void *>(d.storage());
641
642
643
644
647
648
649
650
651
654
655
656
657
658
659
660
661
662
663
664
665
666
669
670
671
672
675
676
677
678
681
682
683
684
687
688
689
690
693
694
695
696
699
700
701
702
705
706
707
708
709
712
713
714
715
716
719
720
721
722
723
726
727
728
729
730
733
734
735
736
737
740
741
742
743
744
747
748
749
750
751
754
755
756
757
758
761
762
763
764
767
768
769
770
773
774
775
776
779
780
781
782
785
786
787
788
791
792
793
794
797
798
799
800
803
804
805
806
809
810
811
812
815
816
817
818
821
822
823
824
827
828
829
830
833
834
835
836
839
840
841
842
845
846
847
848
852
853
854
855
858
859
860
861
864
865
866
867
868
871
872
873
874
877
878
879
880
883
884
885
886
889
890
891
892
893
894
897
898
899
900
901
902
903
904
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924QVariant::QVariant(QMetaType type,
const void *copy)
925 : QVariant(fromMetaType(type, copy))
929QVariant::QVariant(
int val)
noexcept : d(std::piecewise_construct_t{}, val) {}
930QVariant::QVariant(uint val)
noexcept : d(std::piecewise_construct_t{}, val) {}
931QVariant::QVariant(qlonglong val)
noexcept : d(std::piecewise_construct_t{}, val) {}
932QVariant::QVariant(qulonglong val)
noexcept : d(std::piecewise_construct_t{}, val) {}
933QVariant::QVariant(
bool val)
noexcept : d(std::piecewise_construct_t{}, val) {}
934QVariant::QVariant(
double val)
noexcept : d(std::piecewise_construct_t{}, val) {}
935QVariant::QVariant(
float val)
noexcept : d(std::piecewise_construct_t{}, val) {}
937QVariant::QVariant(
const QByteArray &val)
noexcept : d(std::piecewise_construct_t{}, val) {}
938QVariant::QVariant(
const QBitArray &val)
noexcept : d(std::piecewise_construct_t{}, val) {}
939QVariant::QVariant(
const QString &val)
noexcept : d(std::piecewise_construct_t{}, val) {}
940QVariant::QVariant(QChar val)
noexcept : d(std::piecewise_construct_t{}, val) {}
941QVariant::QVariant(
const QStringList &val)
noexcept : d(std::piecewise_construct_t{}, val) {}
943QVariant::QVariant(QDate val)
noexcept : d(std::piecewise_construct_t{}, val) {}
944QVariant::QVariant(QTime val)
noexcept : d(std::piecewise_construct_t{}, val) {}
945QVariant::QVariant(
const QDateTime &val)
noexcept : d(std::piecewise_construct_t{}, val) {}
947QVariant::QVariant(
const QList<QVariant> &list)
noexcept : d(std::piecewise_construct_t{}, list) {}
948QVariant::QVariant(
const QMap<QString, QVariant> &map)
noexcept : d(std::piecewise_construct_t{}, map) {}
949QVariant::QVariant(
const QHash<QString, QVariant> &hash)
noexcept : d(std::piecewise_construct_t{}, hash) {}
951QVariant::QVariant(QLatin1StringView val) : QVariant(QString(val)) {}
953#if QT_CONFIG(easingcurve)
954QVariant::QVariant(
const QEasingCurve &val) : d(std::piecewise_construct_t{}, val) {}
956QVariant::QVariant(QPoint pt)
noexcept
957 : d(std::piecewise_construct_t{}, pt) {}
958QVariant::QVariant(QPointF pt)
noexcept(Private::FitsInInternalSize<
sizeof(qreal) * 2>)
959 : d(std::piecewise_construct_t{}, pt) {}
960QVariant::QVariant(QRect r)
noexcept(Private::FitsInInternalSize<
sizeof(
int) * 4>)
961 : d(std::piecewise_construct_t{}, r) {}
962QVariant::QVariant(QRectF r)
noexcept(Private::FitsInInternalSize<
sizeof(qreal) * 4>)
963 : d(std::piecewise_construct_t{}, r) {}
964QVariant::QVariant(QLine l)
noexcept(Private::FitsInInternalSize<
sizeof(
int) * 4>)
965 : d(std::piecewise_construct_t{}, l) {}
966QVariant::QVariant(QLineF l)
noexcept(Private::FitsInInternalSize<
sizeof(qreal) * 4>)
967 : d(std::piecewise_construct_t{}, l) {}
968QVariant::QVariant(QSize s)
noexcept
969 : d(std::piecewise_construct_t{}, s) {}
970QVariant::QVariant(QSizeF s)
noexcept(Private::FitsInInternalSize<
sizeof(qreal) * 2>)
971 : d(std::piecewise_construct_t{}, s) {}
972QVariant::QVariant(
const QUrl &u)
noexcept : d(std::piecewise_construct_t{}, u) {}
973QVariant::QVariant(
const QLocale &l)
noexcept : d(std::piecewise_construct_t{}, l) {}
974#if QT_CONFIG(regularexpression)
975QVariant::QVariant(
const QRegularExpression &re)
noexcept : d(std::piecewise_construct_t{}, re) {}
977QVariant::QVariant(QUuid uuid)
noexcept(Private::FitsInInternalSize<16>) : d(std::piecewise_construct_t{}, uuid) {}
978QVariant::QVariant(
const QJsonValue &jsonValue)
noexcept(Private::FitsInInternalSize<
sizeof(CborValueStandIn)>)
979 : d(std::piecewise_construct_t{}, jsonValue)
980{
static_assert(
sizeof(CborValueStandIn) ==
sizeof(QJsonValue)); }
981QVariant::QVariant(
const QJsonObject &jsonObject)
noexcept : d(std::piecewise_construct_t{}, jsonObject) {}
982QVariant::QVariant(
const QJsonArray &jsonArray)
noexcept : d(std::piecewise_construct_t{}, jsonArray) {}
983QVariant::QVariant(
const QJsonDocument &jsonDocument) : d(std::piecewise_construct_t{}, jsonDocument) {}
984#if QT_CONFIG(itemmodel)
985QVariant::QVariant(
const QModelIndex &modelIndex)
noexcept(Private::FitsInInternalSize<8 + 2 *
sizeof(quintptr)>)
986 : d(std::piecewise_construct_t{}, modelIndex) {}
987QVariant::QVariant(
const QPersistentModelIndex &modelIndex) : d(std::piecewise_construct_t{}, modelIndex) {}
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
1022
1023
1024
1027
1028
1029
1030
1031
1032
1033
1036
1037
1038
1039
1040
1043
1044
1045QVariant &QVariant::operator=(
const QVariant &variant)
1047 if (
this == &variant)
1051 d = clonePrivate(variant.d);
1056
1057
1058
1059
1062
1063
1064
1065
1067void QVariant::detach()
1069 if (!d.is_shared || d.data.shared->ref.loadRelaxed() == 1)
1072 Q_ASSERT(isValidMetaTypeForVariant(d.typeInterface(), constData()));
1073 Private dd(d.typeInterface());
1075 customConstruct<UseCopy, NonNull>(d.typeInterface(), &dd, constData());
1076 if (!d.data.shared->ref.deref())
1078 d.data.shared = dd.data.shared;
1082
1083
1084
1085
1088
1089
1090
1091
1092
1093
1094
1097
1098
1099
1100void QVariant::clear()
1102 if (!d.is_shared || !d.data.shared->ref.deref())
1108
1109
1110
1111
1112
1113
1114
1115
1118
1119
1120
1121
1122
1123
1124
1125
1126
1128#ifndef QT_NO_DATASTREAM
1132 QMetaType::UnknownType,
1133 QMetaType::QVariantMap,
1134 QMetaType::QVariantList,
1136 QMetaType::QStringList,
1143 QMetaType::QPalette,
1153 QMetaType::QPolygon,
1157 QMetaType::QSizePolicy,
1160 QMetaType::QDateTime,
1161 QMetaType::QByteArray,
1162 QMetaType::QBitArray,
1163#if QT_CONFIG(shortcut)
1164 QMetaType::QKeySequence,
1169 QMetaType::LongLong,
1170 QMetaType::ULongLong,
1171#if QT_CONFIG(easingcurve)
1172 QMetaType::QEasingCurve
1189
1190
1191
1192
1193
1194void QVariant::load(QDataStream &s)
1200 if (s.version() < QDataStream::Qt_4_0) {
1202 if (typeId >= MapFromThreeCount)
1204 typeId = mapIdFromQt3ToCurrent[typeId];
1205 }
else if (s.version() < QDataStream::Qt_5_0) {
1207 if (typeId == 127 ) {
1208 typeId = Qt5UserType;
1209 }
else if (typeId >= 128 && typeId != Qt5UserType) {
1213 }
else if (typeId == 75 ) {
1214 typeId = Qt5SizePolicy;
1215 }
else if (typeId > 75 && typeId <= 86) {
1221 if (s.version() < QDataStream::Qt_6_0) {
1223 if (typeId == Qt5UserType) {
1224 typeId = QMetaType::User;
1225 }
else if (typeId >= Qt5FirstGuiType && typeId <= Qt5LastGuiType) {
1226 typeId += Qt6ToQt5GuiTypeDelta;
1227 }
else if (typeId == Qt5SizePolicy) {
1228 typeId = QMetaType::QSizePolicy;
1229 }
else if (typeId == Qt5RegExp) {
1230 typeId = QMetaType::fromName(
"QRegExp").rawId();
1234 qint8 is_null =
false;
1235 if (s.version() >= QDataStream::Qt_4_2)
1237 if (typeId == QMetaType::User) {
1240 typeId = QMetaType::fromName(name).rawId();
1241 if (typeId == QMetaType::UnknownType) {
1242 s.setStatus(QDataStream::ReadCorruptData);
1243 qWarning(
"QVariant::load: unknown user type with name %s.", name.constData());
1247 *
this = fromMetaType(QMetaType(typeId));
1248 d.is_null = is_null;
1251 if (s.version() < QDataStream::Qt_5_0) {
1261 void *data =
const_cast<
void *>(constData());
1262 if (!d.type().load(s, data)) {
1263 s.setStatus(QDataStream::ReadCorruptData);
1264 qWarning(
"QVariant::load: unable to load type %d.", d.type().rawId());
1269
1270
1271
1272
1273
1274void QVariant::save(QDataStream &s)
const
1276 quint32 typeId = d.type().rawId();
1277 bool saveAsUserType =
false;
1278 if (typeId >= QMetaType::User) {
1279 typeId = QMetaType::User;
1280 saveAsUserType =
true;
1282 if (s.version() < QDataStream::Qt_6_0) {
1284 if (typeId == QMetaType::User) {
1285 typeId = Qt5UserType;
1286 if (!strcmp(d.type().name(),
"QRegExp")) {
1289 }
else if (typeId > Qt5LastCoreType && typeId <= QMetaType::LastCoreType) {
1291 typeId = Qt5UserType;
1292 saveAsUserType =
true;
1293 }
else if (typeId >= QMetaType::FirstGuiType && typeId <= QMetaType::LastGuiType) {
1294 typeId -= Qt6ToQt5GuiTypeDelta;
1295 if (typeId > Qt5LastGuiType) {
1296 typeId = Qt5UserType;
1297 saveAsUserType =
true;
1299 }
else if (typeId == QMetaType::QSizePolicy) {
1300 typeId = Qt5SizePolicy;
1303 if (s.version() < QDataStream::Qt_4_0) {
1305 for (i = 0; i <= MapFromThreeCount - 1; ++i) {
1306 if (mapIdFromQt3ToCurrent[i] == typeId) {
1311 if (i >= MapFromThreeCount) {
1315 }
else if (s.version() < QDataStream::Qt_5_0) {
1316 if (typeId == Qt5UserType) {
1318 saveAsUserType =
true;
1319 }
else if (typeId >= 128 - 97 && typeId <= Qt5LastCoreType) {
1323 }
else if (typeId == Qt5SizePolicy) {
1325 }
else if (typeId >= Qt5KeySequence && typeId <= Qt5QQuaternion) {
1328 }
else if (typeId > Qt5QQuaternion || typeId == QMetaType::QUuid) {
1331 saveAsUserType =
true;
1334 const char *typeName =
nullptr;
1335 if (saveAsUserType) {
1336 if (s.version() < QDataStream::Qt_6_0)
1337 typeName = QtMetaTypePrivate::typedefNameForType(d.type().d_ptr);
1339 typeName = d.type().name();
1342 if (s.version() >= QDataStream::Qt_4_2)
1343 s << qint8(d.is_null);
1348 if (s.version() < QDataStream::Qt_5_0)
1353 if (!d.type().save(s, constData())) {
1354 qWarning(
"QVariant::save: unable to save type '%s' (type id: %d).\n",
1355 d.type().name(), d.type().rawId());
1356 Q_ASSERT_X(
false,
"QVariant::save",
"Invalid type to save");
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1386
1387
1388
1389
1390
1391QDataStream &operator<<(QDataStream &s,
const QVariant &p)
1398
1399
1400
1401
1402
1405
1406
1407
1408
1409
1413
1414
1415
1416
1417
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429QStringList QVariant::toStringList()
const
1431 return qvariant_cast<QStringList>(*
this);
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449QString QVariant::toString()
const
1451 return qvariant_cast<QString>(*
this);
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465QVariantMap QVariant::toMap()
const
1467 return qvariant_cast<QVariantMap>(*
this);
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481QVariantHash QVariant::toHash()
const
1483 return qvariant_cast<QVariantHash>(*
this);
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498QDate QVariant::toDate()
const
1500 return qvariant_cast<QDate>(*
this);
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515QTime QVariant::toTime()
const
1517 return qvariant_cast<QTime>(*
this);
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532QDateTime QVariant::toDateTime()
const
1534 return qvariant_cast<QDateTime>(*
this);
1538
1539
1540
1541
1542
1543
1544
1545
1546#if QT_CONFIG(easingcurve)
1547QEasingCurve QVariant::toEasingCurve()
const
1549 return qvariant_cast<QEasingCurve>(*
this);
1554
1555
1556
1557
1558
1559
1560
1561
1562QByteArray QVariant::toByteArray()
const
1564 return qvariant_cast<QByteArray>(*
this);
1568
1569
1570
1571
1572
1573
1574
1575
1576QPoint QVariant::toPoint()
const
1578 return qvariant_cast<QPoint>(*
this);
1582
1583
1584
1585
1586
1587
1588
1589QRect QVariant::toRect()
const
1591 return qvariant_cast<QRect>(*
this);
1595
1596
1597
1598
1599
1600
1601
1602QSize QVariant::toSize()
const
1604 return qvariant_cast<QSize>(*
this);
1608
1609
1610
1611
1612
1613
1614
1615QSizeF QVariant::toSizeF()
const
1617 return qvariant_cast<QSizeF>(*
this);
1621
1622
1623
1624
1625
1626
1627
1628
1629QRectF QVariant::toRectF()
const
1631 return qvariant_cast<QRectF>(*
this);
1635
1636
1637
1638
1639
1640
1641
1642QLineF QVariant::toLineF()
const
1644 return qvariant_cast<QLineF>(*
this);
1648
1649
1650
1651
1652
1653
1654
1655QLine QVariant::toLine()
const
1657 return qvariant_cast<QLine>(*
this);
1661
1662
1663
1664
1665
1666
1667
1668
1669QPointF QVariant::toPointF()
const
1671 return qvariant_cast<QPointF>(*
this);
1675
1676
1677
1678
1679
1680
1681
1682QUrl QVariant::toUrl()
const
1684 return qvariant_cast<QUrl>(*
this);
1688
1689
1690
1691
1692
1693
1694
1695QLocale QVariant::toLocale()
const
1697 return qvariant_cast<QLocale>(*
this);
1700#if QT_CONFIG(regularexpression)
1702
1703
1704
1705
1706
1707
1708
1709
1710QRegularExpression QVariant::toRegularExpression()
const
1712 return qvariant_cast<QRegularExpression>(*
this);
1716#if QT_CONFIG(itemmodel)
1718
1719
1720
1721
1722
1723
1724
1725QModelIndex QVariant::toModelIndex()
const
1727 return qvariant_cast<QModelIndex>(*
this);
1731
1732
1733
1734
1735
1736
1737
1738QPersistentModelIndex QVariant::toPersistentModelIndex()
const
1740 return qvariant_cast<QPersistentModelIndex>(*
this);
1745
1746
1747
1748
1749
1750
1751
1752
1753QUuid QVariant::toUuid()
const
1755 return qvariant_cast<QUuid>(*
this);
1759
1760
1761
1762
1763
1764
1765
1766QJsonValue QVariant::toJsonValue()
const
1768 return qvariant_cast<QJsonValue>(*
this);
1772
1773
1774
1775
1776
1777
1778
1779QJsonObject QVariant::toJsonObject()
const
1781 return qvariant_cast<QJsonObject>(*
this);
1785
1786
1787
1788
1789
1790
1791
1792QJsonArray QVariant::toJsonArray()
const
1794 return qvariant_cast<QJsonArray>(*
this);
1798
1799
1800
1801
1802
1803
1804
1805QJsonDocument QVariant::toJsonDocument()
const
1807 return qvariant_cast<QJsonDocument>(*
this);
1811
1812
1813
1814
1815
1816
1817
1818
1819QChar QVariant::toChar()
const
1821 return qvariant_cast<QChar>(*
this);
1825
1826
1827
1828
1829
1830QBitArray QVariant::toBitArray()
const
1832 return qvariant_cast<QBitArray>(*
this);
1835template <
typename T>
1846 bool success = QMetaType::convert(d.type(), d.storage(), t, &ret);
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869int QVariant::toInt(
bool *ok)
const
1871 return qNumVariantToHelper<
int>(d, ok);
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891uint QVariant::toUInt(
bool *ok)
const
1893 return qNumVariantToHelper<uint>(d, ok);
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908qlonglong QVariant::toLongLong(
bool *ok)
const
1910 return qNumVariantToHelper<qlonglong>(d, ok);
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925qulonglong QVariant::toULongLong(
bool *ok)
const
1927 return qNumVariantToHelper<qulonglong>(d, ok);
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942bool QVariant::toBool()
const
1944 auto boolType = QMetaType::fromType<
bool>();
1945 if (d.type() == boolType)
1946 return d.get<
bool>();
1949 QMetaType::convert(d.type(), constData(), boolType, &res);
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965double QVariant::toDouble(
bool *ok)
const
1967 return qNumVariantToHelper<
double>(d, ok);
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984float QVariant::toFloat(
bool *ok)
const
1986 return qNumVariantToHelper<
float>(d, ok);
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003qreal QVariant::toReal(
bool *ok)
const
2005 return qNumVariantToHelper<qreal>(d, ok);
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019QVariantList QVariant::toList()
const
2021 return qvariant_cast<QVariantList>(*
this);
2025
2026
2027
2028
2029
2030
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2093bool QVariant::convert(QMetaType targetType)
2095 if (d.type() == targetType)
2096 return targetType.isValid();
2098 QVariant oldValue = std::exchange(*
this, QVariant::fromMetaType(targetType));
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;
2274 return tp == QMetaType::ULongLong || tp == QMetaType::ULong ||
2275 tp == QMetaType::UInt || tp == QMetaType::Char32 ||
2276 (iface->flags & QMetaType::IsUnsignedEnumeration && iface->size >=
sizeof(
int));
2278 bool isUnsigned1 = isUnsigned(t1, iface1);
2279 bool isUnsigned2 = isUnsigned(t2, iface2);
2283 if (isUnsigned1 && iface1->size >
sizeof(
int))
2284 return QMetaType::ULongLong;
2285 if (isUnsigned2 && iface2->size >
sizeof(
int))
2286 return QMetaType::ULongLong;
2289 if (iface1->size >
sizeof(
int) || iface2->size >
sizeof(
int))
2290 return QMetaType::LongLong;
2293 if (isUnsigned1 || isUnsigned2)
2294 return QMetaType::UInt;
2297 return QMetaType::Int;
2303 std::optional<qlonglong> l1 = qConvertToNumber(d1, promotedType == QMetaType::Bool);
2304 std::optional<qlonglong> l2 = qConvertToNumber(d2, promotedType == QMetaType::Bool);
2306 return QPartialOrdering::Unordered;
2307 if (promotedType == QMetaType::UInt)
2308 return Qt::compareThreeWay(uint(*l1), uint(*l2));
2309 if (promotedType == QMetaType::LongLong)
2310 return Qt::compareThreeWay(qlonglong(*l1), qlonglong(*l2));
2311 if (promotedType == QMetaType::ULongLong)
2312 return Qt::compareThreeWay(qulonglong(*l1), qulonglong(*l2));
2314 return Qt::compareThreeWay(
int(*l1),
int(*l2));
2319 uint promotedType = numericTypePromotion(d1->typeInterface(), d2->typeInterface());
2320 if (promotedType != QMetaType::QReal)
2321 return integralCompare(promotedType, d1, d2);
2324 const auto r1 = qConvertToRealNumber(d1);
2325 const auto r2 = qConvertToRealNumber(d2);
2327 return QPartialOrdering::Unordered;
2329 return Qt::compareThreeWay(*r1, *r2);
2334 if ((fromType.flags() & QMetaType::PointerToQObject)
2335 && (toType.flags() & QMetaType::PointerToQObject)) {
2336 const QMetaObject *f = fromType.metaObject();
2337 const QMetaObject *t = toType.metaObject();
2338 return f && t && (f->inherits(t) || t->inherits(f));
2345 return Qt::compareThreeWay(Qt::totally_ordered_wrapper(d1->get<QObject *>()),
2346 Qt::totally_ordered_wrapper(d2->get<QObject *>()));
2350
2351
2352bool QVariant::equals(
const QVariant &v)
const
2354 auto metatype = d.type();
2356 if (metatype != v.metaType()) {
2358 if (canBeNumericallyCompared(metatype.iface(), v.d.type().iface()))
2359 return numericCompare(&d, &v.d) == QPartialOrdering::Equivalent;
2361 if (qvCanConvertMetaObject(metatype, v.metaType()))
2362 return pointerCompare(&d, &v.d) == QPartialOrdering::Equivalent;
2367 if (!metatype.isValid())
2370 return metatype.equals(d.storage(), v.d.storage());
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395QPartialOrdering QVariant::compare(
const QVariant &lhs,
const QVariant &rhs)
2397 QMetaType t = lhs.d.type();
2398 if (t != rhs.d.type()) {
2400 if (canBeNumericallyCompared(lhs.d.type().iface(), rhs.d.type().iface()))
2401 return numericCompare(&lhs.d, &rhs.d);
2402 if (qvCanConvertMetaObject(lhs.metaType(), rhs.metaType()))
2403 return pointerCompare(&lhs.d, &rhs.d);
2404 return QPartialOrdering::Unordered;
2406 return t.compare(lhs.constData(), rhs.constData());
2410
2411
2412
2413
2414
2415
2416
2417
2420
2421
2422
2423
2424
2425
2426
2427
2428void *QVariant::data()
2433 return const_cast<
void *>(constData());
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483bool QVariant::isNull()
const
2485 if (d.is_null || !metaType().isValid())
2487 if (metaType().flags() & QMetaType::IsPointer)
2488 return d.get<
void *>() ==
nullptr;
2492#ifndef QT_NO_DEBUG_STREAM
2493QDebug QVariant::qdebugHelper(QDebug dbg)
const
2495 QDebugStateSaver saver(dbg);
2496 const uint typeId = d.type().rawId();
2497 dbg.nospace() <<
"QVariant(";
2498 if (typeId != QMetaType::UnknownType) {
2499 dbg << d.type().name() <<
", ";
2500 bool streamed = d.type().debugStream(dbg, d.storage());
2501 if (!streamed && canConvert<QString>())
2510QVariant QVariant::moveConstruct(QMetaType type,
void *data)
2513 var.d = QVariant::Private(type.d_ptr);
2514 customConstruct<ForceMove, NonNull>(type.d_ptr, &var.d, data);
2518QVariant QVariant::copyConstruct(QMetaType type,
const void *data)
2521 var.d = QVariant::Private(type.d_ptr);
2522 customConstruct<UseCopy, NonNull>(type.d_ptr, &var.d, data);
2526#if QT_DEPRECATED_SINCE(6
, 0
)
2528QT_WARNING_DISABLE_DEPRECATED
2530QDebug operator<<(QDebug dbg,
const QVariant::Type p)
2532 QDebugStateSaver saver(dbg);
2533 dbg.nospace() <<
"QVariant::"
2534 << (
int(p) !=
int(QMetaType::UnknownType)
2535 ? QMetaType(p).name()
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2559
2560
2561
2562
2565
2566
2567
2568
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
2596
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2627
2628
2629
2630
2631
2632
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2647
2648
2649
2650
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2666
2667
2668
2669
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694QVariant QVariant::fromMetaType(QMetaType type,
const void *copy)
2697 type.registerType();
2698 const auto iface = type.iface();
2699 if (isValidMetaTypeForVariant(iface, copy)) {
2700 result.d = Private(iface);
2701 customConstruct(iface, &result.d, copy);
2707
2708
2709
2710
2711
2712
2713
2714
2715
2718
2719
2720
2721
2722
2723
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2758
2759
2760
2761
2762
2765
2766
2767
2768
2769
2772
2773
2774
2775
2776
2777
2780
2781
2782
2784
2785
2788
2789
2792
2793
2796
2797
2800
2801
2804
2805
2808
2809
2812
2813
2816
2817
2820
2821
2822
2825
2826
2827
2830
2831
2832const void *QtPrivate::QVariantTypeCoercer::convert(
const QVariant &value,
const QMetaType &type)
2834 if (type == QMetaType::fromType<QVariant>())
2837 if (type == value.metaType())
2838 return value.constData();
2840 if (value.canConvert(type)) {
2842 if (converted.convert(type))
2843 return converted.constData();
2850
2851
2852const void *QtPrivate::QVariantTypeCoercer::coerce(
const QVariant &value,
const QMetaType &type)
2854 if (
const void *result = convert(value, type))
2857 converted = QVariant(type);
2858 return converted.constData();
2861#if QT_DEPRECATED_SINCE(6
, 15
)
2863QT_WARNING_DISABLE_DEPRECATED
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2880
2881
2882
2883
2886
2887
2888
2889
2890
2893
2894
2895
2896
2897
2900
2901
2902
2903
2904
2907
2908
2909
2910
2913
2914
2915
2916
2917
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2933
2934
2935QVariantConstPointer::QVariantConstPointer(QVariant variant)
2936 : m_variant(std::move(variant))
2941
2942
2943QVariant QVariantConstPointer::operator*()
const
2949
2950
2951
2952const QVariant *QVariantConstPointer::operator->()
const
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2971
2972
2973
2974
2977
2978
2979
2980
2983
2984
2985
2986
2987
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3008
3009
3010
3011
3014
3015
3016
3017
3020
3021
3022
3023
3026
3027
3028
3029
3033
3034
3035
3036
3037
3038
3039
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3057
3058
3059
3060
3063
3064
3065
3066
3069
3070
3071
3072
3075
3076
3077
3078
3081
3082
3083
3084
3087
3088
3089
3090
3093
3094
3095
3096
3097
3098
3101
3102
3103
3104
3105
3106
3107
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3123
3124
3125
3126
3129
3130
3131
3132
3135
3136
3137
3138
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3154
3155
3156
3157
3160
3161
3162
3163
3166
3167
3168
3169
3172
3173
3174
3175
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)