mirror of https://github.com/qt/qtbase.git
Use QDebugStateSaver in example operator<<()
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 <joerg.bornemann@qt.io>
(cherry picked from commit 9f5f64e9db
)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
07840819fc
commit
325d237899
|
@ -60,6 +60,7 @@ Message::Message(const QString &body, const QStringList &headers)
|
||||||
//! [custom type streaming operator]
|
//! [custom type streaming operator]
|
||||||
QDebug operator<<(QDebug dbg, const Message &message)
|
QDebug operator<<(QDebug dbg, const Message &message)
|
||||||
{
|
{
|
||||||
|
QDebugStateSaver saver(dbg);
|
||||||
QList<QStringView> pieces = message.body().split(u"\r\n", Qt::SkipEmptyParts);
|
QList<QStringView> pieces = message.body().split(u"\r\n", Qt::SkipEmptyParts);
|
||||||
if (pieces.isEmpty())
|
if (pieces.isEmpty())
|
||||||
dbg.nospace() << "Message()";
|
dbg.nospace() << "Message()";
|
||||||
|
@ -67,7 +68,7 @@ QDebug operator<<(QDebug dbg, const Message &message)
|
||||||
dbg.nospace() << "Message(" << pieces.first() << ")";
|
dbg.nospace() << "Message(" << pieces.first() << ")";
|
||||||
else
|
else
|
||||||
dbg.nospace() << "Message(" << pieces.first() << " ...)";
|
dbg.nospace() << "Message(" << pieces.first() << " ...)";
|
||||||
return dbg.maybeSpace();
|
return dbg;
|
||||||
}
|
}
|
||||||
//! [custom type streaming operator]
|
//! [custom type streaming operator]
|
||||||
|
|
||||||
|
|
|
@ -952,6 +952,10 @@ QDebug &QDebug::resetFormat()
|
||||||
so that using << Qt::hex in a QDebug operator doesn't affect other QDebug
|
so that using << Qt::hex in a QDebug operator doesn't affect other QDebug
|
||||||
operators.
|
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
|
\since 5.1
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue