bulldog68
bulldog68

Reputation: 307

How to get data in form of QString from QVariant in Qt5?

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.

Upvotes: 1

Views: 3017

Answers (1)

scopchanov
scopchanov

Reputation: 8419

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;.

Upvotes: 3

Related Questions