[Interest] use old enum with Q_ENUM

Hamish Moffatt hamish at risingsoftware.com
Thu Jul 14 04:04:06 CEST 2016


I've got a lot of code using old enums declared outside of classes, 
which I would now like to use with Q_ENUM, so that I can use them in 
object properties and access them with QMetaEnum.

However, Q_ENUM only works on enums declared inside classes. Moving the 
enum definitions would require me to update tens of thousands of lines 
of code, so I'm trying to find a way to alias them.


The following doesn't work (on Qt 5.5.1 or 5.6); it compiles, but 
object->metaObject()->enumeratorCount() reports there are none. The enum 
values are not in the MOC output.

#include <QObject>

enum OldEnum
{
     VAL1,
     VAL2,
     VAL3
};

class NewEnumWrapper : public QObject
{
     Q_OBJECT

public:
     using Enum = ::OldEnum; // also tried typedef, no change
     Q_ENUM(Enum)
};


The only thing I've found that works is to put the enum inside the 
class, then alias the type and all the values individually back to the 
global namespace. This is pretty ugly..

class NewEnumWrapper : public QObject
{
     Q_OBJECT

public:
     enum NewEnumType { V1, V2 };
     Q_ENUM(NewEnumType)
};

typedef NewEnumWrapper::NewEnumType NewEnum;
const auto V1 = NewEnum::V1;
const auto V2 = NewEnum::V2;



Any other ideas?

thanks,
Hamish



More information about the Interest mailing list