[Qt-interest] Constructing a QEnum with only a metatypeid.

MARTIN Pierre hickscorp at gmail.com
Tue Aug 9 11:44:48 CEST 2011


THank you for your time Thiago!

>> Can someone provide me with an example of code showing how to construct a
>> QVariant "v", based on a given QVariant::UserType "t" where "t" refers to a
>> type registered for an enum type?  
> QVariant v(t, 0);
Ha, that's what i was doing already… But the docs seem to not recommend using that constructor, hence my question :)

> The value will be default constructed. I hope you like your enums of value 0. 
Isn't there a way to tell QVariant that the given metatype "x" gets outputted as "int"? It would be OK to not have the Cnames in the output, just their integal casts / values.

> The 0 above is not the value, it's a null pointer.
However: The data pointed by the second argument seems to be copied into an internal location. When i do:
int const			*pVal	= &val;
QVariant			var		(t, pVal);
Where val is an int value of my enum, everything is working ok, and the variant doesn't seem to store the value rather than the memory address (Which is good for me, and makes things partially work after all). But to recover the value of the variant, i have to do a ugly:
int const	*prVal	= (int const*)v.constData();
And it works, but defeats part of QVariant's functionality… Also, i checked at this point, the memory address returned by constData is really different than the one given in the constructor… Which makes me say that the data is actually copied rather than the memory address itself.

> It will print the type name but not the value. Printing the value is not 
> possible.
Can i have a hint where to search in the sources "why"? Wouldn't it be possible to get the int value outputted at all?

Any other idea for working QVariants and enums, other than a more ugly "if"s branches?

Thanks a lot for all your help!
Pierre.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110809/45d88cb1/attachment.html 


More information about the Qt-interest-old mailing list