I am aware of how to convert a QVariant
containing a QString
to a QString
:
How can I convert QVariant to QString and vice versa in Qt?
What I want to ask is how do I convert ANY QVariant
into a QString
? i.e. even if my QVariant
object has an int
, is there an easy way to convert it to QString
?
You can use QVariant::toString
for types listed in the method documentation.
int value = 1;
QString s = QVariant(value).toString();
You can use Qstring formating
QVariant qv(1);
QString str = QString ("%1").arg(qv.toInt());
also you it could be more generic like this
if(qv.typeName() == QString("QString"))
str = QString("%1").arg(qv.toString());
else if(qv.typeName() == QString("int"));
str = QString ("%1").arg(qv.toInt());
...
or by using qv.type()
if(qv.type() == QVariant::String)
str = QString("%1").arg(qv.toString());
...
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