代码改变世界

Qt 中Enum进行反射来做Enum to QString

2010-11-24 22:37  Loning  阅读(1397)  评论(3编辑  收藏  举报

i assume you want to retrieve a QMetaEnum for a particular enum which is registered with Q_ENUMS.

let {class} denote the QObject-derived class which contains the enum, and let {enumName} denote the name of the enum. the following code gets you the enum's QMetaEnum:

QMetaObject metaObject = {class}.staticMetaObject;
QMetaEnum metaEnum = metaObject.enumerator( metaObject.indexOfEnumerator( {enumName} ) );

this compiles despite the fact that neither QMetaObject nor QMetaEnum define operator=(). i'm not sure why--they must be defined somewhere else, but i can't find them.