0

I am storing QVariantMap as QVariant inside another QVariant. I need to add fileds in this nested QVariantMap in an slot function. Here is what I have so far:-

QVariantMap map = data["nestedMap"].toMap(); //first copy 
map[newfield] = assingCoolStuff(); //add new filed to nestedMap
data["nestedMap"] = map; // second copy ?

If I'm correct then its creating copy of nestedMap two time and will run so slowly. is there any way to optimize this ?

drescherjm
  • 10,365
  • 5
  • 44
  • 64
너를 속였다
  • 899
  • 11
  • 26

1 Answers1

0
template<class T>
T* cast(QVariant& variant)
{
    if (variant.userType() == qMetaTypeId<T>())
        return reinterpret_cast<T*>(variant.data());
    return 0;
}

QVariantMap &map = *cast<QVariantMap>(data["nestedMap"]);
map[newfield] = assingCoolStuff();

If data["nestedMap"] might not contain QVariantMap, changes to the code are needed.

Hrisip
  • 900
  • 4
  • 13