diff --git a/src/corelib/io/qdebug.h b/src/corelib/io/qdebug.h index d2243597241..6b81d9431f5 100644 --- a/src/corelib/io/qdebug.h +++ b/src/corelib/io/qdebug.h @@ -259,21 +259,21 @@ inline QDebug operator<<(QDebug debug, const QContiguousCache &cache) template inline QDebug operator<<(QDebug debug, const QFlags &flags) { - const bool oldSetting = debug.autoInsertSpaces(); - debug.nospace() << "QFlags("; + QDebugStateSaver saver(debug); + debug.resetFormat(); + debug.nospace() << "QFlags(" << hex << showbase; bool needSeparator = false; for (uint i = 0; i < sizeof(T) * 8; ++i) { if (flags.testFlag(T(1 << i))) { if (needSeparator) - debug.nospace() << '|'; + debug << '|'; else needSeparator = true; - debug.nospace() << "0x" << QByteArray::number(typename QFlags::Int(1) << i, 16).constData(); + debug << (typename QFlags::Int(1) << i); } } debug << ')'; - debug.setAutoInsertSpaces(oldSetting); - return debug.maybeSpace(); + return debug; } QT_END_NAMESPACE diff --git a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp index 767fde7f6cd..764c928d76e 100644 --- a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp +++ b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp @@ -63,6 +63,7 @@ private slots: void qDebugQStringRef() const; void qDebugQLatin1String() const; void qDebugQByteArray() const; + void qDebugQFlags() const; void textStreamModifiers() const; void resetFormat() const; void defaultMessagehandler() const; @@ -395,6 +396,28 @@ void tst_QDebug::qDebugQByteArray() const QCOMPARE(QString::fromLatin1(s_function), function); } +enum TestEnum { + Flag1 = 0x1, + Flag2 = 0x10 +}; + +Q_DECLARE_FLAGS(TestFlags, TestEnum) + +void tst_QDebug::qDebugQFlags() const +{ + QFlags flags(Flag1 | Flag2); + + MessageHandlerSetter mhs(myMessageHandler); + { qDebug() << flags; } + QString file = __FILE__; int line = __LINE__ - 1; QString function = Q_FUNC_INFO; + QCOMPARE(s_msgType, QtDebugMsg); + QCOMPARE(s_msg, QString::fromLatin1("QFlags(0x1|0x10)")); + QCOMPARE(QString::fromLatin1(s_file), file); + QCOMPARE(s_line, line); + QCOMPARE(QString::fromLatin1(s_function), function); + +} + void tst_QDebug::textStreamModifiers() const { MessageHandlerSetter mhs(myMessageHandler);