[SOLVED] Getting Object name/type using moc
-
wrote on 28 Sept 2014, 17:22 last edited by
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
-
wrote on 28 Sept 2014, 17:37 last edited by
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
-
wrote on 28 Sept 2014, 20:41 last edited by
Do the subclasses also have Q_OBJECT macros?
-
wrote on 28 Sept 2014, 21:23 last edited by
Hi,
I did not. Added it and now it work.
Thanks a lot for your time and assistance.
-
wrote on 28 Sept 2014, 22:20 last edited by
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.
-
wrote on 29 Sept 2014, 00:12 last edited by
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.
-
wrote on 29 Sept 2014, 20:30 last edited by
Managed to do it. Used the qobject_cast operator and I am getting the results.
Thanks for your time. Appreciated.
1/7