[Solved]Iterate over enum, using QMetaEnum
-
Lets say I have something like
@enum Letters
{ A, B };@How can I iterate over them and print each one as a string for example?
I do know about the existence of QMetaEnum, but I can not find a useful example of it.Thank you for your time.
-
Hi,
QMetaEnum is used in conjunction with Q_ENUMS. Is it the case of yours ?
-
Hi SGaist,
No I was not using Q_ENUMS, yet I am open to any possible suggestions as I have never dealt with anything similar before. -
After SGaist's hint I ended up with a possible solution
I first define on my class
@class SomeClass : public QObject
{
Q_OBJECT
Q_ENUMS(Letters)public:
enum Letters
{ A, B };...@
then I can simply do
@QMetaObject meta = SomeClass::staticMetaObject;
QMetaEnum tmpObj = meta.enumerator(0);
for (int i = 0; i < tmpObj.keyCount(); i++)
{
qDebug() << tmpObj.key(i);
}@If there are any better ways i'd be glad to know.
-
It depends on you use case. You can also have a simple function with a switch that returns a string