PDA

View Full Version : enum to QString



bibhukalyana
21st May 2013, 13:29
Hi everyone,
I am trying to get the string value of enum.
My classa.h:


class classA : public QObject
{
Q_OBJECT
Q_ENUMS(errType)
public:
enum errType{SUCCESS,ERROR};
classA();

};


main.cpp:


int index = classA::staticMetaObject.indexOfEnumerator("errType");
qDebug()<<index;

const QMetaObject & mo = classA::staticMetaObject;
qDebug() << "Available enums in" << mo.className() << ":" << mo.enumeratorCount();


If i will compile with Q_OBJECT then undefined reference to classA::staticMetaObject and undefined reference to vtable for classA built error will come.
If i will compile without Q_OBJECT then compilation will be fine.But index will be -1 and enumetator count will be 0.

Please tell how i will register the enum and get the string value of enum.

Thanks.

wysota
21st May 2013, 14:46
Rerun qmake.

bibhukalyana
22nd May 2013, 05:20
Thanks it is working.

Can i register a global enum using Q_ENUMS ?

wysota
22nd May 2013, 06:44
What do you consider a "global enum"?

ChrisW67
22nd May 2013, 08:33
I think the OP means an enum declared outside a class

The enum must be inside a class with Q_OBJECT or Q_GADGET macros or the supporting infrastructure is not there for QMetaObject and friends.