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>
bb10
Marc Mutz 2022-07-01 17:05:36 +02:00
parent f2c9f8c837
commit 2cfabed1ff
3 changed files with 38 additions and 0 deletions

View File

@ -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.

View File

@ -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;

View File

@ -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. */