Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid duplicate declarations of Q_DECLARE_METATYPE<aCommonType>

Tags:

c++

qt

qvariant

My project consists of an app that links to two static libraries. Each of the libraries declares Q_DECLARE_METATYPE< QUuid >, in order to use QUuid with QVariant, which leads to a 'redefinition of struct QMetaTypeId< QUuid >' error.

What is the correct way to go about this while keeping the ability to use each library on its own in different projects?

like image 869
szayat Avatar asked Jun 29 '12 19:06

szayat


1 Answers

As a workaround you can call the Q_DECLARE_METATYPE macro from the implementation files that need it instead of calling it from the header files, or as the documentation suggests, call it from private headers in each library.

But because QUuid stores its content as a QByteArray which is already supported by QVariant, you don't need to use Q_DECLARE_METATYPE to do that (from Qt 4.8 only):

// QVariant variant;
// QUuid uuid;
variant = uuid.toByteArray();
uuid = variant.toByteArray();

or the same thing, but a little less efficiently, with QString (before Qt 4.8):

variant = uuid.toString();
uuid = variant.toString();

And since QVariant will implicitly convert between QString and QByteArray, you can mix toString and toByteArray without any problem.

like image 184
alexisdm Avatar answered Oct 24 '22 05:10

alexisdm