From ca311cf1ab189ccf91c2835393c71743a3f23fdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20K=C3=B6hne?= Date: Sat, 12 Mar 2022 11:05:12 +0100 Subject: Use QDebugStateSaver in example operator<<() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This makes sure that changes in the debug stream - like nospace() - do not "leak". Also use the example snippet in QDebugStateSaver documentation. Change-Id: I934976d2c7c2335abfec68c763526a5cbb0e6f1e Reviewed-by: Jörg Bornemann (cherry picked from commit 9f5f64e9dbd916679bd881ee0331da9a3dacac32) Reviewed-by: Qt Cherry-pick Bot --- examples/corelib/tools/customtype/message.cpp | 3 ++- src/corelib/io/qdebug.cpp | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/examples/corelib/tools/customtype/message.cpp b/examples/corelib/tools/customtype/message.cpp index 948e323dbf..6b672099ae 100644 --- a/examples/corelib/tools/customtype/message.cpp +++ b/examples/corelib/tools/customtype/message.cpp @@ -60,6 +60,7 @@ Message::Message(const QString &body, const QStringList &headers) //! [custom type streaming operator] QDebug operator<<(QDebug dbg, const Message &message) { + QDebugStateSaver saver(dbg); QList pieces = message.body().split(u"\r\n", Qt::SkipEmptyParts); if (pieces.isEmpty()) dbg.nospace() << "Message()"; @@ -67,7 +68,7 @@ QDebug operator<<(QDebug dbg, const Message &message) dbg.nospace() << "Message(" << pieces.first() << ")"; else dbg.nospace() << "Message(" << pieces.first() << " ...)"; - return dbg.maybeSpace(); + return dbg; } //! [custom type streaming operator] diff --git a/src/corelib/io/qdebug.cpp b/src/corelib/io/qdebug.cpp index a7ce66ad14..4956b2a59c 100644 --- a/src/corelib/io/qdebug.cpp +++ b/src/corelib/io/qdebug.cpp @@ -961,6 +961,10 @@ QDebug &QDebug::resetFormat() so that using << Qt::hex in a QDebug operator doesn't affect other QDebug operators. + QDebugStateSaver is typically used in the implementation of an operator<<() for debugging: + + \snippet tools/customtype/message.cpp custom type streaming operator + \since 5.1 */ -- cgit v1.2.3