QMetaEnum::fromType : 将Enum作为字符串输出

项目中的错误码使用的是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 representation

The 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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值