c++qtqmetaobject

Using qRegisterMetaType on object pointer with Qt5 throws unresolved errors


I'm currently porting my application from Qt4.8 to Qt5.6

Some code that use to compile and link now stopped linking properly. I narrowed it down to a call to qRegisterMetaType<MyClass*> where MyClass* inherits from QObject.

MyClass is defined and implemented in a separate plugin. So in this context, I'm simply including "MyClass.h" in which I have a Q_DECLARE_METATYPE(MyClass*)

It use to work perfectly with Qt4.8 but with Qt5.6 but now it fails to link:

error LNK2001: unresolved external symbol "public: static struct QMetaObject const MyClass::staticMetaObject"

What has changed with Qt5? Am I doing it wrong?


Solution

  • Better late than never, the answer is indeed to use the macro mentioned in the comments above. It is more or less explained in the manual for int qRegisterMetaType(const char *typeName)

    This function requires that T is a fully defined type at the point where the function is called. For pointer types, it also requires that the pointed to type is fully defined. Use Q_DECLARE_OPAQUE_POINTER() to be able to register pointers to forward declared types.