如何在QString.arg()中使用我的枚举? [英] How can I use my enum in QString.arg()?
问题描述
我的枚举被声明为 Q_ENUM
宏,因此与 qDebug()
一起使用时,它将打印枚举字段的名称。因为我使用的是QT 5.5)而不是其值。我想对 QString()。arg()
做同样的事情,所以我对 Q_DECLARE_METATYPE()$ c $声明了相同的枚举。 c>宏,但它也不起作用,并给出以下错误。
My enum is declared as Q_ENUM
macro so it print the enum field's name when using with qDebug()
(as I'm using QT 5.5) instead of its value. I'd like to do the same with QString().arg()
so I declared that same enum with Q_DECLARE_METATYPE()
macro but it didn't work either and give the below error.
代码:
qDebug() << QString("s = %1").arg(myClass::myEnum::ok);
错误:
错误:没有匹配函数调用
'QString :: arg(myClass :: myEnum)'
error: no matching function for call to 'QString::arg(myClass::myEnum)'
我该如何解决?
推荐答案
Q_ENUM不能直接转换为某种字符串值,因此您必须使用QMetaEnum:
Q_ENUM does not provide a direct conversion to some kind of string value, so you would have to use QMetaEnum:
qDebug()<< QStringLiteral( s =%1)。arg(QMetaEnum :: fromType< MyClass :: Priority>()。valueToKey(static_cast< int>(myClass :: myEnum :: ok));
枚举类当然需要static_cast。
static_cast is of course necessary for enum class.
这篇关于如何在QString.arg()中使用我的枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!