[Interest] QMetaType::metaObjectForType

Graham Labdon Graham.Labdon at avalonsciences.com
Mon Feb 17 11:09:50 CET 2014


Thanks!

-----Original Message-----
From: Stephen Kelly [mailto:stephen.kelly at kdab.com] 
Sent: 17 February 2014 10:06
To: Graham Labdon
Cc: interest at qt-project.org
Subject: Re: [Interest] QMetaType::metaObjectForType

On Monday, February 17, 2014 09:47:20 you wrote:
> Hi
> Thanks for your quick reply.
> This does indeed work, but I would be grateful if you could explain 
> why
> 

> > Q_DECLARE_METATYPE(Widget)
> 
> Remove this line.

You don't pass around copies of QObject derived types because it is not copyable. You pass around pointers instead. So, you don't want 'Widget' to be a metatype, you want 'Widget*' to be a metatype so that you can put a Widget* in a QVariant for example.

You can use 

 Q_DECLARE_METATYPE(Widget*)

but it is not needed as of Qt 5.0. There's some background info here:

 http://steveire.wordpress.com/2011/03/16/implementing-qvariantqmetatype-features-with-template-tricks/

and the problems were solved and implemented in 5.0

 http://www.kdab.com/kdab-contributions-to-qt-5-0-part-1/

So, it's better to remove the Q_DECLARE_METATYPE that you don't need rather than add one that is wrong.

> > Class.cpp
> > const int WidgetTypeID = qRegisterMetaType<Widget>("Widget");
> 
> Register the pointer. Don't specify the string:
> 
>  qRegisterMetaType<Widget*>();

By using a string there, you run the risk of typos. The string is not needed and should not be used (even with Qt 4 code). 

Thanks,

--
Stephen Kelly <stephen.kelly at kdab.com> | Software Engineer KDAB (Deutschland) GmbH & Co.KG, a KDAB Group Company www.kdab.com || Germany +49-30-521325470 || Sweden (HQ) +46-563-540090 KDAB - Qt Experts - Platform-Independent Software Solutions



More information about the Interest mailing list