[SOLVED] QMetaObject::className() returns base name



  • I need to to retrieve an object's class to save some values to an XML file. I'm able to use typeid, but just tried to use Qt for it and ran into a problem.

    I have a class B inheriting from class A (A inherits from QObject). I have a manager class M which has a QList<A*>, but actually stores B objects. Inside B I have a function exportXml():

    @void B::exportXml()
    {
    cout << metaObject()->className() << endl;
    }
    @

    The thing is that the output statement prints "A" rather than what I expected: "B". I think it might be because I'm storing the B objects as A-pointers in M and that something might go wrong due to polymorphism. But since I'm calling metaObject() from inside B, shouldn't this print B instead of A, or am I doing something wrong?



  • Is exportXml() also implemented in A? In that case, maybe you have to make it virtual so it resolves to the derived class (B).



  • exportXml() is a virtual function in A and is re-implemented in B



  • Have you included the Q_Object macro in your B class as well?



  • Ahh, adding Q_OBJECT to the subclass did the trick. Thanks panosk.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.