[Qt-interest] Constructing a QEnum with only a metatypeid.
Thiago Macieira
thiago at kde.org
Tue Aug 9 18:00:54 CEST 2011
On Tuesday, 9 de August de 2011 11:44:48 MARTIN Pierre wrote:
> 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 :)
I don't know why you want that...
> > 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.
No.
> > 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();
That constructor is meant to be used as:
Type x = initialiseIt();
QVariant v(qMetaTypeId<Type>(), &x);
That will create a QVariant whose value contains a copy of the type pointed.
It's functionally equivalent to:
QVariant v = QVariant::fromValue(x);
Hence why you shouldn't use the constructor.
> 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.
QVariant is value-based. Its data belongs to it and no one else.
> > 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?
Yes: src/corelib/kernel/qvariant.cpp.
The "why" you get by reading the entire file and realising that there's no
mechanism to tell QVariant and QMetaType that this type is isomorphic to int.
Note: enums are not necessarily isomorphic to int! They could be the size of
short (so there are 2 garbage bytes in a 4-byte read) or the size of a long,
which can be 8 bytes long.
> Any other idea for working QVariants and enums, other than a more ugly "if"s
> branches?
--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Software Architect - Intel Open Source Technology Center
PGP/GPG: 0x6EF45358; fingerprint:
E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110809/d8bfd7e6/attachment.bin
More information about the Qt-interest-old
mailing list