c++qtenumsqt5

Converting enum into String using QMetaEnum


I have searched a lot for this topic and already found some approach but I get some errors I can't find the reason of it.

Idea is to read the keys from the enum with QMetaEnum to fill the strings in a combobox later.

I have already the enum and also setup Q_Object and Q_Enum Macro in the class where the enum is. But I am getting "undefined reference to 'Planet:: metaObject() const'" error message by using the QMetaEnum.

Here is the planet.h

#include <QMetaEnum>
class Planet
{
    Q_OBJECT
public:    
    enum PlanetTypes
    {
       Barren,Gas,Ice,Lava,Oceanic,Plasma,Storm,Temperate
    };Q_ENUM(PlanetTypes)
    Planet();
    //some getters and setters for my private member-variables
};

Here is the snipped where I try to read the enum and getting the error message.

QStringList DataModel::getPlanetTypes()
{
   QStringList PlanetTypesList;

   Planet p;
   const QMetaObject* metaObj = p.metaObject();
   QMetaEnum e = metaObj->enumerator(metaObj->indexOfEnumerator("PlanetTypes"));
   for(int i=0; i<e.keyCount();i++)
   {
        PlanetTypesList.append(e.key(i));
   }
   return PlanetTypesList;

}

The error refers to the line:

QMetaEnum e = metaObj->enumerator(metaObj->indexOfEnumerator("PlanetTypes"));

I even tried to inherit planet from QObject but it also did not solve the issue.

Would be really cool to get some help and maybe some further explanation of QMetaEnum usage.

Edit: it also gives me an error saying: undefined reference to 'vtable for Planet' if that helps somehow to get an idea where this issue comes from.

Edit2: i've found this for the 'vtable' issue but it also does not resolve this error. Qt undefined reference to vtable


Solution

  • I found the answer of my Question.

    As i was researching about this vtable issue i've found this post. C++ - Undefined reference to `vtable

    and i have given it a shot and removed Q_Object macro from the class. Then both errors disappear

    Edit: This does not solve it! But you can find the solution here: QMetaEnum does not read keys from enum As i made the second post i wasn't aware that both issues were connected with each other.