Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, February 2007
QFlags, QVariant and Q_DECLARE_METATYPE


Message 1 in thread

I want to store Qt::Alignment values in a QVariant in such a way that I 
can tell if a QVariant contains them at run-time using the 
QVariant::type() function. Is this possible with Q_DECLARE_METATYPE?

QVariant var = ...
if( var.type() == QMetaType::type( "Qt::Alignment" ) )
    ...

Is this possible?

--Dave

--
 [ signature omitted ] 

Message 2 in thread

Dave Smith wrote:
> I want to store Qt::Alignment values in a QVariant in such a way that 
> I can tell if a QVariant contains them at run-time using the 
> QVariant::type() function. Is this possible with Q_DECLARE_METATYPE?
>
> QVariant var = ...
> if( var.type() == QMetaType::type( "Qt::Alignment" ) )
>    ...
>
> Is this possible?
>
> --Dave
>
>

You can do this with a combination of Q_DECALRE_METATYPE(Type) and 
qRegisterMetaType(Type) if the enum isn't already registered.

However, you can only use the template methods in QVariant for custom 
types. So your code will look like this:

if ( var.canConvert<Qt::Alignment>() )
{
    Qt::Alignment align = qvariant_cast<Qt::Alignment>(var);
    // Do stuff....
}

--Justin

begin:vcard
begin:vcard
fn:Justin Noel
n:Noel;Justin
org:ICS;Engineering
adr:;;54B Middlesex Trpk;Bedford;MA;01730;USA
email;internet:justin@xxxxxxx
title:Sr. Consulting Engineer / Certified Qt Instructor
tel;work:(617) 621-0060
url:http://www.ics.com
version:2.1
end:vcard