I want to convert a QVariant
that stores a string to a value with the template method value()
. The same thing can be done with other methods like toInt()
, toDouble()
and so no.
My problem now is that using for example toDouble(bool *ok = Q_NULLPTR)
I can pass as argument a pointer to bool for check if the conversion was gone well. but I can't perform this check with value()
. Here a little example to reproduce it.
#include <QVariant>
#include <QDebug>
int main()
{
QVariant v;
QString str = "300.0"; //Valid number
v.setValue(str);
QVariant v2;
QString str2 = "3kk.4f"; //Invalid number
v2.setValue(str2);
if( v.canConvert<double>() ) {
qDebug() << "Ok QString to double is permitted";
qDebug() << "Result: " << v.value<double>();
}
if( v2.canConvert<double>() ) {
qDebug() << "Yes QString to double is already permitted";
qDebug() << "Result: " << v2.value<double>();
//Oh oh result is 0.0 but is string invalid or it really contain value 0 ?
}
return 0;
}
Some tips on how to do this conversion check using the template method value()
?