[SOLVED] Getting Object name/type using moc



  • Hi All,

    Need some help in using meta functionality in Qt. I am probably going wrong in many places.

    I have a Film Class. I have also derived (not sure if this is correct terminology) two more classes under Film, Educational and Entertainment which are sub-classes.

    I have a FilmMap class which is sub-class of QMap<QSting, Film*>.

    I have now written a third class FilmMapWriter which has write XML file of the FilmMap.

    @
    foreach (QString i, map1.keys()) {

       QVariant name = map1[i]->QObject->objectName();
        
       qDebug() << name.toString();
        qDebug()<< " In the FilmMapWriter class";
    
    }
    

    @

    In the above code I am trying to get the type of the object. If it is Film, Entertainment or Educational.

    But I am getting errors. Where am I going wrong?

    Thanks



  • I have changed the code to the following:

    @
    foreach (QString i, map1.keys()) {

       QString name = map1[i]->metaObject()->className();
    
    
       qDebug() << name;
    
    
        qDebug()<< " In the FilmMapWriter class";
    
    }
    

    @

    Now it is working. But the problem now is that it is showing all classes as Film.

    Not any of the sub-classes. Entertainment or Educational.

    How do I get capture that information.

    Thanks



  • Do the subclasses also have Q_OBJECT macros?



  • Hi,

    I did not. Added it and now it work.

    Thanks a lot for your time and assistance.



  • Hi,

    I now have another issue.

    In the Film class I have declared 5 variables.

    In the sub-classes I have declared 2 more variables in each sub-class.

    For example in Film class I have declared a QString m_Director.

    I can access that by the following statement:

    @
    map1[i]->m_Director

    @

    Now I have declared QString m_Subject in the sub-class Educational.

    But when I use "->" I am not getting m_Subject option.

    How do I get it?

    Thanks again.



  • It sounds like a general C++ inheritance tutorial such as http://www.cplusplus.com/doc/tutorial/inheritance/ will be a lot more useful than answers to individual questions.

    For this particular question, the problem might be an access specifier issue, or using a pointer to the base type instead of the derived one. Without seeing the source, it's hard to tell.



  • Managed to do it. Used the qobject_cast operator and I am getting the results.

    Thanks for your time. Appreciated.


Log in to reply
 

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