Results 1 to 4 of 4

Thread: QObject::MetaObject()

  1. #1
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QObject::MetaObject()

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QObject::MetaObject()

    Then you are doing something wrong.
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class A: public QObject {
    5. Q_OBJECT
    6. Q_PROPERTY(int propA READ propA WRITE setPropA USER true)
    7.  
    8. public:
    9. int propA() const {return m_propA;}
    10. void setPropA(int propA) {m_propA = propA;}
    11.  
    12. private:
    13. int m_propA;
    14. };
    15.  
    16. class B: public A {
    17. Q_OBJECT
    18. Q_PROPERTY(int propB READ propB WRITE setPropB USER true)
    19.  
    20. public:
    21. int propB() const {return m_propB;}
    22. void setPropB(int propB) {m_propB = propB;}
    23.  
    24. private:
    25. int m_propB;
    26. };
    27.  
    28.  
    29. int main(int argc, char *argv[])
    30. {
    31. QApplication app(argc, argv);
    32.  
    33. QObject *b = new B;
    34.  
    35. const QMetaObject *o = b->metaObject();
    36.  
    37. qWarning() << o->className();
    38. QStringList properties;
    39. for(int i = 0; i < o->propertyCount(); ++i)
    40. properties << QString::fromLatin1(o->property(i).name());
    41. qWarning() << properties;
    42.  
    43.  
    44. delete b;
    45. return 0;
    46. }
    47. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    works perfect.

  3. #3
    Join Date
    Aug 2008
    Posts
    45
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QObject::MetaObject()

    Sounds like you have forgotten to add Q_OBJECT-macro to your class B.

  4. #4
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QObject::MetaObject()

    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...

Similar Threads

  1. Working with QObject
    By been_1990 in forum Qt Programming
    Replies: 15
    Last Post: 20th June 2009, 21:12
  2. Replies: 1
    Last Post: 31st October 2007, 14:14
  3. help on QObject::tr()
    By sincnarf in forum Qt Programming
    Replies: 1
    Last Post: 16th October 2007, 07:52
  4. How to add an QObject to QListWidget
    By steg90 in forum Newbie
    Replies: 7
    Last Post: 14th May 2007, 15:53
  5. Example code and metaObject question
    By bruccutler in forum Newbie
    Replies: 1
    Last Post: 12th January 2007, 18:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.