Search code examples
c++qtqmapqvector

how to have a QVector of QMap


I wanna have a QVector of QMap. I used this syntax:

QVector<QMap<QString, QString>> x;
x[0].insert("name", "jim");
x[0].insert("lname", "helpert");
x[1].insert("name", "dwight");
x[1].insert("lname", "schrute");

but this is not working:

enter image description here

I'd appreciate it if someone guide me to the correct format.


Solution

  • The "Index Out of Range" error comes up because you are trying to access an element of the vector which doesn't exist. Instead of accessing a particular index/element of the array it would be better to create a QMap outside of the QVector first and then x.push_back(map) so the map will be happily placed at the back of the QVector.

    A similar thing applies to normal C++ with std::vector as you need to either push_back or emplace_back data onto the vector