PDA

View Full Version : Q_ENUMS outside of the class?



stefan
10th October 2013, 13:16
Hi all!

I want to use type safe enumeration like in C++11


enum class Fruit
{
apple,
orange,
pear
}

and use it in more (then one) classes


class FruitCake
{
Fruit baseFruit; //use: baseFruit = Fruit::apple;
}
class Person
{
Fruit favoriteFruit; //use: favoriteFruit = Fruit::apple;
}

if(john.favoriteFruit == myCake.baseFruit)
john.eat(myCake)

OK, this can be done easily. But I want all the goodies that Q_ENUMS provides (The meta data of enum).
How can i do that?

Sorry for non-imaginative example ;)

anda_skoa
11th October 2013, 09:54
Q_ENUMS is processed by MOC, so it needs to be in a class that is QObject derived and has a Q_OBJECT marker.

Cheers,
_

ChrisW67
11th October 2013, 20:35
Or you can try Q_GADGET on your enum class
http://stackoverflow.com/questions/6798575/c-qt-reflection-with-copy-and-assignment

Q_GADGET is only mentioned in passing in the QObjectvdocs. I suspect it will not well in the enum class.

stefan
12th October 2013, 10:04
Thanks for your answers.
I think that I will adjust my design and go with Qt style (QObject class with Q_ENUMS) :)