一、enum 自省
const QMetaObject &mo = [ClassName]::staticMetaObject; int index = mo.indexOfEnumerator("[EnumName]"); QMetaEnum me = mo.enumerator(index);
若 enum 定义在当前类中,则可以简写为
int index = staticMetaObject.indexOfEnumerator("[EnumName]"); QMetaEnum me = staticMetaObject.enumerator(index);
二、enum 转换
1、 enum <-> int
enum Color { Red, White, Blue};
Color c = (Color)1; //c=White
int i = Red; //i=0
2、 enum <-> string
enum Color { Red, White, Blue};
QString s = "White";
int idx = mo.indexOfEnumerator("Color");
QMetaEnum iEnum = mo.enumerator(idx);
int val = iEnum.keysToValue(s.toAscii());
Color c = (Color)val ;