Qt enum使用總結

1、enum 自省spa

const QMetaObject &mo = [ClassName]::staticMetaObject;
int index = mo.indexOfEnumerator("[EnumName]");
QMetaEnum me = mo.enumerator(index);

若 enum 定義在當前類中,則能夠簡寫爲blog

int index = staticMetaObject.indexOfEnumerator("[EnumName]");
QMetaEnum me = staticMetaObject.enumerator(index);

2、enum 轉換ci

一、 enum <-> intstring

enum Color { Red, White, Blue};
Color c = (Color)1;   //c=White
int i = Red;       //i=0

二、 enum <-> stringit

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 ;
相關文章
相關標籤/搜索