项目中的错误码使用的是Enum类型,因此每次输出错误码为数字时,还需查看对应的错误类型,幸好Qt已经有QMetaEnum类,可以将Enum 输出为string,在看到错误提示时,一目了然。
QMetaEnum Class :The QMetaEnum class provides meta-data about an enumerator.
Use name() for the enumerator’s name. The enumerator’s keys (names of each enumerated item) are returned by key();
use keyCount() to find the number of keys. isFlag() returns whether the enumerator is meant to be used as a flag, meaning that its values can be combined using the OR operator.The conversion functions keyToValue(), valueToKey(), keysToValue(),
and valueToKeys() allow conversion between the integer representation
of an enumeration or set value and its literal representationThe scope() function returns the class scope this enumerator was
declared in.
QMetaEnum::fromType
enum class Reason : int {
Failure = -1,
Success = 0,
Memory_unavailable = 1,
Access_violation = 2,
Parameter_value_inappropriate = 3,
Access_not_allowed_with_current_role = 4,
Resource_not_found = 5,
Datablock_too_large = 6,
Operation_failed = 7,
Access_not_allowed_in_current_state = 8
};
Q_ENUM(Reason)
示例:
QMetaEnum metaEnum = QMetaEnum::fromType<CThreadMetaData::Reason>();
qDebug() << "errno:"<< metaEnum.valueToKey(ret);