5

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?

Community
  • 1
  • 1
manatttta
  • 3,054
  • 4
  • 34
  • 72
  • 1
    You can use the toString() method as long as it is one of those types: http://doc.qt.io/qt-5/qvariant.html#toString otherwise, you can deduce if the type can be printed via http://doc.qt.io/qt-5/qvariant.html#canConvert-1 – DuKes0mE Dec 15 '16 at 11:02
  • 2
    What will you use that string for? does it have to be human-readable? if so, what if the `QVariant` contains say a `QPixmap`/`QImage`? How would you make a `QString` out of that? – Mike Dec 15 '16 at 11:44
  • 1
    Possible duplicate of [How can I convert QVariant to QString and vice versa in Qt?](http://stackoverflow.com/questions/7228826/how-can-i-convert-qvariant-to-qstring-and-vice-versa-in-qt) – Elcan Dec 15 '16 at 12:59
  • @FlorentUguet did you even read my first statement? – manatttta Dec 15 '16 at 14:25
  • 1
    @manatttta I did, but what you ask is exactly the same. Even if your QVariant is an int, it'll be output as a String. Only if you use custom classes it won't work. – Elcan Dec 15 '16 at 14:27

2 Answers2

16

You can use QVariant::toString for types listed in the method documentation.

int value = 1;
QString s = QVariant(value).toString();
talamaki
  • 5,324
  • 1
  • 27
  • 40
2

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());
...
e.jahandar
  • 1,715
  • 12
  • 30
  • Thanks for the reply. However that is specific for `int` types. I meant all types! – manatttta Dec 15 '16 at 11:01
  • @manatttta i think you could write a function and iterate for all types, added an example. – e.jahandar Dec 15 '16 at 11:04
  • @Falcon I would rather use type() than typeName(), like "if(qv.type() == QVariant::String) etc. – talamaki Dec 15 '16 at 11:39
  • 1
    @e.jahandar , Never compare c strings using `==` use `strcmp` if you want to, see [this question](http://stackoverflow.com/q/4843640/2666212). In this case you can use `qv.type()` instead as suggested by @talamaki, this would be faster and more to the point. Please edit your answer – Mike Dec 15 '16 at 12:06
  • @Mike , its not c string, its Qstring with const char * , lookat documentation, http://doc.qt.io/qt-5/qstring.html the QString class has overloaded == operator – e.jahandar Dec 15 '16 at 12:09
  • 1
    @e.jahandar , [`QVariant::typeName()`](https://doc.qt.io/qt-5/qvariant.html#typeName) returns a `const char*` and you are comparing that with your string literal `"QString"` or `"int"`. Neither the LHS nor the RHS of the `operator==` is a `QString`. – Mike Dec 15 '16 at 12:13
  • 1
    It'd be less overhead to use `QString::number(foo)` instead of `QString("%1").arg(foo)`. And simply do `str = qv.toString()`. Finally, instead of `QString("xyz")` prefer `QStringLiteral("xyz")` - it saves on runtime conversions from C string to QString. – Kuba hasn't forgotten Monica Dec 16 '16 at 00:06