[Qt-interest] QVariant::type() usage for custom QVariant types

Bruno Tezine bruno at tezine.com
Thu Jul 28 15:38:46 CEST 2011


Hi Eckhard,
This might not be the best aproach, but it works:
You can get the type() by using qMetaTypeId<YourClass> ();
This will give you the type id at compile time. Then you can use the 
number in switch.

Em 28/07/2011 09:19, Eckhard Jokisch escreveu:
> Hi,
>
> thanks to Q_DECLARE_METATYPE it is possible to use custom classes as
> QVariant.
>
> Now when I use this QVariant I have to check either with
>
> ---------------------------------
> QVariant v;
> if(v.canConvert<myClaas1>())
> 	....
> else if(v.canConvert<myClaas2>())
> 	....
> else if(v.canConvert<myClaas3>())
> 	....
> ------------------------------
> or I have to lookup v.typeName() in a QStringList to find the index and
> then do  a switch like this:
> ---------------------------------
> QStringList list;
> list.append("myClass1");
> list.append("myClass2");
> list.append("myClass3");
>
> switch(list.indexOf(v.typeName)
> {
> .....
> }
> ---------------------------------
>
>
> How can I use
> int i = v.type();
> and get a unique and reliable (in terms of usable in a switch/case)
> typenumber for my various classes?
>
> _______________________________________________
> 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