PDA

View Full Version : How to define class RTTI info macro?



lni
7th January 2010, 08:05
I am trying to define some macro for RTTI identification to get some class info, such as class name and base class name for object reconstruction. The class is derived from QGraphicsItem.

For instance:



class MyGraphicsItem : public QGraphicsItem {
...
}


I need some macro so I can do
obj->getClassName() // return "MyGraphicsItem"
or
obj->getBase()->getClassName() // return "QGraphicsItem"

How do I define those macros and how to use it?

Many thanks

wysota
7th January 2010, 10:09
Inherit from both QObject and QGraphicsItem and make sure you put the Q_OBJECT macro in the class header.

lni
7th January 2010, 12:26
There will be costly to inherit from QObject as there are thousand of QGraphicsItem.

I just need a macro so I can get class info, such as



class MyGraphicsItem : public QGraphicsItem
{
TYPE_INFO( MyGraphicsItem )
...
}


But I don't know how to write such macro...

faldzip
7th January 2010, 12:36
I think that much more simple would by to use QGraphicsItem::type()

lni
7th January 2010, 12:57
I have many subclass from QGraphicsItem, it is very hard to track the type(). I have to make sure they don't return the same value. It would be easier to have a macro ....

wysota
7th January 2010, 15:18
But what would this macro return? And why a macro and not a method? You can create a virtual method in your common base class to return the name of the class and reimplement the method in each subclass. Of course that's more or less equivalent of using QGraphicsItem::Type...