[Interest] Why no T& QVariant::value()?

Jason H jhihn at gmx.com
Mon Jun 4 15:05:44 CEST 2018



> Sent: Sunday, June 03, 2018 at 7:34 PM
> From: "Hamish Moffatt" <hamish at risingsoftware.com>
> To: interest at qt-project.org
> Subject: Re: [Interest] Why no T& QVariant::value()?
>
> 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.

Because I'm not just storing QRects, I'm storing everything that a QVariant can.



More information about the Interest mailing list