[Qt-interest] Using typedef'ed custom type as Q_PROPERTY

Ian Thomson Ian.Thomson at iongeo.com
Fri Aug 28 14:11:00 CEST 2009


Hi,

The macro Q_DECLARE_METATYPE(TYPE) calls qRegisterMetaType with the 
actual type as a template parameter and the type converted to a string 
using the stringify macro operator #. If you use "typedef int ID" and 
try to register ID, it will get called as qRegisterMetaType<int>("ID") 
because the template will 'see through' the typedef. As int is already 
registered, this won't work.

You can see this in the source:
http://qt.gitorious.org/qt/qt/blobs/master/src/corelib/kernel/qmetatype.h#line237

So it will work with typedefs, it just won't work with typedefs to types 
that are already registered.

The workaround for you is to just use int as the type of your id for the 
property.

Cheers,
Ian.

Andras Toth wrote:
> Thanks for making that clear. The problem is, however, that if the
> type ID is not registered, a runtime warning is issued:
> 
> QMetaProperty::read: Unable to handle unregistered datatype 'ID' for
> property 'Cat::id'
> 
> and indeed, the property setting and querying does not work for this value.
> 
> Is there a workaround for this, or can we state that it is impossible
> to use typedef'ed types as properties in Qt?
> 
> On Thu, Aug 27, 2009 at 5:50 PM, Thiago
> Macieira<thiago.macieira at trolltech.com> wrote:
>> Em Quinta-feira 27 Agosto 2009, às 16:06:52, Andras Toth escreveu:
>>> Hello, I have been trying all day to use a typedef'ed custom variable
>>> as Q_PROPERTY, however, it seems impossible to read and write this
>>> value through methods property(const char*) and setProperty(const
>>> char*, const QVariant &)
>>>
>> A meta type can only be registered once. Your type (int) is already
>> registered, so you can't register the typedef again.
>>
>> --
>> Thiago Macieira - thiago.macieira (AT) nokia.com
>>  Senior Product Manager - Nokia, Qt Development Frameworks
>>     Sandakerveien 116, NO-0402 Oslo, Norway
>>
>> Qt Developer Days 2009 | Registration Now Open!
>> Munich, Germany: Oct 12 - 14     San Francisco, California: Nov 2 - 4
>>      http://qt.nokia.com/qtdevdays2009
>>
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>
>>
> 
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest




More information about the Qt-interest-old mailing list