QDebug: Add getter/setter for auto-insert-spaces.

This is useful for inserting a string without space-handling, given that
dbg.nospace() followed by dbg.space() inserts a space.

It's also useful for QDebug operators for custom types, so that they
can disable space handling and then restore to whatever it was before
(rather than forcing it to space() mode).

Change-Id: I9d72e9ffbcbc581ed093168752c29af924405b33
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
David Faure 2012-07-27 13:53:16 +02:00 committed by Qt by Nokia
parent 54e3ce1705
commit 8cf7cf0cb9
3 changed files with 42 additions and 0 deletions

View File

@ -161,6 +161,24 @@
\sa space(), nospace() \sa space(), nospace()
*/ */
/*!
\fn bool QDebug::autoInsertSpaces()
Returns true if this QDebug instance will automatically insert spaces
between writes.
\since 5.0
*/
/*!
\fn void QDebug::setAutoInsertSpaces(bool b)
Enables automatic insertion of spaces between writes if \a b is true; otherwise
automatic insertion of spaces is disabled.
\since 5.0
*/
/*! /*!
\fn QDebug &QDebug::operator<<(QChar t) \fn QDebug &QDebug::operator<<(QChar t)

View File

@ -97,6 +97,9 @@ public:
inline QDebug &nospace() { stream->space = false; return *this; } inline QDebug &nospace() { stream->space = false; return *this; }
inline QDebug &maybeSpace() { if (stream->space) stream->ts << ' '; return *this; } inline QDebug &maybeSpace() { if (stream->space) stream->ts << ' '; return *this; }
bool autoInsertSpaces() const { return stream->space; }
void setAutoInsertSpaces(bool b) { stream->space = b; }
inline QDebug &operator<<(QChar t) { stream->ts << '\'' << t << '\''; return maybeSpace(); } inline QDebug &operator<<(QChar t) { stream->ts << '\'' << t << '\''; return maybeSpace(); }
inline QDebug &operator<<(bool t) { stream->ts << (t ? "true" : "false"); return maybeSpace(); } inline QDebug &operator<<(bool t) { stream->ts << (t ? "true" : "false"); return maybeSpace(); }
inline QDebug &operator<<(char t) { stream->ts << t; return maybeSpace(); } inline QDebug &operator<<(char t) { stream->ts << t; return maybeSpace(); }

View File

@ -52,6 +52,7 @@ private slots:
void warningWithoutDebug() const; void warningWithoutDebug() const;
void criticalWithoutDebug() const; void criticalWithoutDebug() const;
void debugWithBool() const; void debugWithBool() const;
void debugNoSpaces() const;
void veryLongWarningMessage() const; void veryLongWarningMessage() const;
void qDebugQStringRef() const; void qDebugQStringRef() const;
void qDebugQLatin1String() const; void qDebugQLatin1String() const;
@ -149,6 +150,26 @@ void tst_QDebug::debugWithBool() const
QCOMPARE(QString::fromLatin1(s_function), function); QCOMPARE(QString::fromLatin1(s_function), function);
} }
void tst_QDebug::debugNoSpaces() const
{
MessageHandlerSetter mhs(myMessageHandler);
{
QDebug d = qDebug();
QVERIFY(d.autoInsertSpaces());
d.setAutoInsertSpaces(false);
QVERIFY(!d.autoInsertSpaces());
d << " ";
d.setAutoInsertSpaces(true);
QVERIFY(d.autoInsertSpaces());
d << "foo";
d.nospace();
d << "key=" << "value";
d.space();
d << 1 << 2;
}
QCOMPARE(s_msg, QString::fromLatin1(" foo key=value 1 2 "));
}
void tst_QDebug::veryLongWarningMessage() const void tst_QDebug::veryLongWarningMessage() const
{ {
MessageHandlerSetter mhs(myMessageHandler); MessageHandlerSetter mhs(myMessageHandler);