Skip to content

Commit

Permalink
Use QDebugStateSaver in example operator<<()
Browse files Browse the repository at this point in the history
This makes sure that changes in the debug stream - like nospace() -
do not "leak". Also use the example snippet in QDebugStateSaver
documentation.

Pick-to: 6.2 6.3
Change-Id: I934976d2c7c2335abfec68c763526a5cbb0e6f1e
Reviewed-by: Jörg Bornemann <[email protected]>
  • Loading branch information
kkoehne committed Mar 16, 2022
1 parent 21bffee commit 9f5f64e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion examples/corelib/tools/customtype/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@ Message::Message(const QString &body, const QStringList &headers)
//! [custom type streaming operator]
QDebug operator<<(QDebug dbg, const Message &message)
{
QDebugStateSaver saver(dbg);
QList<QStringView> pieces = message.body().split(u"\r\n", Qt::SkipEmptyParts);
if (pieces.isEmpty())
dbg.nospace() << "Message()";
else if (pieces.size() == 1)
dbg.nospace() << "Message(" << pieces.first() << ")";
else
dbg.nospace() << "Message(" << pieces.first() << " ...)";
return dbg.maybeSpace();
return dbg;
}
//! [custom type streaming operator]

Expand Down
4 changes: 4 additions & 0 deletions src/corelib/io/qdebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand Down

0 comments on commit 9f5f64e

Please sign in to comment.