[Qt-interest] Constructing a QEnum with only a metatypeid.
Eckhard Jokisch
e.jokisch at orange-moon.de
Thu Aug 11 10:12:43 CEST 2011
Seems to me the same problem that I had.
I solved it with a workaround using v.typeName() and searching the typename in a const QList with list.indexOf().
Maybe it's possible to subclass QVariant and add a setCustomType(int) function?
Eckhard
> Thanks a lot Thiago. Everything is clear for me now.
>
>>>>> 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…
> Because i need the QVariant to carry the value as well as the type.
>
>> 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.
> i have to, because using QVariant::fromValue doesn't store the type information (When having the key value for my enum, it's type is unknown because everything happens at runtime. So i just have created a small constructor static function which takes a metatype id and an int value for the enum, and do the QVariant::fromValue<right type here>() based on the metatype.
>
>>> 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.
> i understand that, and that's exactly what i'm looking for. The only thing giving me so much sadness is that Moc'ing an enum doesn't generate much for them to use with QVariants after all :)
>
>> 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.
> i see.
>
> Thanks,
> Pierre.
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
More information about the Qt-interest-old
mailing list