[Qt-interest] Comparing two QMetaObjects

Thiago Macieira thiago at kde.org
Mon Feb 7 15:35:49 CET 2011


On Monday, 7 de February de 2011 14:58:59 Oliver.Knoll at comit.ch wrote:
> I am not sure whether a->metaObject will return a pointer to the
> A::staticMetaObject instance! In fact I doubt it very much, since I think
> every instance of QObject as its OWN instance of meta object (for the
> concrete signal/slot connections).

That's easy to check:

$ moc <<<'class A: public QObject { Q_OBJECT };' | grep -A3 ::metaObject 
const QMetaObject *A::metaObject() const
{
    return QObject::d_ptr->metaObject ? QObject::d_ptr->metaObject : 
&staticMetaObject;
}

The d_ptr->metaObject thing is for the dynamic meta object used by 
QtDeclarative (and possibly in the future by QtDBus). Under normal 
circumstances, it will be 0.

So that answers your question.
 
> What is it actually you want to achieve again? Do you want to know whether a
> given instance is of a specific class? 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()) {...}

Better yet:
	if (qobject_cast<A *>(a))

as this involves no string comparison. (It's implemented by comparing meta 
objects)

-- 
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/d6d897f7/attachment.bin 


More information about the Qt-interest-old mailing list