[Qt-interest] Comparing two QMetaObjects

Thiago Macieira thiago at kde.org
Mon Feb 7 15:39:36 CET 2011


On Monday, 7 de February de 2011 15:23:20 Oliver.Knoll at comit.ch wrote:
> On 2011-02-07 ITS-CMT-SL-SFR-FIN-DEV Knoll Oliver, ITS-CMT-SL-SFR-FIN-DEV 
wrote:
> > ... Usually that is done with
> > http://doc.qt.nokia.com/4.7/qobject.html#inherits (which makes use of
> > the meta
> > 
> > objects), as in:
> >   if (a->inherits(A::staticMetaObject().className()) {...}
> 
> The comment from Thiago reminded me of a very crucial thing: EVERY object in
> the class hierarchy has to have the Q_OBJECT macro in their header/class
> declaration! 

Well, the code compiles just fine without it. The question is whether that 
missing Q_OBJECT was something you needed, like you mention:

> if (myClass.inherits("MyClass")) {...}
> 
> would fail, because myClass.metaObject().className() would return "QObject"
> and NOT the (expected) "MyClass" value: no proper meta object information
> is generated for class MyClass, due to the missing Q_OBJECT declaration!

Correct. And if you use the more pleasing:
	if (qobject_cast<MyClass *>(myClass))

you'll get an error with something about a function called 
qYouForgotTheQ_OBJECT_Macro. See:

http://labs.qt.nokia.com/2007/02/26/new-feature-compiler-errors/

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110207/827b3eea/attachment.bin 


More information about the Qt-interest-old mailing list