PDA

View Full Version : indexOfEnumerator does not work for Q_ENUMS defined in a namespace



sandhyasapare
24th January 2014, 12:46
filename : test.h

namespace model
{
namespace create
{
enum sample
{
first =1,
second
};
}
}

SpeechEnums.h

#include <QObject>
#include <QString>

#include "test.h"
using namespace model::create;

#ifndef Q_MOC_RUN
namespace MyNS
#else
class MyNS
#endif
{
#if defined(Q_MOC_RUN)
Q_GADGET
Q_ENUMS(sample
)
#include "test.h"
public:
#endif

extern const QMetaObject staticMetaObject;

}

Q_DECLARE_METATYPE(sample)


==================================
in the cpp file

// get the QMetaEnum object
const QMetaObject &mo = MyNS::staticMetaObject;
int enum_index = mo.indexOfEnumerator("sample");
QMetaEnum metaEnum = mo.enumerator(enum_index);
QByteArray str = metaEnum.valueToKey(1);
qDebug() << "Value as str:" << str;

This does not work, I am not sure what I missed
str always returns empty

Please help