2

I have a QVariantMap whose key is a string and value is an array(of ints or strings)

How do I get the individual elements of the array?

map["key"] has a method toList(). Can I apply that to arrays?

user1065969
  • 589
  • 4
  • 10
  • 19

2 Answers2

5

Yes,

QString first_string_of_key = QVariantMap["key"].toList()[0];

but this is only for reading. Any attempt to write will not work because QVariant will return a copy of the list. Read this post: Assigning to nested QVariantMap

Community
  • 1
  • 1
Joren Boulanger
  • 131
  • 2
  • 3
0

Maybe you can try this if we say that we have an iterator it moving around the keys:

we could say

if(it.value("key").type()==QMetaType::ByteArray)
  std::vector<std::string> vs_array_valus(QVariantMap["key"].value().toArray());

Or direct use as below:

if(QVariantMap["key"].value().type()==QMetaType::ByteArray)
std::vector<std::string> vs_array_valus(QVariantMap["key"].value().toArray());

Hope this Will help.

norrr
  • 24
  • 1
  • 3