在带有 Qt5 的对象指针上使用 qRegisterMetaType 会引发未解决的错误
Using qRegisterMetaType on object pointer with Qt5 throws unresolved errors
我目前正在将我的应用程序从 Qt4.8 移植到 Qt5.6
一些用于编译和 link 的代码现在可以正常停止 linking。
我将其缩小为对 qRegisterMetaType<MyClass*>
的调用,其中 MyClass* 继承自 QObject。
MyClass 是在一个单独的插件中定义和实现的。所以在这种情况下,我只是包括 "MyClass.h",其中我有一个 Q_DECLARE_METATYPE(MyClass*)
它过去与 Qt4.8 完美配合,但与 Qt5.6 配合使用,但现在无法正常工作 link:
error LNK2001: unresolved external symbol "public: static struct QMetaObject const MyClass::staticMetaObject"
Qt5 有什么变化?我做错了吗?
迟到总比不到好,答案确实是使用上面评论中提到的宏。 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.
我目前正在将我的应用程序从 Qt4.8 移植到 Qt5.6
一些用于编译和 link 的代码现在可以正常停止 linking。
我将其缩小为对 qRegisterMetaType<MyClass*>
的调用,其中 MyClass* 继承自 QObject。
MyClass 是在一个单独的插件中定义和实现的。所以在这种情况下,我只是包括 "MyClass.h",其中我有一个 Q_DECLARE_METATYPE(MyClass*)
它过去与 Qt4.8 完美配合,但与 Qt5.6 配合使用,但现在无法正常工作 link:
error LNK2001: unresolved external symbol "public: static struct QMetaObject const MyClass::staticMetaObject"
Qt5 有什么变化?我做错了吗?
迟到总比不到好,答案确实是使用上面评论中提到的宏。 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.