1

I have a QVariant with userType QVariantList and the exact format looks like this when i qDebug the qvariant

QVariant(QVariantList, (QVariant(QVariantMap, QMap(("name", QVariant(QString, "UK English Female"))))

I want QString, "UK English Female" from the variant how can i get that i have already tried QVariant.toStringList() but the stringlist is empty. Thanks.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
bulldog68
  • 197
  • 1
  • 10
  • 1
    Could you provide a [SSCCE](http://sscce.org/)? It is unclear what you are asking. It looks like you need to write a couple lines of code to build a string, but you don't. – Dmitry Sazonov Jan 04 '18 at 08:50

1 Answers1

3

From what is shown as a debug output, I presume it was produced in a similar manner:

QVariantMap map;

map.insert("name", QVariant("UK English Female"));
qDebug() << QVariant(QVariantList{QVariant(map)});

and as a result you have a string data, which is burried relatively deep in a sequence of containers.

So, how to get there?

As a whole you have a QVariant holding a QVariantList with a single QVariant value, which in turn is a QMap. The map has a key name of type QVariant holding a QString. Chaining the necessary conversions, we end up with:

qDebug() << myVar.value<QVariantList>().first().toMap().value("name").toString();

assuming the whole thing is held in QVariant myVar;.

scopchanov
  • 7,966
  • 10
  • 40
  • 68
  • 1
    Thanks a lot it solved my problem. Actually am getting that variant by calling a JS function in QWebview. :) – bulldog68 Jan 04 '18 at 11:56