[Interest] Why no T& QVariant::value()?
Hamish Moffatt
hamish at risingsoftware.com
Mon Jun 4 01:34:41 CEST 2018
On 24/05/18 00:16, Jason H wrote:
> Really wishing this function existed. Can someone explain why it doesnt' exist?
QVariant::value<T>() converts to value T (as per the documentation). How
could it return an lvalue then, as the value returned may not be of the
same type?
>
> I'm using it in conjunction with QVariantMap and QVariant list, where I want to change the value held at key or index. But in a nested map or list, since value() returns a copy, the change does not persist in the container.
>
> QVariantMap mapOfMap;
> QVariantMap map {{"key", QRect(0,0,5,5)}};
> mapOfMap["map"]=map;
> mapOfMap["map"].toMap()["key"] = QRect(1,1,6,6); // copies all the way down, changed a copy of mapOfMap["map"].toMap()["key"], gets discarded
> map = mapOfMap["map"].toMap();
> map["key"] = QRect(1,1,6,6); // changed a copy of mapOfMap["map"].toMap()["key"]
> mapOfMap["map"]=map // but we set the copy to be in the original map, so it'll stay
>
> Help?
Use a different map? There must be some other reason you are using
QVariantMap because there doesn't seem to be any need from this code.
Hamish
More information about the Interest
mailing list