summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qvariant.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/kernel/qvariant.cpp')
-rw-r--r--src/corelib/kernel/qvariant.cpp63
1 files changed, 49 insertions, 14 deletions
diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp
index 54b4f67..d5b2d16 100644
--- a/src/corelib/kernel/qvariant.cpp
+++ b/src/corelib/kernel/qvariant.cpp
@@ -685,6 +685,7 @@ static bool convert(const QVariant::Private *d, QVariant::Type t, void *result,
break;
case QVariant::Url:
*str = v_cast<QUrl>(d)->toString();
+ break;
default:
return false;
}
@@ -962,10 +963,10 @@ static bool convert(const QVariant::Private *d, QVariant::Type t, void *result,
float *f = static_cast<float *>(result);
switch (d->type) {
case QVariant::String:
- *f = float(v_cast<QString>(d)->toDouble(ok));
+ *f = v_cast<QString>(d)->toFloat(ok);
break;
case QVariant::ByteArray:
- *f = float(v_cast<QByteArray>(d)->toDouble(ok));
+ *f = v_cast<QByteArray>(d)->toFloat(ok);
break;
case QVariant::Bool:
*f = float(d->data.b);
@@ -1080,7 +1081,7 @@ static void streamDebug(QDebug dbg, const QVariant &v)
dbg.nospace() << v.toULongLong();
break;
case QMetaType::Float:
- dbg.nospace() << qVariantValue<float>(v);
+ dbg.nospace() << v.toFloat();
break;
case QMetaType::QObjectStar:
dbg.nospace() << qVariantValue<QObject *>(v);
@@ -1190,9 +1191,8 @@ const QVariant::Handler *QVariant::handler = &qt_kernel_variant_handler;
\brief The QVariant class acts like a union for the most common Qt data types.
\ingroup objectmodel
- \ingroup misc
\ingroup shared
- \mainclass
+
Because C++ forbids unions from including types that have
non-default constructors or destructors, most interesting Qt
@@ -2357,16 +2357,17 @@ QBitArray QVariant::toBitArray() const
}
template <typename T>
-inline T qNumVariantToHelper(const QVariant::Private &d, QVariant::Type t,
+inline T qNumVariantToHelper(const QVariant::Private &d,
const QVariant::Handler *handler, bool *ok, const T& val)
{
+ uint t = qMetaTypeId<T>();
if (ok)
*ok = true;
if (d.type == t)
return val;
T ret;
- if (!handler->convert(&d, t, &ret, ok) && ok)
+ if (!handler->convert(&d, QVariant::Type(t), &ret, ok) && ok)
*ok = false;
return ret;
}
@@ -2388,7 +2389,7 @@ inline T qNumVariantToHelper(const QVariant::Private &d, QVariant::Type t,
*/
int QVariant::toInt(bool *ok) const
{
- return qNumVariantToHelper<int>(d, Int, handler, ok, d.data.i);
+ return qNumVariantToHelper<int>(d, handler, ok, d.data.i);
}
/*!
@@ -2408,7 +2409,7 @@ int QVariant::toInt(bool *ok) const
*/
uint QVariant::toUInt(bool *ok) const
{
- return qNumVariantToHelper<uint>(d, UInt, handler, ok, d.data.u);
+ return qNumVariantToHelper<uint>(d, handler, ok, d.data.u);
}
/*!
@@ -2423,7 +2424,7 @@ uint QVariant::toUInt(bool *ok) const
*/
qlonglong QVariant::toLongLong(bool *ok) const
{
- return qNumVariantToHelper<qlonglong>(d, LongLong, handler, ok, d.data.ll);
+ return qNumVariantToHelper<qlonglong>(d, handler, ok, d.data.ll);
}
/*!
@@ -2439,7 +2440,7 @@ qlonglong QVariant::toLongLong(bool *ok) const
*/
qulonglong QVariant::toULongLong(bool *ok) const
{
- return qNumVariantToHelper<qulonglong>(d, ULongLong, handler, ok, d.data.ull);
+ return qNumVariantToHelper<qulonglong>(d, handler, ok, d.data.ull);
}
/*!
@@ -2466,7 +2467,7 @@ bool QVariant::toBool() const
/*!
Returns the variant as a double if the variant has type() \l
- Double, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
+ Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
UInt, or \l ULongLong; otherwise returns 0.0.
If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
@@ -2476,7 +2477,41 @@ bool QVariant::toBool() const
*/
double QVariant::toDouble(bool *ok) const
{
- return qNumVariantToHelper<double>(d, Double, handler, ok, d.data.d);
+ return qNumVariantToHelper<double>(d, handler, ok, d.data.d);
+}
+
+/*!
+ Returns the variant as a float if the variant has type() \l
+ Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
+ UInt, or \l ULongLong; otherwise returns 0.0.
+
+ \since 4.6
+
+ If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
+ converted to a double; otherwise \c{*}\a{ok} is set to false.
+
+ \sa canConvert(), convert()
+*/
+float QVariant::toFloat(bool *ok) const
+{
+ return qNumVariantToHelper<float>(d, handler, ok, d.data.d);
+}
+
+/*!
+ Returns the variant as a qreal if the variant has type() \l
+ Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
+ UInt, or \l ULongLong; otherwise returns 0.0.
+
+ \since 4.6
+
+ If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
+ converted to a double; otherwise \c{*}\a{ok} is set to false.
+
+ \sa canConvert(), convert()
+*/
+qreal QVariant::toReal(bool *ok) const
+{
+ return qNumVariantToHelper<qreal>(d, handler, ok, d.data.d);
}
/*!
@@ -2767,7 +2802,7 @@ bool QVariant::cmp(const QVariant &v) const
if (d.type != v2.d.type) {
if (qIsNumericType(d.type) && qIsNumericType(v.d.type)) {
if (qIsFloatingPoint(d.type) || qIsFloatingPoint(v.d.type))
- return qFuzzyCompare(toDouble(), v.toDouble());
+ return qFuzzyCompare(toReal(), v.toReal());
else
return toLongLong() == v.toLongLong();
}