14#if QT_CONFIG(easingcurve)
15#include "qeasingcurve.h"
18#if QT_CONFIG(regularexpression)
19#include "qregularexpression.h"
26#if QT_CONFIG(itemmodel)
27#include "qabstractitemmodel.h"
29#ifndef QT_BOOTSTRAPPED
39#include "private/qlocale_p.h"
41#include <qmetaobject.h>
43#ifndef QT_NO_GEOM_VARIANT
58using namespace Qt::StringLiterals;
62static qlonglong qMetaTypeNumberBySize(
const QVariant::Private *d)
64 switch (d->typeInterface()->size) {
66 return d->get<
signed char>();
68 return d->get<
short>();
72 return d->get<qlonglong>();
74 Q_UNREACHABLE_RETURN(0);
77static qlonglong qMetaTypeNumber(
const QVariant::Private *d)
79 switch (d->typeInterface()->typeId) {
81 case QMetaType::LongLong:
83 case QMetaType::SChar:
84 case QMetaType::Short:
86 return qMetaTypeNumberBySize(d);
87 case QMetaType::Float:
88 return qRound64(d->get<
float>());
89 case QMetaType::Double:
90 return qRound64(d->get<
double>());
91#ifndef QT_BOOTSTRAPPED
92 case QMetaType::QJsonValue:
93 return d->get<QJsonValue>().toDouble();
94 case QMetaType::QCborValue:
95 return d->get<QCborValue>().toInteger();
98 Q_UNREACHABLE_RETURN(0);
101static qulonglong qMetaTypeUNumber(
const QVariant::Private *d)
103 switch (d->typeInterface()->size) {
105 return d->get<
unsigned char>();
107 return d->get<
unsigned short>();
109 return d->get<
unsigned int>();
111 return d->get<qulonglong>();
113 Q_UNREACHABLE_RETURN(0);
116static std::optional<qlonglong> qConvertToNumber(
const QVariant::Private *d,
bool allowStringToBool =
false)
119 switch (d->typeInterface()->typeId) {
120 case QMetaType::QString: {
121 const QString &s = d->get<QString>();
122 if (qlonglong l = s.toLongLong(&ok); ok)
124 if (allowStringToBool) {
125 if (s ==
"false"_L1 || s ==
"0"_L1)
127 if (s ==
"true"_L1 || s ==
"1"_L1)
132 case QMetaType::QChar:
133 return d->get<QChar>().unicode();
134 case QMetaType::QByteArray:
135 if (qlonglong l = d->get<QByteArray>().toLongLong(&ok); ok)
138 case QMetaType::Bool:
139 return qlonglong(d->get<
bool>());
140#ifndef QT_BOOTSTRAPPED
141 case QMetaType::QCborValue:
142 if (!d->get<QCborValue>().isInteger() && !d->get<QCborValue>().isDouble())
144 return qMetaTypeNumber(d);
145 case QMetaType::QJsonValue:
146 if (!d->get<QJsonValue>().isDouble())
150 case QMetaType::Double:
152 case QMetaType::Char:
153 case QMetaType::SChar:
154 case QMetaType::Short:
155 case QMetaType::Long:
156 case QMetaType::Float:
157 case QMetaType::LongLong:
158 return qMetaTypeNumber(d);
159 case QMetaType::ULongLong:
160 case QMetaType::UInt:
161 case QMetaType::UChar:
162 case QMetaType::Char16:
163 case QMetaType::Char32:
164 case QMetaType::UShort:
165 case QMetaType::ULong:
166 return qlonglong(qMetaTypeUNumber(d));
169 if (d->typeInterface()->flags & QMetaType::IsEnumeration
170 || d->typeInterface()->typeId == QMetaType::QCborSimpleType)
171 return qMetaTypeNumberBySize(d);
176static std::optional<
double> qConvertToRealNumber(
const QVariant::Private *d)
179 switch (d->typeInterface()->typeId) {
180 case QMetaType::QString:
181 if (
double r = d->get<QString>().toDouble(&ok); ok)
184 case QMetaType::Double:
185 return d->get<
double>();
186 case QMetaType::Float:
187 return double(d->get<
float>());
188 case QMetaType::Float16:
190 case QMetaType::ULongLong:
191 case QMetaType::UInt:
192 case QMetaType::UChar:
193 case QMetaType::Char16:
194 case QMetaType::Char32:
195 case QMetaType::UShort:
196 case QMetaType::ULong:
197 return double(qMetaTypeUNumber(d));
198#ifndef QT_BOOTSTRAPPED
199 case QMetaType::QCborValue:
200 return d->get<QCborValue>().toDouble();
201 case QMetaType::QJsonValue:
202 return d->get<QJsonValue>().toDouble();
206 if (std::optional<qlonglong> l = qConvertToNumber(d))
215 if (!iface || iface->size == 0)
218 Q_ASSERT(!isInterfaceFor<
void>(iface));
222 qWarning(
"QVariant: Provided metatype for '%s' does not support destruction and "
223 "copy construction", iface
->name);
229 qWarning(
"QVariant: Cannot create type '%s' without a default constructor", iface
->name);
236enum CustomConstructMoveOptions {
242enum CustomConstructNullabilityOption {
249template <CustomConstructMoveOptions moveOption = UseCopy, CustomConstructNullabilityOption nullability = MaybeNull>
251 std::conditional_t<moveOption == ForceMove,
void *,
const void *> copy)
255 Q_ASSERT(iface->size);
256 Q_ASSERT(!isInterfaceFor<
void>(iface));
260 if constexpr (moveOption == ForceMove)
262 if constexpr (nullability == NonNull)
263 Q_ASSERT(copy !=
nullptr);
268 d->is_null = !copy QT6_ONLY(|| isInterfaceFor<std::nullptr_t>(iface));
270 if (QVariant::Private::canUseInternalSpace(iface)) {
271 d->is_shared =
false;
274 if constexpr (moveOption == ForceMove && nullability == NonNull)
275 moveConstruct(iface, d->data.data, copy);
277 construct(iface, d->data.data, copy);
279 d->data.shared = customConstructShared(iface->size, iface->alignment, [=](
void *where) {
280 if constexpr (moveOption == ForceMove && nullability == NonNull)
281 moveConstruct(iface, where, copy);
283 construct(iface, where, copy);
289static void customClear(QVariant::Private *d)
298 QVariant::PrivateShared::free(d->data.shared);
302static QVariant::Private clonePrivate(
const QVariant::Private &other)
304 QVariant::Private d = other;
306 d.data.shared->ref.ref();
308 Q_ASSERT(d.canUseInternalSpace(iface));
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
396
397
398
399
400
401
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
467
468
469
470
471
472
475
476
477
478
479
480
481
484
485
486
487
488
489
492
493
494
495
498
499
500
501
502
503
504
505
507void QVariant::create(
int type,
const void *copy)
509 create(QMetaType(type), copy);
513
514
515
516
517
518void QVariant::create(QMetaType type,
const void *copy)
520 *
this = QVariant::fromMetaType(type, copy);
524
525
526
527
531 if (!d.is_shared || !d.data.shared->ref.deref())
536
537
538
539
540
542QVariant::QVariant(
const QVariant &p)
543 : d(clonePrivate(p.d))
548
549
550
551
552
553
554
555
556
557
558
559
560
561
564
565
566
567
568
569
570
571
572
576
577
578
579
580
581
582
583
584
587
588
589
590
591
592
593
594
596QVariant::QVariant(std::in_place_t, QMetaType type) : d(type.iface())
600 if (!Private::canUseInternalSpace(type.iface())) {
601 d.data.shared = PrivateShared::create(type.sizeOf(), type.alignOf());
607
608
609
610
611
612void *QVariant::prepareForEmplace(QMetaType type)
615
616
617
618
619
620 auto typeFits = [&] {
621 auto newIface = type.iface();
622 auto oldIface = d.typeInterface();
623 auto newSize = PrivateShared::computeAllocationSize(newIface->size, newIface->alignment);
624 auto oldSize = PrivateShared::computeAllocationSize(oldIface->size, oldIface->alignment);
625 return newSize <= oldSize;
627 if (Private::canUseInternalSpace(type.iface())) {
629 d.packedType = quintptr(type.iface()) >> 2;
631 }
else if (d.is_shared && isDetached() && typeFits()) {
632 QtMetaTypePrivate::destruct(d.typeInterface(), d.data.shared->data());
634 const auto ps = d.data.shared;
635 const auto align = type.alignOf();
636 ps->offset = PrivateShared::computeOffset(ps, align);
637 d.packedType = quintptr(type.iface()) >> 2;
641 QVariant newVariant(std::in_place, type);
644 return const_cast<
void *>(d.storage());
648
649
650
651
654
655
656
657
658
661
662
663
664
665
666
667
668
669
670
671
672
673
676
677
678
679
682
683
684
685
688
689
690
691
694
695
696
697
700
701
702
703
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
765
768
769
770
771
774
775
776
777
780
781
782
783
786
787
788
789
792
793
794
795
798
799
800
801
804
805
806
807
810
811
812
813
816
817
818
819
822
823
824
825
828
829
830
831
834
835
836
837
840
841
842
843
846
847
848
849
852
853
854
855
859
860
861
862
865
866
867
868
871
872
873
874
875
878
879
880
881
884
885
886
887
890
891
892
893
896
897
898
899
900
901
904
905
906
907
908
909
910
911
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931QVariant::QVariant(QMetaType type,
const void *copy)
934 *
this = fromMetaType(type, copy);
937QVariant::QVariant(
int val)
noexcept : d(std::piecewise_construct_t{}, val) {}
938QVariant::QVariant(uint val)
noexcept : d(std::piecewise_construct_t{}, val) {}
939QVariant::QVariant(qlonglong val)
noexcept : d(std::piecewise_construct_t{}, val) {}
940QVariant::QVariant(qulonglong val)
noexcept : d(std::piecewise_construct_t{}, val) {}
941QVariant::QVariant(
bool val)
noexcept : d(std::piecewise_construct_t{}, val) {}
942QVariant::QVariant(
double val)
noexcept : d(std::piecewise_construct_t{}, val) {}
943QVariant::QVariant(
float val)
noexcept : d(std::piecewise_construct_t{}, val) {}
945QVariant::QVariant(
const QByteArray &val)
noexcept : d(std::piecewise_construct_t{}, val) {}
946#ifndef QT_BOOTSTRAPPED
947QVariant::QVariant(
const QBitArray &val)
noexcept : d(std::piecewise_construct_t{}, val) {}
949QVariant::QVariant(
const QString &val)
noexcept : d(std::piecewise_construct_t{}, val) {}
950QVariant::QVariant(QChar val)
noexcept : d(std::piecewise_construct_t{}, val) {}
951QVariant::QVariant(
const QStringList &val)
noexcept : d(std::piecewise_construct_t{}, val) {}
953QVariant::QVariant(QDate val)
noexcept : d(std::piecewise_construct_t{}, val) {}
954QVariant::QVariant(QTime val)
noexcept : d(std::piecewise_construct_t{}, val) {}
955QVariant::QVariant(
const QDateTime &val)
noexcept : d(std::piecewise_construct_t{}, val) {}
957QVariant::QVariant(
const QList<QVariant> &list)
noexcept : d(std::piecewise_construct_t{}, list) {}
958QVariant::QVariant(
const QMap<QString, QVariant> &map)
noexcept : d(std::piecewise_construct_t{}, map) {}
959QVariant::QVariant(
const QHash<QString, QVariant> &hash)
noexcept : d(std::piecewise_construct_t{}, hash) {}
961QVariant::QVariant(QLatin1StringView val) : QVariant(QString(val)) {}
963#if QT_CONFIG(easingcurve)
964QVariant::QVariant(
const QEasingCurve &val) : d(std::piecewise_construct_t{}, val) {}
966#ifndef QT_NO_GEOM_VARIANT
967QVariant::QVariant(QPoint pt)
noexcept
968 : d(std::piecewise_construct_t{}, pt) {}
969QVariant::QVariant(QPointF pt)
noexcept(Private::FitsInInternalSize<
sizeof(qreal) * 2>)
970 : d(std::piecewise_construct_t{}, pt) {}
971QVariant::QVariant(QRect r)
noexcept(Private::FitsInInternalSize<
sizeof(
int) * 4>)
972 : d(std::piecewise_construct_t{}, r) {}
973QVariant::QVariant(QRectF r)
noexcept(Private::FitsInInternalSize<
sizeof(qreal) * 4>)
974 : d(std::piecewise_construct_t{}, r) {}
975QVariant::QVariant(QLine l)
noexcept(Private::FitsInInternalSize<
sizeof(
int) * 4>)
976 : d(std::piecewise_construct_t{}, l) {}
977QVariant::QVariant(QLineF l)
noexcept(Private::FitsInInternalSize<
sizeof(qreal) * 4>)
978 : d(std::piecewise_construct_t{}, l) {}
979QVariant::QVariant(QSize s)
noexcept
980 : d(std::piecewise_construct_t{}, s) {}
981QVariant::QVariant(QSizeF s)
noexcept(Private::FitsInInternalSize<
sizeof(qreal) * 2>)
982 : d(std::piecewise_construct_t{}, s) {}
984#ifndef QT_BOOTSTRAPPED
985QVariant::QVariant(
const QUrl &u)
noexcept : d(std::piecewise_construct_t{}, u) {}
987QVariant::QVariant(
const QLocale &l)
noexcept : d(std::piecewise_construct_t{}, l) {}
988#if QT_CONFIG(regularexpression)
989QVariant::QVariant(
const QRegularExpression &re)
noexcept : d(std::piecewise_construct_t{}, re) {}
991QVariant::QVariant(QUuid uuid)
noexcept(Private::FitsInInternalSize<16>) : d(std::piecewise_construct_t{}, uuid) {}
992#ifndef QT_BOOTSTRAPPED
993QVariant::QVariant(
const QJsonValue &jsonValue)
noexcept(Private::FitsInInternalSize<
sizeof(CborValueStandIn)>)
994 : d(std::piecewise_construct_t{}, jsonValue)
995{
static_assert(
sizeof(CborValueStandIn) ==
sizeof(QJsonValue)); }
996QVariant::QVariant(
const QJsonObject &jsonObject)
noexcept : d(std::piecewise_construct_t{}, jsonObject) {}
997QVariant::QVariant(
const QJsonArray &jsonArray)
noexcept : d(std::piecewise_construct_t{}, jsonArray) {}
998QVariant::QVariant(
const QJsonDocument &jsonDocument) : d(std::piecewise_construct_t{}, jsonDocument) {}
1000#if QT_CONFIG(itemmodel)
1001QVariant::QVariant(
const QModelIndex &modelIndex)
noexcept(Private::FitsInInternalSize<8 + 2 *
sizeof(quintptr)>)
1002 : d(std::piecewise_construct_t{}, modelIndex) {}
1003QVariant::QVariant(
const QPersistentModelIndex &modelIndex) : d(std::piecewise_construct_t{}, modelIndex) {}
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1043
1044
1045
1046
1047
1048
1049
1052
1053
1054
1055
1056QMetaType QVariant::metaType()
const
1062
1063
1064QVariant &QVariant::operator=(
const QVariant &variant)
1066 if (
this == &variant)
1070 d = clonePrivate(variant.d);
1075
1076
1077
1078
1081
1082
1083
1084
1086void QVariant::detach()
1088 if (!d.is_shared || d.data.shared->ref.loadRelaxed() == 1)
1091 Q_ASSERT(isValidMetaTypeForVariant(d.typeInterface(), constData()));
1092 Private dd(d.typeInterface());
1094 customConstruct<UseCopy, NonNull>(d.typeInterface(), &dd, constData());
1095 if (!d.data.shared->ref.deref())
1097 d.data.shared = dd.data.shared;
1101
1102
1103
1104
1107
1108
1109
1110
1111
1112const char *QVariant::typeName()
const
1114 return d.type().name();
1118
1119
1120
1121void QVariant::clear()
1123 if (!d.is_shared || !d.data.shared->ref.deref())
1129
1130
1131
1132
1133
1134
1135
1136
1139
1140
1141
1142
1143
1144
1145
1146
1147
1149#ifndef QT_NO_DATASTREAM
1153 QMetaType::UnknownType,
1154 QMetaType::QVariantMap,
1155 QMetaType::QVariantList,
1157 QMetaType::QStringList,
1164 QMetaType::QPalette,
1174 QMetaType::QPolygon,
1178 QMetaType::QSizePolicy,
1181 QMetaType::QDateTime,
1182 QMetaType::QByteArray,
1183 QMetaType::QBitArray,
1184#if QT_CONFIG(shortcut)
1185 QMetaType::QKeySequence,
1190 QMetaType::LongLong,
1191 QMetaType::ULongLong,
1192#if QT_CONFIG(easingcurve)
1193 QMetaType::QEasingCurve
1210
1211
1212
1213
1214
1215void QVariant::load(QDataStream &s)
1221 if (s.version() < QDataStream::Qt_4_0) {
1223 if (typeId >= MapFromThreeCount)
1225 typeId = mapIdFromQt3ToCurrent[typeId];
1226 }
else if (s.version() < QDataStream::Qt_5_0) {
1228 if (typeId == 127 ) {
1229 typeId = Qt5UserType;
1230 }
else if (typeId >= 128 && typeId != Qt5UserType) {
1234 }
else if (typeId == 75 ) {
1235 typeId = Qt5SizePolicy;
1236 }
else if (typeId > 75 && typeId <= 86) {
1242 if (s.version() < QDataStream::Qt_6_0) {
1244 if (typeId == Qt5UserType) {
1245 typeId = QMetaType::User;
1246 }
else if (typeId >= Qt5FirstGuiType && typeId <= Qt5LastGuiType) {
1247 typeId += Qt6ToQt5GuiTypeDelta;
1248 }
else if (typeId == Qt5SizePolicy) {
1249 typeId = QMetaType::QSizePolicy;
1250 }
else if (typeId == Qt5RegExp) {
1251 typeId = QMetaType::fromName(
"QRegExp").id();
1255 qint8 is_null =
false;
1256 if (s.version() >= QDataStream::Qt_4_2)
1258 if (typeId == QMetaType::User) {
1261 typeId = QMetaType::fromName(name).id();
1262 if (typeId == QMetaType::UnknownType) {
1263 s.setStatus(QDataStream::ReadCorruptData);
1264 qWarning(
"QVariant::load: unknown user type with name %s.", name.constData());
1268 create(typeId,
nullptr);
1269 d.is_null = is_null;
1272 if (s.version() < QDataStream::Qt_5_0) {
1282 void *data =
const_cast<
void *>(constData());
1283 if (!d.type().load(s, data)) {
1284 s.setStatus(QDataStream::ReadCorruptData);
1285 qWarning(
"QVariant::load: unable to load type %d.", d.type().id());
1290
1291
1292
1293
1294
1295void QVariant::save(QDataStream &s)
const
1297 quint32 typeId = d.type().id();
1298 bool saveAsUserType =
false;
1299 if (typeId >= QMetaType::User) {
1300 typeId = QMetaType::User;
1301 saveAsUserType =
true;
1303 if (s.version() < QDataStream::Qt_6_0) {
1305 if (typeId == QMetaType::User) {
1306 typeId = Qt5UserType;
1307 if (!strcmp(d.type().name(),
"QRegExp")) {
1310 }
else if (typeId > Qt5LastCoreType && typeId <= QMetaType::LastCoreType) {
1312 typeId = Qt5UserType;
1313 saveAsUserType =
true;
1314 }
else if (typeId >= QMetaType::FirstGuiType && typeId <= QMetaType::LastGuiType) {
1315 typeId -= Qt6ToQt5GuiTypeDelta;
1316 if (typeId > Qt5LastGuiType) {
1317 typeId = Qt5UserType;
1318 saveAsUserType =
true;
1320 }
else if (typeId == QMetaType::QSizePolicy) {
1321 typeId = Qt5SizePolicy;
1324 if (s.version() < QDataStream::Qt_4_0) {
1326 for (i = 0; i <= MapFromThreeCount - 1; ++i) {
1327 if (mapIdFromQt3ToCurrent[i] == typeId) {
1332 if (i >= MapFromThreeCount) {
1336 }
else if (s.version() < QDataStream::Qt_5_0) {
1337 if (typeId == Qt5UserType) {
1339 saveAsUserType =
true;
1340 }
else if (typeId >= 128 - 97 && typeId <= Qt5LastCoreType) {
1344 }
else if (typeId == Qt5SizePolicy) {
1346 }
else if (typeId >= Qt5KeySequence && typeId <= Qt5QQuaternion) {
1349 }
else if (typeId > Qt5QQuaternion || typeId == QMetaType::QUuid) {
1352 saveAsUserType =
true;
1355 const char *typeName =
nullptr;
1356 if (saveAsUserType) {
1357 if (s.version() < QDataStream::Qt_6_0)
1358 typeName = QtMetaTypePrivate::typedefNameForType(d.type().d_ptr);
1360 typeName = d.type().name();
1363 if (s.version() >= QDataStream::Qt_4_2)
1364 s << qint8(d.is_null);
1369 if (s.version() < QDataStream::Qt_5_0)
1374 if (!d.type().save(s, constData())) {
1375 qWarning(
"QVariant::save: unable to save type '%s' (type id: %d).\n",
1376 d.type().name(), d.type().id());
1377 Q_ASSERT_X(
false,
"QVariant::save",
"Invalid type to save");
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1407
1408
1409
1410
1411
1412QDataStream &operator<<(QDataStream &s,
const QVariant &p)
1419
1420
1421
1422
1423
1426
1427
1428
1429
1430
1434
1435
1436
1437
1438
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450QStringList QVariant::toStringList()
const
1452 return qvariant_cast<QStringList>(*
this);
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470QString QVariant::toString()
const
1472 return qvariant_cast<QString>(*
this);
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486QVariantMap QVariant::toMap()
const
1488 return qvariant_cast<QVariantMap>(*
this);
1492
1493
1494
1495
1496
1497QVariantHash QVariant::toHash()
const
1499 return qvariant_cast<QVariantHash>(*
this);
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514QDate QVariant::toDate()
const
1516 return qvariant_cast<QDate>(*
this);
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531QTime QVariant::toTime()
const
1533 return qvariant_cast<QTime>(*
this);
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548QDateTime QVariant::toDateTime()
const
1550 return qvariant_cast<QDateTime>(*
this);
1554
1555
1556
1557
1558
1559
1560
1561
1562#if QT_CONFIG(easingcurve)
1563QEasingCurve QVariant::toEasingCurve()
const
1565 return qvariant_cast<QEasingCurve>(*
this);
1570
1571
1572
1573
1574
1575
1576
1577
1578QByteArray QVariant::toByteArray()
const
1580 return qvariant_cast<QByteArray>(*
this);
1583#ifndef QT_NO_GEOM_VARIANT
1585
1586
1587
1588
1589
1590
1591
1592
1593QPoint QVariant::toPoint()
const
1595 return qvariant_cast<QPoint>(*
this);
1599
1600
1601
1602
1603
1604
1605
1606QRect QVariant::toRect()
const
1608 return qvariant_cast<QRect>(*
this);
1612
1613
1614
1615
1616
1617
1618
1619QSize QVariant::toSize()
const
1621 return qvariant_cast<QSize>(*
this);
1625
1626
1627
1628
1629
1630
1631
1632QSizeF QVariant::toSizeF()
const
1634 return qvariant_cast<QSizeF>(*
this);
1638
1639
1640
1641
1642
1643
1644
1645
1646QRectF QVariant::toRectF()
const
1648 return qvariant_cast<QRectF>(*
this);
1652
1653
1654
1655
1656
1657
1658
1659QLineF QVariant::toLineF()
const
1661 return qvariant_cast<QLineF>(*
this);
1665
1666
1667
1668
1669
1670
1671
1672QLine QVariant::toLine()
const
1674 return qvariant_cast<QLine>(*
this);
1678
1679
1680
1681
1682
1683
1684
1685
1686QPointF QVariant::toPointF()
const
1688 return qvariant_cast<QPointF>(*
this);
1693#ifndef QT_BOOTSTRAPPED
1695
1696
1697
1698
1699
1700
1701
1702QUrl QVariant::toUrl()
const
1704 return qvariant_cast<QUrl>(*
this);
1709
1710
1711
1712
1713
1714
1715
1716QLocale QVariant::toLocale()
const
1718 return qvariant_cast<QLocale>(*
this);
1721#if QT_CONFIG(regularexpression)
1723
1724
1725
1726
1727
1728
1729
1730
1731QRegularExpression QVariant::toRegularExpression()
const
1733 return qvariant_cast<QRegularExpression>(*
this);
1737#if QT_CONFIG(itemmodel)
1739
1740
1741
1742
1743
1744
1745
1746QModelIndex QVariant::toModelIndex()
const
1748 return qvariant_cast<QModelIndex>(*
this);
1752
1753
1754
1755
1756
1757
1758
1759QPersistentModelIndex QVariant::toPersistentModelIndex()
const
1761 return qvariant_cast<QPersistentModelIndex>(*
this);
1766
1767
1768
1769
1770
1771
1772
1773
1774QUuid QVariant::toUuid()
const
1776 return qvariant_cast<QUuid>(*
this);
1779#ifndef QT_BOOTSTRAPPED
1781
1782
1783
1784
1785
1786
1787
1788QJsonValue QVariant::toJsonValue()
const
1790 return qvariant_cast<QJsonValue>(*
this);
1794
1795
1796
1797
1798
1799
1800
1801QJsonObject QVariant::toJsonObject()
const
1803 return qvariant_cast<QJsonObject>(*
this);
1807
1808
1809
1810
1811
1812
1813
1814QJsonArray QVariant::toJsonArray()
const
1816 return qvariant_cast<QJsonArray>(*
this);
1820
1821
1822
1823
1824
1825
1826
1827QJsonDocument QVariant::toJsonDocument()
const
1829 return qvariant_cast<QJsonDocument>(*
this);
1834
1835
1836
1837
1838
1839
1840
1841
1842QChar QVariant::toChar()
const
1844 return qvariant_cast<QChar>(*
this);
1847#ifndef QT_BOOTSTRAPPED
1849
1850
1851
1852
1853
1854QBitArray QVariant::toBitArray()
const
1856 return qvariant_cast<QBitArray>(*
this);
1860template <
typename T>
1871 bool success = QMetaType::convert(d.type(), d.storage(), t, &ret);
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894int QVariant::toInt(
bool *ok)
const
1896 return qNumVariantToHelper<
int>(d, ok);
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916uint QVariant::toUInt(
bool *ok)
const
1918 return qNumVariantToHelper<uint>(d, ok);
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933qlonglong QVariant::toLongLong(
bool *ok)
const
1935 return qNumVariantToHelper<qlonglong>(d, ok);
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950qulonglong QVariant::toULongLong(
bool *ok)
const
1952 return qNumVariantToHelper<qulonglong>(d, ok);
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967bool QVariant::toBool()
const
1969 auto boolType = QMetaType::fromType<
bool>();
1970 if (d.type() == boolType)
1971 return d.get<
bool>();
1974 QMetaType::convert(d.type(), constData(), boolType, &res);
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990double QVariant::toDouble(
bool *ok)
const
1992 return qNumVariantToHelper<
double>(d, ok);
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009float QVariant::toFloat(
bool *ok)
const
2011 return qNumVariantToHelper<
float>(d, ok);
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028qreal QVariant::toReal(
bool *ok)
const
2030 return qNumVariantToHelper<qreal>(d, ok);
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044QVariantList QVariant::toList()
const
2046 return qvariant_cast<QVariantList>(*
this);
2050
2051
2052
2053
2054
2055
2058
2059
2060
2061
2062
2063
2064
2065
2066
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2114bool QVariant::convert(QMetaType targetType)
2116 if (d.type() == targetType)
2117 return targetType.isValid();
2119 QVariant oldValue = *
this;
2122 create(targetType,
nullptr);
2123 if (!oldValue.canConvert(targetType))
2127 if (oldValue.d.is_null && oldValue.d.type().id() != QMetaType::Nullptr)
2130 bool ok = QMetaType::convert(oldValue.d.type(), oldValue.constData(), targetType, data());
2136
2137
2138
2139
2140bool QVariant::convert(
int type,
void *ptr)
const
2142 return QMetaType::convert(d.type(), constData(), QMetaType(type), ptr);
2146
2147
2148bool QVariant::view(
int type,
void *ptr)
2150 return QMetaType::view(d.type(), data(), QMetaType(type), ptr);
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2203 static const qulonglong numericTypeBits =
2204 Q_UINT64_C(1) << QMetaType::QString |
2205 Q_UINT64_C(1) << QMetaType::Bool |
2206 Q_UINT64_C(1) << QMetaType::Double |
2207 Q_UINT64_C(1) << QMetaType::Float |
2208 Q_UINT64_C(1) << QMetaType::Char |
2209 Q_UINT64_C(1) << QMetaType::Char16 |
2210 Q_UINT64_C(1) << QMetaType::Char32 |
2211 Q_UINT64_C(1) << QMetaType::SChar |
2212 Q_UINT64_C(1) << QMetaType::UChar |
2213 Q_UINT64_C(1) << QMetaType::Short |
2214 Q_UINT64_C(1) << QMetaType::UShort |
2215 Q_UINT64_C(1) << QMetaType::Int |
2216 Q_UINT64_C(1) << QMetaType::UInt |
2217 Q_UINT64_C(1) << QMetaType::Long |
2218 Q_UINT64_C(1) << QMetaType::ULong |
2219 Q_UINT64_C(1) << QMetaType::LongLong |
2220 Q_UINT64_C(1) << QMetaType::ULongLong;
2221 return tp < (CHAR_BIT *
sizeof numericTypeBits) ? numericTypeBits & (Q_UINT64_C(1) << tp) :
false;
2226 return tp == QMetaType::Double || tp == QMetaType::Float || tp == QMetaType::Float16;
2232 if (!iface1 || !iface2)
2237 bool isNumeric1 = qIsNumericType(iface1->typeId);
2238 bool isNumeric2 = qIsNumericType(iface2->typeId);
2241 if (isNumeric1 && isNumeric2)
2244 bool isEnum1 = iface1->flags & QMetaType::IsEnumeration;
2245 bool isEnum2 = iface2->flags & QMetaType::IsEnumeration;
2250 if (isEnum1 && isEnum2)
2251 return QMetaType(iface1) == QMetaType(iface2);
2254 if (isEnum1 && isNumeric2)
2256 if (isNumeric1 && isEnum2)
2270 uint t1 = iface1->typeId;
2271 uint t2 = iface2->typeId;
2273 if ((t1 == QMetaType::Bool && t2 == QMetaType::QString) ||
2274 (t2 == QMetaType::Bool && t1 == QMetaType::QString))
2275 return QMetaType::Bool;
2292 if (qIsFloatingPoint(t1) || qIsFloatingPoint(t2))
2293 return QMetaType::QReal;
2295 auto isUnsigned = [](uint tp) {
2297 return tp == QMetaType::ULongLong || tp == QMetaType::ULong ||
2298 tp == QMetaType::UInt || tp == QMetaType::Char32;
2300 bool isUnsigned1 = isUnsigned(t1);
2301 bool isUnsigned2 = isUnsigned(t2);
2305 if (isUnsigned1 && iface1->size >
sizeof(
int))
2306 return QMetaType::ULongLong;
2307 if (isUnsigned2 && iface2->size >
sizeof(
int))
2308 return QMetaType::ULongLong;
2311 if (iface1->size >
sizeof(
int) || iface2->size >
sizeof(
int))
2312 return QMetaType::LongLong;
2315 if (isUnsigned1 || isUnsigned2)
2316 return QMetaType::UInt;
2319 return QMetaType::Int;
2325 return QPartialOrdering::Equivalent;
2326 if constexpr (
std::numeric_limits<Numeric>::has_quiet_NaN) {
2327 if (std::isnan(lhs) || std::isnan(rhs))
2328 return QPartialOrdering::Unordered;
2332 if constexpr (
std::is_same_v<Numeric, QObject *>)
2333 smaller = std::less<QObject *>()(lhs, rhs);
2335 smaller = lhs < rhs;
2336 return smaller ? QPartialOrdering::Less : QPartialOrdering::Greater;
2342 std::optional<qlonglong> l1 = qConvertToNumber(d1, promotedType == QMetaType::Bool);
2343 std::optional<qlonglong> l2 = qConvertToNumber(d2, promotedType == QMetaType::Bool);
2345 return QPartialOrdering::Unordered;
2346 if (promotedType == QMetaType::UInt)
2347 return spaceShip<uint>(*l1, *l2);
2348 if (promotedType == QMetaType::LongLong)
2349 return spaceShip<qlonglong>(*l1, *l2);
2350 if (promotedType == QMetaType::ULongLong)
2351 return spaceShip<qulonglong>(*l1, *l2);
2353 return spaceShip<
int>(*l1, *l2);
2358 uint promotedType = numericTypePromotion(d1->typeInterface(), d2->typeInterface());
2359 if (promotedType != QMetaType::QReal)
2360 return integralCompare(promotedType, d1, d2);
2363 const auto r1 = qConvertToRealNumber(d1);
2364 const auto r2 = qConvertToRealNumber(d2);
2366 return QPartialOrdering::Unordered;
2368 return QPartialOrdering::Equivalent;
2370 return spaceShip(*r1, *r2);
2373#ifndef QT_BOOTSTRAPPED
2376 if ((fromType.flags() & QMetaType::PointerToQObject)
2377 && (toType.flags() & QMetaType::PointerToQObject)) {
2378 const QMetaObject *f = fromType.metaObject();
2379 const QMetaObject *t = toType.metaObject();
2380 return f && t && (f->inherits(t) || t->inherits(f));
2387 return spaceShip<QObject *>(d1->get<QObject *>(), d2->get<QObject *>());
2392
2393
2394bool QVariant::equals(
const QVariant &v)
const
2396 auto metatype = d.type();
2398 if (metatype != v.metaType()) {
2400 if (canBeNumericallyCompared(metatype.iface(), v.d.type().iface()))
2401 return numericCompare(&d, &v.d) == QPartialOrdering::Equivalent;
2402#ifndef QT_BOOTSTRAPPED
2404 if (qvCanConvertMetaObject(metatype, v.metaType()))
2405 return pointerCompare(&d, &v.d) == QPartialOrdering::Equivalent;
2411 if (!metatype.isValid())
2414 return metatype.equals(d.storage(), v.d.storage());
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439QPartialOrdering QVariant::compare(
const QVariant &lhs,
const QVariant &rhs)
2441 QMetaType t = lhs.d.type();
2442 if (t != rhs.d.type()) {
2444 if (canBeNumericallyCompared(lhs.d.type().iface(), rhs.d.type().iface()))
2445 return numericCompare(&lhs.d, &rhs.d);
2446#ifndef QT_BOOTSTRAPPED
2447 if (qvCanConvertMetaObject(lhs.metaType(), rhs.metaType()))
2448 return pointerCompare(&lhs.d, &rhs.d);
2450 return QPartialOrdering::Unordered;
2452 return t.compare(lhs.constData(), rhs.constData());
2456
2457
2458
2459
2460
2461
2462
2463
2466
2467
2468
2469
2470
2471
2472
2473
2474void *QVariant::data()
2479 return const_cast<
void *>(constData());
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529bool QVariant::isNull()
const
2531 if (d.is_null || !metaType().isValid())
2533 if (metaType().flags() & QMetaType::IsPointer)
2534 return d.get<
void *>() ==
nullptr;
2538#ifndef QT_NO_DEBUG_STREAM
2539QDebug QVariant::qdebugHelper(QDebug dbg)
const
2541 QDebugStateSaver saver(dbg);
2542 const uint typeId = d.type().id();
2543 dbg.nospace() <<
"QVariant(";
2544 if (typeId != QMetaType::UnknownType) {
2545 dbg << d.type().name() <<
", ";
2546 bool streamed = d.type().debugStream(dbg, d.storage());
2547 if (!streamed && canConvert<QString>())
2556QVariant QVariant::moveConstruct(QMetaType type,
void *data)
2559 var.d = QVariant::Private(type.d_ptr);
2560 customConstruct<ForceMove, NonNull>(type.d_ptr, &var.d, data);
2564QVariant QVariant::copyConstruct(QMetaType type,
const void *data)
2567 var.d = QVariant::Private(type.d_ptr);
2568 customConstruct<UseCopy, NonNull>(type.d_ptr, &var.d, data);
2572#if QT_DEPRECATED_SINCE(6
, 0
)
2574QT_WARNING_DISABLE_DEPRECATED
2576QDebug operator<<(QDebug dbg,
const QVariant::Type p)
2578 QDebugStateSaver saver(dbg);
2579 dbg.nospace() <<
"QVariant::"
2580 << (
int(p) !=
int(QMetaType::UnknownType)
2581 ? QMetaType(p).name()
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2605
2606
2607
2608
2611
2612
2613
2614
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2673
2674
2675
2676
2677
2678
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2693
2694
2695
2696
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2712
2713
2714
2715
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740QVariant QVariant::fromMetaType(QMetaType type,
const void *copy)
2743 type.registerType();
2744 const auto iface = type.iface();
2745 if (isValidMetaTypeForVariant(iface, copy)) {
2746 result.d = Private(iface);
2747 customConstruct(iface, &result.d, copy);
2753
2754
2755
2756
2757
2758
2759
2760
2761
2764
2765
2766
2767
2768
2769
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2804
2805
2806
2807
2808
2811
2812
2813
2814
2815
2818
2819
2820
2821
2822
2823
2826
2827
2828
2830
2831
2834
2835
2838
2839
2842
2843
2846
2847
2850
2851
2854
2855
2858
2859
2862
2863
2866
2867
2868
2871
2872
2873
2876
2877
2878const void *QtPrivate::QVariantTypeCoercer::convert(
const QVariant &value,
const QMetaType &type)
2880 if (type == QMetaType::fromType<QVariant>())
2883 if (type == value.metaType())
2884 return value.constData();
2886 if (value.canConvert(type)) {
2888 if (converted.convert(type))
2889 return converted.constData();
2896
2897
2898const void *QtPrivate::QVariantTypeCoercer::coerce(
const QVariant &value,
const QMetaType &type)
2900 if (
const void *result = convert(value, type))
2903 converted = QVariant(type);
2904 return converted.constData();
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2922
2923
2924
2925
2928
2929
2930
2931
2932
2935
2936
2937
2938
2939
2942
2943
2944
2945
2946
2949
2950
2951
2952
2955
2956
2957
2958
2959
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2974
2975
2976QVariantConstPointer::QVariantConstPointer(QVariant variant)
2977 : m_variant(std::move(variant))
2982
2983
2984QVariant QVariantConstPointer::operator*()
const
2990
2991
2992
2993const QVariant *QVariantConstPointer::operator->()
const
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3011
3012
3013
3014
3017
3018
3019
3020
3023
3024
3025
3026
3027
QDataStream & operator>>(QDataStream &s, QVariant &p)
\keyword 16-bit Floating Point Support\inmodule QtCore \inheaderfile QFloat16
constexpr int Qt6ToQt5GuiTypeDelta
static QPartialOrdering spaceShip(Numeric lhs, Numeric rhs)
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)