how to have a QVector of QMap

538 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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

0
On

You are getting "Index Out Of Range" because you are accessing an empty QVector. You need to first insert QMap elements to QVector. Then you can access x[0] -> for first QMap at 0th index, x[1] -> for second QMap at 1st index....... Make QMap object. Insert Elments to it. Make QVector object. Insert that QMap object to this QVector. Read the documents and use appropriate functions for it https://doc.qt.io/archives/qt-4.8/