I am trying to use the Qt reflection for converting enum to QString.
Here is the part of code:
class ModelApple
{
Q_GADGET
Q_ENUMS(AppleType)
You need to use Q_ENUM macro, which registers an enum type with the meta-object system.
enum AppleType {
Big,
Small
};
Q_ENUM(AppleType)
And now you can use the QMetaEnum class to access meta-data about an enumerator.
QMetaEnum metaEnum = QMetaEnum::fromType();
qDebug() << metaEnum.valueToKey(ModelApple::Big);
Here is a generic template for such utility:
template
std::string QtEnumToString (const QEnum value)
{
return std::string(QMetaEnum::fromType().valueToKey(value));
}