Q_DECLARE_METATYPE and qRegisterMetaType?
-
Hello all,
I am using Qt 5.4.2. I have an enum, ClientError, that I am trying to use in several ways:
- A meta type for use with QVariants in a QAbstractListModel:
errorVariant.canConvert<ErrorHandler::ClientError>()
- A function parameter:
ErrorModel::addError(ErrorHandler::ClientError)
- A QML enum:
errorModel.addError(ClientError.MajorError)
ErrorHandler.h contains the following:
class ErrorHandler : public QObject { Q_OBJECT Q_ENUMS(ClientError) public: enum ClientError { MajorError, ... ... }; Q_DECLARE_METATYPE(ErrorHandler::ClientError) #endif // ERRORHANDLER_H
Main.cpp contains the following:
#include <client/ErrorHandler.h> int main(int argc, char **argv) { ... ... qRegisterMetaType<ErrorHandler::ClientError>("ErrorHandler::ClientError"); qmlRegisterUncreatableType<ErrorHandler>("Client", 1, 0, "ClientError","Display Client Errors");
When I build, I get the following:
main.obj:-1: error: LNK2019: unresolved external symbol "public: static struct QMetaObject const ErrorHandler::staticMetaObject" (?staticMetaObject@ErrorHandler@@2UQMetaObject@@B) referenced in function "int __cdecl qRegisterNormalizedMetaType<class ErrorHandler *>(class QByteArray const &,class ErrorHandler * *,enum QtPrivate::MetaTypeDefinedHelper<class ErrorHandler *,1>::DefinedType)" (??$qRegisterNormalizedMetaType@PEAVErrorHandler@@@@YAHAEBVQByteArray@@PEAPEAVErrorHandler@@W4DefinedType@?$MetaTypeDefinedHelper@PEAVErrorHandler@@$00@QtPrivate@@@Z)
Am I doing anything wrong? Is it possible to double-use an enum in this way?
Thanks.
-
Is this a library? I mean where you define the
ErrorHandler
class. If so you need to export that class from it.PS. Also use
qRegisterMetaType
without a string argument:qRegisterMetaType<ErrorHandler::ClientError>();
-
By export you mean "CONFIG += client" in clientApp.pro, correct?
I tried without a string argument, same result.
-
By export you mean "CONFIG += client" in clientApp.pro, correct?
I tried without a string argument, same result.
-
It is a library and it's exported correctly. I am able to access a different enum from a different class in the same library from QML; the only difference is me using Q_DECLARE_METATYPE with ClientError.
@EStudley said in Q_DECLARE_METATYPE and qRegisterMetaType?:
Firstly, you shouldn't need to useQ_DECLARE_METATYPE
(orqRegisterMetaType
) with the enum at all, this is handled by themoc
. Here you can see that the macro expands to nothing.
Secondly, your linker (MSVC) complains that it can't find themetaObject
for yourErrorHandler
enum, which is normal as it is not exported from the binary byQ_DECLARE_METATYPE
.In conclusion:
Don't useQ_DECLARE_METATYPE
andqRegisterMetaType
for yourQObject
enum. Use it if you want to use it as a global enumerator and then you need to call the meta-type runtime registration from the library, not from the application.