I will do a method that write data on modbus. I need that QVariant return if is String or Int.
My var.: QVariant dataToWrite
Exist a method that return dataToWrite content is String or Integer? In c# I did how this solution: How do I identify if a string is a number?
I saw documentation on this link: http://doc.qt.io/qt-5/qvariant.html But in qt i didn't find solutions.
Thanks
To know if a QVariant
is a QString
or an integer you have to use QVariant::type()
or QVariant::userType()
.
bool isInteger(const QVariant &variant)
{
switch (variant.userType())
{
case QMetaType::Int:
case QMetaType::UInt:
case QMetaType::LongLong:
case QMetaType::ULongLong:
return true;
}
return false;
}
bool isString(const QVariant &variant)
{
return variant.userType() == QMetaType::QString;
}
If you use other methods to get the type of the variant, you will get wrong answers.
For instance:
// This function does not work!
bool isInteger(const QVariant &variant)
{
bool ok = false;
variant.toInt(&ok);
return ok;
}
QString aString = "42";
QVariant var = aString;
isInteger(var); // Returns true, but var.type() returns QMetaType::QString !!!
Now if you want to know if a QVariant
can be converted to an integer, you have the beginning of an answer in Qt Documentation:
Returns the variant as an int if the variant has userType() QMetaType::Int, QMetaType::Bool, QMetaType::QByteArray, QMetaType::QChar, QMetaType::Double, QMetaType::LongLong, QMetaType::QString, QMetaType::UInt, or QMetaType::ULongLong; otherwise returns 0.
Which means that there are many non integer variants that can be converted to an integer. The safest way to do it is to use QVariant::canConvert()
and QVariant::value()
:
QString aString = "42";
QVariant var = aString;
if (var.canConvert<int>())
{
auto anInteger = var.value<int>();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With