PDA

View Full Version : QObject::MetaObject()



pkj
4th June 2011, 07:39
Hi,
Suppose class A subclasses QObject and class B subclasses class A. Now if i store class B pointer in QObject * obj and call obj->MetaObject(), the metaObject I receive contains metatype information of class A and nothing of class B. Can I drill down information of class B with the class hierarchy I have, or class B must inherit from QObject directly if I am to see into metaobject properties of class B. I see that QObject::MetaObject() is virtual. Will reimplementing it in class B help?

Lykurg
4th June 2011, 08:08
Then you are doing something wrong.
#include <QtGui>
#include <QDebug>

class A: public QObject {
Q_OBJECT
Q_PROPERTY(int propA READ propA WRITE setPropA USER true)

public:
int propA() const {return m_propA;}
void setPropA(int propA) {m_propA = propA;}

private:
int m_propA;
};

class B: public A {
Q_OBJECT
Q_PROPERTY(int propB READ propB WRITE setPropB USER true)

public:
int propB() const {return m_propB;}
void setPropB(int propB) {m_propB = propB;}

private:
int m_propB;
};


int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QObject *b = new B;

const QMetaObject *o = b->metaObject();

qWarning() << o->className();
QStringList properties;
for(int i = 0; i < o->propertyCount(); ++i)
properties << QString::fromLatin1(o->property(i).name());
qWarning() << properties;


delete b;
return 0;
}
#include "main.moc"works perfect.

joyer83
4th June 2011, 10:24
Sounds like you have forgotten to add Q_OBJECT-macro to your class B.

pkj
4th June 2011, 11:41
I missed Q_OBJECT macro.. eeks... joyer83 u were right... And after I did add I ran into all sorts of trouble.. untill i came to know i have to qmake again...