From 2cfabed1ff9c822458c467ff072e7a3a6c0fbf3d Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 1 Jul 2022 17:05:36 +0200 Subject: [PATCH] Long live QDebug op<< QMetaType! It's needed in QtHttpServer. [ChangeLog][QtCore][QDebug] Can now stream QMetaType. [ChangeLog][QtCore][QMetaType] Can now be streamed through QDebug. Change-Id: I974d77d678137715472a3907ab1e50ba2dbaa087 Reviewed-by: Thiago Macieira --- src/corelib/kernel/qmetatype.cpp | 11 ++++++++++ src/corelib/kernel/qmetatype.h | 3 +++ tests/auto/corelib/io/qdebug/tst_qdebug.cpp | 24 +++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/src/corelib/kernel/qmetatype.cpp b/src/corelib/kernel/qmetatype.cpp index 780ae10396..2760a67e8a 100644 --- a/src/corelib/kernel/qmetatype.cpp +++ b/src/corelib/kernel/qmetatype.cpp @@ -1703,6 +1703,17 @@ void QMetaType::unregisterConverterFunction(QMetaType from, QMetaType to) #ifndef QT_NO_DEBUG_STREAM +/*! + \fn QDebug QMetaType::operator<<(QDebug d, QMetaType m) + \since 6.5 + Writes the QMetaType \a m to the stream \a d, and returns the stream. +*/ +QDebug operator<<(QDebug d, QMetaType m) +{ + const QDebugStateSaver saver(d); + return d.nospace() << "QMetaType(" << m.name() << ")"; +} + /*! Streams the object at \a rhs to the debug stream \a dbg. Returns \c true on success, otherwise false. diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index 71153c3e8f..2e97e1892d 100644 --- a/src/corelib/kernel/qmetatype.h +++ b/src/corelib/kernel/qmetatype.h @@ -491,6 +491,9 @@ public: friend bool operator!=(QMetaType a, QMetaType b) { return !(a == b); } #ifndef QT_NO_DEBUG_STREAM +private: + friend Q_CORE_EXPORT QDebug operator<<(QDebug d, QMetaType m); +public: bool debugStream(QDebug& dbg, const void *rhs); bool hasRegisteredDebugStreamOperator() const; diff --git a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp index 400625ba7d..7be4c92002 100644 --- a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp +++ b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp @@ -14,8 +14,12 @@ #include #include #include +#include + +using namespace Qt::StringLiterals; static_assert(QTypeTraits::has_ostream_operator_v); +static_assert(QTypeTraits::has_ostream_operator_v); static_assert(QTypeTraits::has_ostream_operator_v>); static_assert(QTypeTraits::has_ostream_operator_v>); struct NonStreamable {}; @@ -54,6 +58,7 @@ private slots: void stateSaver() const; void veryLongWarningMessage() const; void qDebugQChar() const; + void qDebugQMetaType() const; void qDebugQString() const; void qDebugQStringView() const; void qDebugQUtf8StringView() const; @@ -428,6 +433,25 @@ void tst_QDebug::qDebugQChar() const } +void tst_QDebug::qDebugQMetaType() const +{ + QString file, function; + int line = 0; + MessageHandlerSetter mhs(myMessageHandler); + { + QDebug d = qDebug(); + d << QMetaType::fromType() << QMetaType::fromType(); + } +#ifndef QT_NO_MESSAGELOGCONTEXT + file = __FILE__; line = __LINE__ - 4; function = Q_FUNC_INFO; +#endif + QCOMPARE(s_msgType, QtDebugMsg); + QCOMPARE(s_msg, R"(QMetaType(int) QMetaType(QString))"_L1); + QCOMPARE(QString::fromLatin1(s_file), file); + QCOMPARE(s_line, line); + QCOMPARE(QString::fromLatin1(s_function), function); +} + void tst_QDebug::qDebugQString() const { /* Use a basic string. */