I'm using nested QVariantMap and have a problem with defining a method that takes a path (string list) and returns the pointer to low-level QVariantMaps:
QVariantMap * getQVariantMap( QStringList spec) const {
QVariantMap * deep_p = this->hash_p;
for( QStringList::const_iterator it = spec.begin();
it!= spec.end();
++it)
{
QVariantMap::const_iterator i = hash_p->find( *it);
if( i == hash_p->end())
return 0x00;
res_p = & i.value().toMap();
}
return res_p;
}
...
QVariantMap map, level1, level2;
level2["L2"] = "bazzinga!";
level1["L1"] = QVariant::fromValue( level2);
level1["foo"] = "foo";
map["L0"] = QVariant::fromValue( level1);
map["foo"] = "foo";
QStringList qsl;
qsl.append( "L1");
qsl.append( "L0");
QVariantMap * qvm = getQVariantMap( qsl);//pointer to level2 part of the nested QVariantMap.
qvm[ "foo"] = "baz";
//map[ "L0"].toMap()[ "L1"].toMap()[ "foo"] == "baz";
...
I got the following error:
taking address of temporary [-fpermissive]
and I know why. The problem is that I need to get the pointer to the part of the nested structure, but I can't get reference to the internals of the QVariant. I tried to use data_ptr, suggested in post Assigning to nested QVariantMap, with no luck. For the obvious reasons I don't want to make special wrappers on insert/remove/value (that why, as I understood, accepted answer in linked topic is not my case).
I tried:
- to store QPointer inside QVariant -- with no luck because QVariant is not QObject;
- to store pointer using QVariant::fromValue( QVariantMap *), but when I making toMap() call it return the QVariantMap, and not the pointer, so I got the taking-address-of-temporary-error again. And either way, I don't think that this is what I want.
How someone should do it properly?