Avoid newline in qDebug()

sashoalm picture sashoalm · Nov 18, 2012 · Viewed 9.6k times · Source

Sometimes I want to output a single line in qDebug(), but with some conditional text, like

if (fontMetricsLeading < 0)
    qDebug() << "!!!";
qDebug() << fontMetricsLeading;

However, that would output them on 2 separate lines.

Is there a way to avoid appending a new line after each qDebug()?

Answer

sashoalm picture sashoalm · Nov 18, 2012

I just found a solution that seems to work. Reading the docs qDebug() returns a temporary QDebug object, which appends newline on destruction. It seems this temporary object can be stored in a temporary variable:

QDebug debug = qDebug();
if (fontMetricsLeading < 0)
    debug << "!!!";
debug << fontMetricsLeading;