I have this set of data:
[ step:-1, name:"BaseName1", value:v1 ]
[ step:-1, name:"BaseName2", value:v2 ]
[ step:2, name:"ParamName3", value:v3 ]
[ step:2, name:"ParamName4", value:v4 ]
[ step:0, name:"ParamName5", value:v5 ]
[ step:0, name:"ParamName6", value:v6 ]
[ step:1, name:"ParamName7", value:v7 ]
[ step:1, name:"ParamName8", value:v8 ]
That I would like to convert to a QVariant, like this:
[
"BaseName1": v1,
"BaseName2": v2,
"steps": [{
"ParamName5": v5,
"ParamName6": v6
}, {
"ParamName7": v7,
"ParamName8": v8
}, {
"ParamName3": v3,
"ParamName4": v4
}
]
]
I figured it should be like this:
QVariantMap params;
params["Base_param_1"] = value;
params["Base_param_2"] = value2;
params["steps"] = QVariantList();
But for the array, I need the data to be ordered; which means I should insert step 0 in the params["steps"]
before steps 1 and 3 (and step 2 should be empty).
Also, sorting the input data is not an option.
I tried to implement this with the following code:
struct Params{
QString name;
QVariant value;
int step;/* -1 for base param */
};
QList<Params> _params = (...);
QVariantMap v_settings;
QVariantList v_steps;
for(int i=0; i<_params.size(); i++){
const Param ¶m = _params[i];
if( param.index == -1 ){
v_settings.insert(param.name, param.value);
}
else {
// if list too small, append empties to the list until it exists
while( v_steps.size() < param.index ){
QVariantMap map;
v_steps.append(map);
}
v_steps.at(param.index).toMap().insert(param.name, param.value);
}
}
v_settings.insert("steps", v_steps);
But the code fails at some point with an index out of bounds when encountering this line:
v_steps.at(param.index).toMap().insert(param.name, param.value);
So I guess my "fill with empty maps" loop failed somehow. How should I do here?