summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorOlivier Goffart <ogoffart@woboq.com>2011-12-10 10:58:09 +0100
committerOlivier Goffart (Woboq GmbH) <ogoffart@woboq.com>2015-02-05 14:28:27 +0000
commiteb4f183127cb5c73fe84adc296fbc03fa8d61523 (patch)
tree8710d898e1126f2d6abe07ce89e8bfd3d311836b /src
parent9b72bcfd608817509d062f0971212cccfbe3d285 (diff)
Add a qDebug operator overload to handle registered enum
So if you stream enum type into qDebug, it will show the name of the enum value instead of the int Change-Id: Iec5e826623353560319890d3e7c4ab97d0645f4a Reviewed-by: Marc Mutz <marc.mutz@kdab.com> Reviewed-by: Shawn Rutledge <shawn.rutledge@digia.com>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/kernel/qobject.cpp14
-rw-r--r--src/corelib/kernel/qobject.h10
2 files changed, 24 insertions, 0 deletions
diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp
index be6441933f..0a8369acbf 100644
--- a/src/corelib/kernel/qobject.cpp
+++ b/src/corelib/kernel/qobject.cpp
@@ -4105,6 +4105,20 @@ QDebug operator<<(QDebug dbg, const QObject *o)
dbg << ')';
return dbg;
}
+
+QDebug qt_QMetaEnum_debugOperator(QDebug &dbg, int value, const QMetaObject *meta, const char *name)
+{
+ QDebugStateSaver saver(dbg);
+ QMetaEnum me = meta->enumerator(meta->indexOfEnumerator(name));
+ const char *key = me.valueToKey(value);
+ dbg.nospace() << meta->className() << "::" << name << '(';
+ if (key)
+ dbg << key;
+ else
+ dbg << value;
+ dbg << ')';
+ return dbg;
+}
#endif
/*!
diff --git a/src/corelib/kernel/qobject.h b/src/corelib/kernel/qobject.h
index 78f913acf8..20ff9981b1 100644
--- a/src/corelib/kernel/qobject.h
+++ b/src/corelib/kernel/qobject.h
@@ -547,6 +547,16 @@ template <class T> inline const char * qobject_interface_iid()
#ifndef QT_NO_DEBUG_STREAM
Q_CORE_EXPORT QDebug operator<<(QDebug, const QObject *);
+Q_CORE_EXPORT QDebug qt_QMetaEnum_debugOperator(QDebug&, int value, const QMetaObject *meta, const char *name);
+
+template<typename T>
+typename QtPrivate::QEnableIf<QtPrivate::IsQEnumHelper<T>::Value , QDebug>::Type
+operator<<(QDebug &dbg, T value)
+{
+ const QMetaObject *obj = qt_getEnumMetaObject(value);
+ const char *name = qt_getEnumName(value);
+ return qt_QMetaEnum_debugOperator(dbg, typename QFlags<T>::Int(value), obj, name);
+}
#endif
class QSignalBlocker