There are several issues in your code.
"Index is out of bounds" issue happens because you didn't add enough objects to your list. You should write
while( v_steps.size() <= param.index ) {
i.e. replace <
with <=
. It will ensure that, e.g. 6 elements will be contained in the array before you access the 5th element (elements count from 0, remember?)
Another issue is in toMap().insert
. toMap()
returns QMap
by value, not by reference. So there is no point in calling insert
on it because it's just temporary value that has not been assigned to any variable. It will be destroyed immediately after insert
ends, and no changes will be stored.
I suggest you to use QVector<QVariantMap> v_steps
to store your data. QVector
is better than QList
in this case because it's easy to resize()
it. Inserting can be done using the following code:
v_steps[param.index].insert(param.name, param.value);
Then you need to convert it to QVariantList
:
QVariantList v_steps_real;
foreach(QVariantMap map, v_steps) {
v_steps_real << map;
}
v_settings.insert("steps", v_steps_real);
Note that you can use []
operator instead of insert()
to insert values into a map:
map["key"] = value;