diff --git a/src/corelib/doc/snippets/code/src_corelib_io_qdebug.cpp b/src/corelib/doc/snippets/code/src_corelib_io_qdebug.cpp index 14e72e99dd..5154dc5d68 100644 --- a/src/corelib/doc/snippets/code/src_corelib_io_qdebug.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_io_qdebug.cpp @@ -93,3 +93,8 @@ ba = QByteArray("a\0b", 3); qDebug() << ba // prints: "\a\x00""b" //! [1] + +//! [toString] + QTRY_VERIFY2(list.isEmpty(), qPrintable(QString::fromLatin1( + "Expected list to be empty, but it has the following items: %1")).arg(QDebug::toString(list))); +//! [toString] diff --git a/src/corelib/doc/src/includes/qdebug-toString.qdocinc b/src/corelib/doc/src/includes/qdebug-toString.qdocinc new file mode 100644 index 0000000000..4602a10511 --- /dev/null +++ b/src/corelib/doc/src/includes/qdebug-toString.qdocinc @@ -0,0 +1,7 @@ +Streams \a object into a QDebug instance that operates on a string, +and then returns that string. + +This function is useful for cases where you need the textual representation +of an object for debugging, but cannot use \c {operator<<}. For example: + +\snippet code/src_corelib_io_qdebug.cpp toString diff --git a/src/corelib/io/qdebug.cpp b/src/corelib/io/qdebug.cpp index d13e94e096..11f2b9f3c9 100644 --- a/src/corelib/io/qdebug.cpp +++ b/src/corelib/io/qdebug.cpp @@ -713,6 +713,20 @@ QDebug &QDebug::resetFormat() \internal */ +/*! + \fn template QString QDebug::toString(const T &object) + \since 6.0 + + \include qdebug-toString.qdocinc +*/ + +/*! + \fn template QString QDebug::toString(const T *object) + \since 6.0 + + \include qdebug-toString.qdocinc +*/ + /*! \fn template QDebug operator<<(QDebug debug, const QList &list) \relates QDebug diff --git a/src/corelib/io/qdebug.h b/src/corelib/io/qdebug.h index 2314b36bda..9a6abc1994 100644 --- a/src/corelib/io/qdebug.h +++ b/src/corelib/io/qdebug.h @@ -173,6 +173,24 @@ public: inline QDebug &operator<<(QTextStreamManipulator m) { stream->ts << m; return *this; } + + template + static QString toString(const T &object) + { + QString buffer; + QDebug stream(&buffer); + stream << object; + return buffer; + } + + template + static QString toString(const T *object) + { + QString buffer; + QDebug stream(&buffer); + stream << object; + return buffer; + } }; Q_DECLARE_SHARED(QDebug) diff --git a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp index 584e66a7db..7b9e614cf7 100644 --- a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp +++ b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp @@ -66,6 +66,7 @@ private slots: void resetFormat() const; void defaultMessagehandler() const; void threadSafety() const; + void toString() const; }; void tst_QDebug::assignment() const @@ -740,6 +741,28 @@ void tst_QDebug::threadSafety() const } } +void tst_QDebug::toString() const +{ + // By reference. + { + MyPoint point(3, 4); + QString expectedString; + QDebug stream(&expectedString); + stream << point; + QCOMPARE(QDebug::toString(point), expectedString); + } + + // By pointer. + { + QObject qobject; + qobject.setObjectName("test"); + QString expectedString; + QDebug stream(&expectedString); + stream << &qobject; + QCOMPARE(QDebug::toString(&qobject), expectedString); + } +} + // Should compile: instentiation of unrelated operator<< should not cause cause compilation // error in QDebug operators (QTBUG-47375) class TestClassA {};