mdavidjohnson
mdavidjohnson

Reputation: 119

Qt 5.7 \n behavior

In a Qt 5.7 Console Application:

#include <QCoreApplication>
#include <QtDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QString s = "MDJ\nTest.\n";
    qDebug() << "MDJ\nTest.\n";
    qDebug() << s;

    /* Prints:
        MDJ
        Test.

        MDJ\nTest.\n
    */

    return a.exec();
}

i.e. the \n works as expected in a direct debug print, but is treated as just two plain characters when debug printing a (supposedly identical content) variable.

I'm encountering similar problems in Qt 5.7 Widget Applications as well.

I've searched the documentation, stackoverflow, and Qt Centre and I've been unable to discover what I'm doing wrong.

Could somebody please point me to a solution for this?

Upvotes: 0

Views: 51

Answers (1)

Patrick Artner
Patrick Artner

Reputation: 51623

The docs give you hints:

<< QString()

Normally, QDebug prints the string inside quotes and transforms non-printable characters to their Unicode values (\u1234).
To print non-printable characters without transformation, enable the noquote() functionality. Note that some QDebug backends might not be 8-bit clean.

vs.

<< const char*

Writes the '\0'-terminated string, s, to the stream and returns a reference to the stream. The string is never quoted nor transformed to the output, but note that some QDebug backends might not be 8-bit clean.

Solution: qDebug().noquote() << "some\nspecial\nchars\n\tincluded"

Upvotes: 2

Related Questions