14#if QT_CONFIG(easingcurve)
18#if QT_CONFIG(regularexpression)
26#if QT_CONFIG(itemmodel)
29#ifndef QT_BOOTSTRAPPED
39#include "private/qlocale_p.h"
43#ifndef QT_NO_GEOM_VARIANT
64 switch (
d->typeInterface()->size) {
66 return d->get<
signed char>();
68 return d->get<
short>();
74 Q_UNREACHABLE_RETURN(0);
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:
89 case QMetaType::Double:
91#ifndef QT_BOOTSTRAPPED
92 case QMetaType::QJsonValue:
94 case QMetaType::QCborValue:
98 Q_UNREACHABLE_RETURN(0);
103 switch (
d->typeInterface()->size) {
105 return d->get<
unsigned char>();
107 return d->get<
unsigned short>();
109 return d->get<
unsigned int>();
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: {
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:
138 case QMetaType::Bool:
140#ifndef QT_BOOTSTRAPPED
141 case QMetaType::QCborValue:
144 return qMetaTypeNumber(
d);
145 case QMetaType::QJsonValue:
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:
170 ||
d->typeInterface()->typeId == QMetaType::QCborSimpleType)
171 return qMetaTypeNumberBySize(
d);
179 switch (
d->typeInterface()->typeId) {
180 case QMetaType::QString:
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:
201 case QMetaType::QJsonValue:
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 {
258 std::unique_ptr<QVariant::PrivateShared, Deleter>
ptr;
261 return ptr.release();
265template <CustomConstructMoveOptions moveOption = UseCopy, CustomConstructNullabilityOption nullability = MaybeNull>
267 std::conditional_t<moveOption == ForceMove, void *, const void *>
copy)
272 Q_ASSERT(!isInterfaceFor<void>(iface));
276 if constexpr (moveOption == ForceMove)
278 if constexpr (nullability == NonNull)
279 Q_ASSUME(
copy !=
nullptr);
284 d->is_null = !
copy QT6_ONLY(|| isInterfaceFor<std::nullptr_t>(iface));
287 d->is_shared =
false;
290 if constexpr (moveOption == ForceMove && nullability == NonNull)
296 if constexpr (moveOption == ForceMove && nullability == NonNull)
297 moveConstruct(iface, where, copy);
299 construct(iface, where, copy);
322 d.data.shared->ref.ref();
558 :
d(clonePrivate(
p.
d))
635 auto typeFits = [&] {
636 auto newIface =
type.iface();
640 return newSize <= oldSize;
649 const auto ps =
d.
data.shared;
650 const auto align =
type.alignOf();
659 return const_cast<void *
>(
d.
storage());
946 if (isValidMetaTypeForVariant(
type.iface(),
copy))
976#if QT_CONFIG(easingcurve)
979#ifndef QT_NO_GEOM_VARIANT
981 :
d(std::piecewise_construct_t{}, pt) {}
983 :
d(std::piecewise_construct_t{}, pt) {}
985 :
d(std::piecewise_construct_t{},
r) {}
987 :
d(std::piecewise_construct_t{},
r) {}
989 :
d(std::piecewise_construct_t{}, l) {}
991 :
d(std::piecewise_construct_t{}, l) {}
993 :
d(std::piecewise_construct_t{},
s) {}
995 :
d(std::piecewise_construct_t{},
s) {}
997#ifndef QT_BOOTSTRAPPED
1001#if QT_CONFIG(regularexpression)
1005#ifndef QT_BOOTSTRAPPED
1007 :
d(std::piecewise_construct_t{}, jsonValue)
1008{
static_assert(
sizeof(CborValueStandIn) ==
sizeof(
QJsonValue)); }
1013#if QT_CONFIG(itemmodel)
1015 :
d(std::piecewise_construct_t{}, modelIndex) {}
1110 if (!
d.
data.shared->ref.deref())
1164#ifndef QT_NO_DATASTREAM
1169 QMetaType::QVariantMap,
1170 QMetaType::QVariantList,
1172 QMetaType::QStringList,
1179 QMetaType::QPalette,
1189 QMetaType::QPolygon,
1193 QMetaType::QSizePolicy,
1196 QMetaType::QDateTime,
1197 QMetaType::QByteArray,
1198 QMetaType::QBitArray,
1199#if QT_CONFIG(shortcut)
1200 QMetaType::QKeySequence,
1205 QMetaType::LongLong,
1206 QMetaType::ULongLong,
1207#if QT_CONFIG(easingcurve)
1208 QMetaType::QEasingCurve
1249 }
else if (
typeId == 75 ) {
1264 typeId = QMetaType::QSizePolicy;
1270 qint8 is_null =
false;
1279 qWarning(
"QVariant::load: unknown user type with name %s.",
name.constData());
1313 bool saveAsUserType =
false;
1316 saveAsUserType =
true;
1322 if (!strcmp(
d.
type().
name(),
"QRegExp")) {
1328 saveAsUserType =
true;
1333 saveAsUserType =
true;
1335 }
else if (
typeId == QMetaType::QSizePolicy) {
1354 saveAsUserType =
true;
1367 saveAsUserType =
true;
1371 if (saveAsUserType) {
1390 qWarning(
"QVariant::save: unable to save type '%s' (type id: %d).\n",
1392 Q_ASSERT_X(
false,
"QVariant::save",
"Invalid type to save");
1467 return qvariant_cast<QStringList>(*
this);
1487 return qvariant_cast<QString>(*
this);
1503 return qvariant_cast<QVariantMap>(*
this);
1514 return qvariant_cast<QVariantHash>(*
this);
1531 return qvariant_cast<QDate>(*
this);
1548 return qvariant_cast<QTime>(*
this);
1565 return qvariant_cast<QDateTime>(*
this);
1577#if QT_CONFIG(easingcurve)
1580 return qvariant_cast<QEasingCurve>(*
this);
1595 return qvariant_cast<QByteArray>(*
this);
1598#ifndef QT_NO_GEOM_VARIANT
1610 return qvariant_cast<QPoint>(*
this);
1623 return qvariant_cast<QRect>(*
this);
1636 return qvariant_cast<QSize>(*
this);
1649 return qvariant_cast<QSizeF>(*
this);
1663 return qvariant_cast<QRectF>(*
this);
1676 return qvariant_cast<QLineF>(*
this);
1689 return qvariant_cast<QLine>(*
this);
1703 return qvariant_cast<QPointF>(*
this);
1708#ifndef QT_BOOTSTRAPPED
1719 return qvariant_cast<QUrl>(*
this);
1733 return qvariant_cast<QLocale>(*
this);
1736#if QT_CONFIG(regularexpression)
1748 return qvariant_cast<QRegularExpression>(*
this);
1752#if QT_CONFIG(itemmodel)
1763 return qvariant_cast<QModelIndex>(*
this);
1776 return qvariant_cast<QPersistentModelIndex>(*
this);
1791 return qvariant_cast<QUuid>(*
this);
1794#ifndef QT_BOOTSTRAPPED
1805 return qvariant_cast<QJsonValue>(*
this);
1818 return qvariant_cast<QJsonObject>(*
this);
1831 return qvariant_cast<QJsonArray>(*
this);
1844 return qvariant_cast<QJsonDocument>(*
this);
1859 return qvariant_cast<QChar>(*
this);
1870 return qvariant_cast<QBitArray>(*
this);
1873template <
typename T>
1909 return qNumVariantToHelper<int>(
d,
ok);
1931 return qNumVariantToHelper<uint>(
d,
ok);
1948 return qNumVariantToHelper<qlonglong>(
d,
ok);
1965 return qNumVariantToHelper<qulonglong>(
d,
ok);
1982 auto boolType = QMetaType::fromType<bool>();
1983 if (
d.
type() == boolType)
1984 return d.
get<
bool>();
2005 return qNumVariantToHelper<double>(
d,
ok);
2024 return qNumVariantToHelper<float>(
d,
ok);
2043 return qNumVariantToHelper<qreal>(
d,
ok);
2059 return qvariant_cast<QVariantList>(*
this);
2129 if (
d.
type() == targetType)
2135 create(targetType,
nullptr);
2140 if (oldValue.
d.
is_null && oldValue.
d.
type().
id() != QMetaType::Nullptr)
2234 return tp < (CHAR_BIT *
sizeof numericTypeBits) ? numericTypeBits & (
Q_UINT64_C(1) << tp) :
false;
2239 return tp == QMetaType::Double || tp == QMetaType::Float || tp == QMetaType::Float16;
2245 if (!iface1 || !iface2)
2254 if (isNumeric1 && isNumeric2)
2263 if (isEnum1 && isEnum2)
2267 if (isEnum1 && isNumeric2)
2269 if (isNumeric1 && isEnum2)
2286 if ((
t1 == QMetaType::Bool &&
t2 == QMetaType::QString) ||
2287 (
t2 == QMetaType::Bool &&
t1 == QMetaType::QString))
2288 return QMetaType::Bool;
2308 auto isUnsigned = [](
uint tp) {
2310 return tp == QMetaType::ULongLong || tp == QMetaType::ULong ||
2311 tp == QMetaType::UInt || tp == QMetaType::Char32;
2313 bool isUnsigned1 = isUnsigned(
t1);
2314 bool isUnsigned2 = isUnsigned(
t2);
2318 if (isUnsigned1 && iface1->
size >
sizeof(
int))
2319 return QMetaType::ULongLong;
2320 if (isUnsigned2 && iface2->
size >
sizeof(
int))
2321 return QMetaType::ULongLong;
2324 if (iface1->
size >
sizeof(
int) || iface2->
size >
sizeof(
int))
2325 return QMetaType::LongLong;
2328 if (isUnsigned1 || isUnsigned2)
2329 return QMetaType::UInt;
2332 return QMetaType::Int;
2339 if constexpr (std::numeric_limits<Numeric>::has_quiet_NaN) {
2340 if (std::isnan(lhs) || std::isnan(rhs))
2345 if constexpr (std::is_same_v<Numeric, QObject *>)
2346 smaller = std::less<QObject *>()(lhs, rhs);
2348 smaller = lhs < rhs;
2355 std::optional<qlonglong> l1 = qConvertToNumber(
d1, promotedType == QMetaType::Bool);
2356 std::optional<qlonglong> l2 = qConvertToNumber(
d2, promotedType == QMetaType::Bool);
2359 if (promotedType == QMetaType::UInt)
2360 return spaceShip<uint>(*l1, *l2);
2361 if (promotedType == QMetaType::LongLong)
2362 return spaceShip<qlonglong>(*l1, *l2);
2363 if (promotedType == QMetaType::ULongLong)
2364 return spaceShip<qulonglong>(*l1, *l2);
2366 return spaceShip<int>(*l1, *l2);
2376 const auto r1 = qConvertToRealNumber(
d1);
2377 const auto r2 = qConvertToRealNumber(
d2);
2386#ifndef QT_BOOTSTRAPPED
2393 return f &&
t && (
f->inherits(
t) ||
t->inherits(
f));
2409 auto metatype =
d.
type();
2411 if (metatype !=
v.metaType()) {
2415#ifndef QT_BOOTSTRAPPED
2424 if (!metatype.isValid())
2427 return metatype.equals(
d.
storage(),
v.d.storage());
2459#ifndef QT_BOOTSTRAPPED
2547 return d.
get<
void *>() ==
nullptr;
2551#ifndef QT_NO_DEBUG_STREAM
2556 dbg.nospace() <<
"QVariant(";
2573 customConstruct<ForceMove, NonNull>(
type.d_ptr, &
var.
d,
data);
2581 customConstruct<UseCopy, NonNull>(
type.d_ptr, &
var.
d,
data);
2585#if QT_DEPRECATED_SINCE(6, 0)
2592 dbg.nospace() <<
"QVariant::"
2850 if (
type == QMetaType::fromType<QVariant>())
2854 return value.constData();
2874 return converted.constData();
qlonglong toLongLong(bool *ok=nullptr, int base=10) const
Returns the byte array converted to a {long long} using base base, which is ten by default.
\inmodule QtCore\reentrant
bool isDouble() const
Returns true if this QCborValue is of the floating-point type.
bool isInteger() const
Returns true if this QCborValue is of the integer type.
\inmodule QtCore\reentrant
\inmodule QtCore\reentrant
\inmodule QtCore \reentrant
\inmodule QtCore\reentrant
\inmodule QtCore\reentrant
\inmodule QtCore\reentrant
\inmodule QtCore\reentrant
bool isDouble() const
Returns true if the value contains a double.
static const QPartialOrdering Less
static const QPartialOrdering Greater
static const QPartialOrdering Equivalent
static const QPartialOrdering Unordered
\inmodule QtCore\reentrant
\inmodule QtCore\reentrant
\inmodule QtCore\reentrant
\inmodule QtCore\reentrant
\inmodule QtCore \reentrant
\macro QT_RESTRICTED_CAST_FROM_ASCII
double toDouble(bool *ok=nullptr) const
Returns the string converted to a double value.
\inmodule QtCore \reentrant
QVariantConstPointer(QVariant variant)
Constructs a QVariantConstPointer from a variant.
const QVariant * operator->() const
Returns a const pointer to the QVariant, conforming to the conventions for operator->().
QVariant operator*() const
Dereferences the QVariantConstPointer to retrieve its internal QVariant.
QChar toChar() const
Returns the variant as a QChar if the variant has userType() \l QMetaType::QChar, \l QMetaType::Int,...
void * data()
Returns a pointer to the contained object as a generic void* that can be written to.
bool convert(QMetaType type)
Casts the variant to the requested type, targetType.
QVariant() noexcept
Constructs an invalid variant.
QDataStream & operator<<(QDataStream &s, const QVariant &p)
Writes a variant p to the stream s.
void clear()
Convert this variant to type QMetaType::UnknownType and free up any resources used.
QPointF toPointF() const
Returns the variant as a QPointF if the variant has userType() \l QMetaType::QPoint or \l QMetaType::...
T view()
Returns a mutable view of template type {T} on the stored value.
static QPartialOrdering compare(const QVariant &lhs, const QVariant &rhs)
Compares the objects at lhs and rhs for ordering.
qreal toReal(bool *ok=nullptr) const
Returns the variant as a qreal if the variant has userType() \l QMetaType::Double,...
QDateTime toDateTime() const
Returns the variant as a QDateTime if the variant has userType() \l QMetaType::QDateTime,...
void swap(QVariant &other) noexcept
bool isValid() const
Returns true if the storage type of this variant is not QMetaType::UnknownType; otherwise returns fal...
QJsonValue toJsonValue() const
double toDouble(bool *ok=nullptr) const
Returns the variant as a double if the variant has userType() \l QMetaType::Double,...
QList< QVariant > toList() const
Returns the variant as a QVariantList if the variant has userType() \l QMetaType::QVariantList.
QMap< QString, QVariant > toMap() const
Returns the variant as a QVariantMap if the variant has type() \l QMetaType::QVariantMap.
qlonglong toLongLong(bool *ok=nullptr) const
Returns the variant as a long long int if the variant has userType() \l QMetaType::LongLong,...
QPoint toPoint() const
Returns the variant as a QPoint if the variant has userType() \l QMetaType::QPoint or \l QMetaType::Q...
int toInt(bool *ok=nullptr) const
Returns the variant as an int if the variant has userType() \l QMetaType::Int, \l QMetaType::Bool,...
QLocale toLocale() const
Returns the variant as a QLocale if the variant has userType() \l QMetaType::QLocale; otherwise retur...
QSize toSize() const
Returns the variant as a QSize if the variant has userType() \l QMetaType::QSize; otherwise returns a...
void save(QDataStream &ds) const
Internal function for saving a variant to the stream s.
QTime toTime() const
Returns the variant as a QTime if the variant has userType() \l QMetaType::QTime, \l QMetaType::QDate...
float toFloat(bool *ok=nullptr) const
Returns the variant as a float if the variant has userType() \l QMetaType::Double,...
uint toUInt(bool *ok=nullptr) const
Returns the variant as an unsigned int if the variant has userType() \l QMetaType::UInt,...
QString toString() const
Returns the variant as a QString if the variant has a userType() including, but not limited to:
void load(QDataStream &ds)
Internal function for loading a variant from stream s.
bool toBool() const
Returns the variant as a bool if the variant has userType() Bool.
QBitArray toBitArray() const
Returns the variant as a QBitArray if the variant has userType() \l QMetaType::QBitArray; otherwise r...
QJsonArray toJsonArray() const
QHash< QString, QVariant > toHash() const
Returns the variant as a QHash<QString, QVariant> if the variant has type() \l QMetaType::QVariantHas...
int typeId() const
Returns the storage type of the value stored in the variant.
bool isNull() const
Returns true if this is a null variant, false otherwise.
const char * typeName() const
Returns the name of the type stored in the variant.
QJsonDocument toJsonDocument() const
QRectF toRectF() const
Returns the variant as a QRectF if the variant has userType() \l QMetaType::QRect or \l QMetaType::QR...
QLineF toLineF() const
Returns the variant as a QLineF if the variant has userType() \l QMetaType::QLineF; otherwise returns...
QVariant & operator=(const QVariant &other)
Move-assigns other to this QVariant instance.
QJsonObject toJsonObject() const
bool canConvert(QMetaType targetType) const
QRect toRect() const
Returns the variant as a QRect if the variant has userType() \l QMetaType::QRect; otherwise returns a...
QSizeF toSizeF() const
Returns the variant as a QSizeF if the variant has userType() \l QMetaType::QSizeF; otherwise returns...
QDate toDate() const
Returns the variant as a QDate if the variant has userType() \l QMetaType::QDate, \l QMetaType::QDate...
QByteArray toByteArray() const
Returns the variant as a QByteArray if the variant has userType() \l QMetaType::QByteArray or \l QMet...
QLine toLine() const
Returns the variant as a QLine if the variant has userType() \l QMetaType::QLine; otherwise returns a...
~QVariant()
Destroys the QVariant and the contained object.
QDataStream & operator>>(QDataStream &s, QVariant &p)
void create(int type, const void *copy)
QMetaType metaType() const
qulonglong toULongLong(bool *ok=nullptr) const
Returns the variant as an unsigned long long int if the variant has type() \l QMetaType::ULongLong,...
const void * constData() const
QStringList toStringList() const
Returns the variant as a QStringList if the variant has userType() \l QMetaType::QStringList,...
QUrl toUrl() const
Returns the variant as a QUrl if the variant has userType() \l QMetaType::QUrl; otherwise returns an ...
bool equals(const QVariant &other) const
const void * coerce(const QVariant &value, const QMetaType &type)
const void * convert(const QVariant &value, const QMetaType &type)
\keyword 16-bit Floating Point Support\inmodule QtCore \inheaderfile QFloat16
QHash< int, QWidget * > hash
[35multi]
QMap< QString, QString > map
[6]
Combined button and popup list for selecting options.
constexpr QBindableInterface iface
static jboolean copy(JNIEnv *, jobject)
#define QT_WARNING_DISABLE_DEPRECATED
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
qint64 qRound64(qfloat16 d) noexcept
static ControlElement< T > * ptr(QWidget *widget)
GLsizei const GLfloat * v
[13]
GLint GLint GLint GLint GLint x
[0]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
[4]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
#define Q_ASSERT_X(cond, x, msg)
QT_BEGIN_NAMESPACE class QT6_ONLY(Q_CORE_EXPORT) QReadWriteLock
constexpr std::underlying_type_t< Enum > qToUnderlying(Enum e) noexcept
QT_BEGIN_NAMESPACE typedef signed char qint8
static double toDouble(Value v)
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 const ushort mapIdFromQt3ToCurrent[MapFromThreeCount]
static QPartialOrdering integralCompare(uint promotedType, const QVariant::Private *d1, const QVariant::Private *d2)
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)
static QT_BEGIN_NAMESPACE QVariant::PrivateShared * customConstructShared(size_t size, size_t align, F &&construct)
v canConvert< QString >()
static void free(PrivateShared *p)
static PrivateShared * create(size_t size, size_t align)
static size_t computeAllocationSize(size_t size, size_t align)
static int computeOffset(PrivateShared *ps, size_t align)
static constexpr bool canUseInternalSpace(const QtPrivate::QMetaTypeInterface *type)
const void * storage() const
const QtPrivate::QMetaTypeInterface * typeInterface() const
uchar data[MaxInternalSize]