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
? ie 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());
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.