typeid meets heritage
-
Hi Qt Community :)
Sorry for the lazy post but ,
i spent a long time searching for a solution to this simple issue :if(typeid(*(element)) == typeid(motherClass)) doSmth();
The typeid can only identify the motherClass instances , and not its childs (inheriting classes)
How to fix this obvious issue ?
Nice regards :) -
Hi,
What do you want to achieve ? Call doSmth if element is a derived class of motherClass ?
-
if (dynamic_cast<motherClass*>(element)) doSmth();
or, in case of QObject derived classes
if (qobject_cast<motherClass*>(element)) doSmth();
-
Does this trick work even if my class is derived of both QObject , and QGraphicsitem ? Because compiler gives me that known warning that object_cast won't work unil i implement the type() method .
-
As I said -
qobject_cast
works only for QObject derived classes. QGraphicsItem does not derive from QObject. For that you need adynamic_cast
. -
Thank you very much , works like a charm :)