[Development] Why does Q_ENUM() need qRegisterMetaType()?

Olivier Goffart olivier at woboq.com
Thu Jul 4 16:12:17 CEST 2019

On 04.07.19 12:43, Tom Isaacson wrote:
>> The problem is that moc only generates the code that calls qRegisterMetaType if it sees Q_DECLARE_METATYPE. Actually, it does a bit more than that. It could as well do it for Q_ENUM type, just not implemented yet.
>> We should consider doing it for all the types used in signals/slots/properties.
>>   But it does not do it because that could be a potential breaking change for forward declared types.
>> Maybe something for Qt6.
> It's very confusing. You said yourself on https://woboq.com/blog/q_enum.html that "These enums are automatically declared as a QMetaTypes (no need to add them in Q_DECLARE_METATYPE anymore)." So Q_ENUM replaces Q_DECLARE_METATYPE and provides additional functionality but requires you to add an additional call to qRegisterMetaType? That seems backward.

I know, the whole story behind Q_DECLARE_METATYPE and qRegisterMetaType  is a 
bit confusing.

Regarding Q_ENUM specifically, it could be fixed in moc, to be in the list of 
the type that are declared.

> Can I use Q_DECLARE_METATYPE and Q_ENUM and avoid qRegisterMetaType?

Yes, youu can use both Q_DECLARE_METATYPE and Q_ENUM

More information about the Development mailing list