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>
This commit is contained in:
parent
9b72bcfd60
commit
eb4f183127
@ -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
|
||||
|
||||
/*!
|
||||
|
@ -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
|
||||
|
@ -225,6 +225,7 @@ private slots:
|
||||
void signal();
|
||||
void signalIndex_data();
|
||||
void signalIndex();
|
||||
void enumDebugStream();
|
||||
|
||||
signals:
|
||||
void value6Changed();
|
||||
@ -1206,7 +1207,6 @@ void tst_QMetaObject::metaMethod()
|
||||
QCOMPARE(str, QString("foo"));
|
||||
QCOMPARE(ret, QString("bar"));
|
||||
|
||||
|
||||
QtTestObject obj;
|
||||
QString t1("1"); QString t2("2"); QString t3("3"); QString t4("4"); QString t5("5");
|
||||
QString t6("6"); QString t7("7"); QString t8("8"); QString t9("9"); QString t10("X");
|
||||
@ -1412,5 +1412,12 @@ void tst_QMetaObject::signalIndex()
|
||||
SignalTestHelper::signalIndex(mm));
|
||||
}
|
||||
|
||||
void tst_QMetaObject::enumDebugStream()
|
||||
{
|
||||
QTest::ignoreMessage(QtDebugMsg, "hello MyNamespace::MyClass::MyEnum(MyEnum2) world ");
|
||||
MyNamespace::MyClass::MyEnum e = MyNamespace::MyClass::MyEnum2;
|
||||
qDebug() << "hello" << e << "world";
|
||||
}
|
||||
|
||||
QTEST_MAIN(tst_QMetaObject)
|
||||
#include "tst_qmetaobject.moc"
|
||||
|
Loading…
x
Reference in New Issue
Block a user