Add pretty printer for Qt enums.

This commit is contained in:
Alan Briolat 2012-01-16 14:22:30 +00:00 committed by John Maguire
parent 0212a1fa91
commit 66cc5d432e
2 changed files with 18 additions and 0 deletions

View File

@ -35,6 +35,7 @@
#include <QXmlStreamReader>
#include <QtDebug>
#include <QtGlobal>
#include <QMetaEnum>
#if defined(Q_OS_UNIX)
# include <sys/statvfs.h>
@ -395,6 +396,17 @@ void ConsumeCurrentElement(QXmlStreamReader* reader) {
}
}
const char* EnumToString(const QMetaObject& meta, const char* name, int value) {
int index = meta.indexOfEnumerator(name);
if (index == -1)
return "[UnknownEnum]";
QMetaEnum metaenum = meta.enumerator(index);
const char* result = metaenum.valueToKey(value);
if (result == 0)
return "[UnknownEnumValue]";
return result;
}
} // namespace Utilities

View File

@ -30,6 +30,7 @@
class QIODevice;
class QMouseEvent;
class QXmlStreamReader;
class QMetaObject;
namespace Utilities {
QString PrettyTime(int seconds);
@ -81,6 +82,11 @@ namespace Utilities {
// file - whichever came first.
void ConsumeCurrentElement(QXmlStreamReader* reader);
// Shortcut for getting a Qt-aware enum value as a string.
// Pass in the QMetaObject of the class that owns the enum, the string name of
// the enum and a valid value from that enum.
const char* EnumToString(const QMetaObject& meta, const char* name, int value);
enum ConfigPath {
Path_Root,