Q_DECLARE_METATYPE with using and same types
Solved
General and Desktop
-
Hello,
i'm having issues with Q_DECLARE_METATYPE. My header file:
// ... namespace Storage { namespace Types { using tCommandValue = int; using tStateValue = int; using tEventValue = QVariant; } }
They are registered with qRegisterMetaType:
qRegisterMetaType<Storage::Types::tCommandValue>(); qRegisterMetaType<Storage::Types::tEventValue>(); qRegisterMetaType<Storage::Types::tStateValue>();
The usage in signal/slots:
void addEvent(const IDs::Events id, const Types::tEventValue value);
Now when using no Q_DECLARE_METATYPE, i get this error:
QObject::connect: Cannot queue arguments of type 'Storage::Types::tStateValue' (Make sure 'Storage::Types::tStateValue' is registered using qRegisterMetaType().)
When i add the types with Q_DECLARE_METATYPE in my header file, i get a compile error:
// ... Q_DECLARE_METATYPE(Storage::Types::tCommandValue); Q_DECLARE_METATYPE(Storage::Types::tEventValue); Q_DECLARE_METATYPE(Storage::Types::tStateValue); // <--- Error: redefinition of 'QMetaTypeId<int>'
Maybe someone has an idea.
Thanks!Best regards,
Kevin -
Found a solution. For POD-types that are already registered Q_DECLARE_METATYPE is not needed.
qRegisterMetaType is still needed but needs an additional type name:qRegisterMetaType<Storage::Types::tStateValue>("Storage::Types::tStateValue"); qRegisterMetaType<Storage::Types::tEventValue>("Storage::Types::tEventValue"); qRegisterMetaType<Storage::Types::tCommandValue>("Storage::Types::tCommandValue");
-
Hi,
The "named" version is needed because you are not declaring a new type but in fact you are declaring an allias.