I'm using QVariant
to manage the project settings of our In-House application.
For this I'm using a nested QVariantMap
recursively containing QVariantMap
s and leaves holding the actual values.
Now, I found it quite cumbersome to set and remove nodes from this tree like structure. Especially, if the nesting reaches a certain depth.
Part of the problem is, that value<T>
returns a copy instead of a reference. (I'm wondering, why Qt is lacking this feature??)
#include <QVariantMap>
#include <QDebug>
int main(int argc, char** args) {
QVariantMap map = { {"A", QVariantMap{ {"B",5.} }} };
{
// Change value
auto nested = map["A"].value<QVariantMap>();
nested["B"] = 6;
map["A"] = nested;
qDebug() << map;
// What it should be
// map["A"]["B"] = 5;
}
{
// Remove value
auto nested = map["A"].value<QVariantMap>();
nested.remove("B");
map["A"] = nested;
qDebug() << map;
// What it should be
// map["A"].remove("B");
}
}
What might be the easiest way to directly set and remove values and to make my function a one-liner? Performance is not critical, but ease of usability is definitely an issue for me.