0

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

  • what toolchain you are using, are you certain that your header file is added to project and moc utility is called for it? Q_OBJECT is essential to any meta-properties, other macros are useless without it present. – Swift - Friday Pie Jun 12 '21 at 18:50

3 Answers3

0

Including QMetaEnum and deriving from QObject usually does the trick:

#include <QMetaEnum>
class Planet : public QObject
    {
        Q_OBJECT
    public: 

Everything else looks fine (at first glance).

Jens
  • 6,173
  • 2
  • 24
  • 43
  • I tried to inherit from QObject again just in case, but the error keeps there, sadly. I edited my code post because i forgot to mention the inclusion. It was allredy there. – Rolf Hausen Jun 11 '21 at 22:17
  • I have my doubts about your "solution" of removing Q_OBJECT. I tried it with Q_OBJECT declared in the private section of the class and it worked. Looks more like a dirty build or a header missing in the HEADERs section of your pro file. – Jens Jun 12 '21 at 16:17
0

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.

  • Actually no! this is not the solution. Afterwards i found that it then just doesn' work at all. But swift-friday-pie gave me a solution for the other problem appeared after removing Q_Object. so if you stumble on the same issue read here https://stackoverflow.com/questions/67947276/qmetaenum-does-not-read-keys-from-enum – Rolf Hausen Jun 13 '21 at 12:35
0

There are only two possible explanations for it:

  1. moc utility isn't called to process your header file containing Planet for whatever reason (bad timestamp which prevents it to be executed second time and there is old version of moc_planet.h, file not added to project and isn't in makefile rules, there is duplicate of header)

  2. compiler optimized-out empty vtable for Planet class. In this case any virtual function that isn't declared inline, e.g. ~Planet(); with body defined in .cpp, will do.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42