Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Q_ENUM and Q_ENUMS

Tags:

c++

qt5

qmetatype

I just found multiple examples showing the usage of Q_ENUM and Q_ENUMS and looking into the definition of Q_ENUM showed me that it includes Q_ENUMS and other definitions.

I am not sure which one to write when using the enum in Q_PROPERTY, Qml/QtQuick, in signals/slots, QVariants and qDebug() output.

It seems like the Q_ENUM is the better one as it is defined using Q_ENUMS, but I'm just guessing here.

What exactly are the differences, why are there two at all and which one should be prefered?

like image 879
feedc0de Avatar asked Oct 11 '17 14:10

feedc0de


People also ask

What is enum in Qt?

An enumerated type, or enum, is a data type consisting of a set of named values called elements, members, enumeral, or enumerators of the type. Enums are incredibly useful when portraying status, options, or modes that are exclusive within a grouping.

How does QML define enum?

Using the enumeration Type in QMLThe enumeration type is a representation of a C++ enum type. It is not possible to refer to the enumeration type in QML itself; instead, the int or var types can be used when referring to enumeration values from QML code. See also QML Value Types and Enumeration Attributes.


1 Answers

The What's New in Qt 5.5 says:

Added Q_ENUM to replace Q_ENUMS, which allows to get a QMetaEnum at compile time using QMetaEnum::fromType. Such enums are now automatically registered as metatypes, and can be converted to strings within QVariant, or printed as string by qDebug().

like image 78
Andrii Avatar answered Oct 06 '22 19:10

Andrii