I have a collection of QVariantMaps which contains QVariantLists in SOME of their entries.
I need to append to the lists. These lists can grow rather large, and I have to do this for many of them, many times per second, basically the whole time the program is running. So, efficiency is pretty important here..
I'm not arriving at a way to avoid making copies of these lists though! They might as well be immutable. :(
This works:
map[ key ] = QVariantList() << map[ key ].toList() << someOtherVarList;
But, that's doing a whole lot more work (under the hood) than I want...
Here's something I've tried, but ran into a wall:
I can get a non-const pointer to the variant I want to modify directly by using this macro I wrote utilizing a QMutableMapIterator
:
#define getMapValuePtr( keyType, valueType, map, keyValue, ptrName ) \
valueType *ptrName( nullptr ); \
for( QMap<keyType, valueType>::iterator it = map.begin(); \
it != map.end(); ++it ) \
{ if( it.key()==keyValue ) { ptrName = &(*it); break; } }
Example:
const QString key( "mylist" );
QVariantMap map;
map[ key ] = QVariantList( {"a","b","c"} );
getMapValuePtr( QString, QVariant, map, key, valuePtr )
So now valuePtr
could give me the ability to directly modify the value in the map, but the fact it's a QVariant is still in the way...
I have next tried the following without Qt Creator highlighting anything to indicate an error, but it does NOT quite compile despite that...
QVariantList *valListPtr( qvariant_cast<QVariantList *>(*valuePtr) );
valListPtr->append( QVariantList( {"x","y","z"} ) );
Ideas?
Note: Not all entries in these maps are list. I CANNOT therefore change my map type to QMap<QString,QVariantList>
. I tried, and the many client classes to this code all threw fits... That would have simplified matters, had it worked, by implicitly eliminating this final sticking point.