diff options
author | Marc Mutz <marc.mutz@qt.io> | 2022-07-01 17:05:36 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2022-07-09 06:40:08 +0200 |
commit | 2cfabed1ff9c822458c467ff072e7a3a6c0fbf3d (patch) | |
tree | d026d9135350af18d767f27481ac48534a5487b2 | |
parent | f2c9f8c83781fea73b148bf24472f1d3d191c26e (diff) |
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 <thiago.macieira@intel.com>
-rw-r--r-- | src/corelib/kernel/qmetatype.cpp | 11 | ||||
-rw-r--r-- | src/corelib/kernel/qmetatype.h | 3 | ||||
-rw-r--r-- | tests/auto/corelib/io/qdebug/tst_qdebug.cpp | 24 |
3 files changed, 38 insertions, 0 deletions
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 @@ -1704,6 +1704,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. \since 5.2 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 <QLine> #include <QMimeType> #include <QMimeDatabase> +#include <QMetaType> + +using namespace Qt::StringLiterals; static_assert(QTypeTraits::has_ostream_operator_v<QDebug, int>); +static_assert(QTypeTraits::has_ostream_operator_v<QDebug, QMetaType>); static_assert(QTypeTraits::has_ostream_operator_v<QDebug, QList<int>>); static_assert(QTypeTraits::has_ostream_operator_v<QDebug, QMap<int, QString>>); 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<int>() << QMetaType::fromType<QString>(); + } +#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. */ |