How to print QString containing line breaks using qDebug

10,394

qDebug() is meant for debugging purposes, so it escapes non-printable characters and adds quotes when printing QString, QByteArray, QChar arguments.

Try using qDebug().noquote() as this disables escaping non-printable characters, like this:

QString output("test1\ntest2");
qDebug().noquote() << output;

Warning:

qDebug(), qInfo(), qWarning(), qCritical() and qFatal() are all provided for debugging purposes. They are not meant to display something to the user in production code.

Please, don't use these methods unless you are printing/logging some debug statements.

Share:
10,394
Paddre
Author by

Paddre

Updated on June 18, 2022

Comments

  • Paddre
    Paddre 6 months

    This code:

    QString output("test1\ntest2");
    qDebug() << output;
    

    leads to this output:

    "test1\ntest2"
    

    What I want is:

    "test1
    test2"
    

    So how can I use qDebug() (and similar output functions) to print a QString containing line break characters in multiple lines?

  • MrEricSir
    MrEricSir over 6 years
    Beat me to it! I was about to post the same answer.
  • Paddre
    Paddre over 6 years
    Worked perfectly well :-) Thanks to both of you
  • Mike
    Mike over 6 years
    @Paddre , Just a side note: qDebug is meant for debugging purposes, you shouldn't rely on it for your program's output.
  • Paddre
    Paddre over 6 years
    Actually I'm using qWarning() but since I guess it behaves in a similar way and more people might be more familiat wirh qDebug() I chose it as an example.
  • Mike
    Mike over 6 years
    @Paddre , qDebug(), qWarning(), qCritical() and qFatal() are all provided for debugging purposes. They are not meant to output something to the user in production code. Please, don't use these methods unless you are printing/logging some debug statements.
  • Paddre
    Paddre over 6 years
    Apparently I misinterpreted the semantics behind these functions. I'll consider your remarks. However, this doesn't change the intention behind my question as I really need some readable multi-line debug output ;-)
  • Fryz
    Fryz about 4 years
    Could you explain why you consider that all qDebug's messages are not meant to display something to the user in production code ?
  • Mike
    Mike about 4 years
    @Scab, I have already provided the relevant documentation links in my answer. qDebug() is better suited to log debug messages and Qt provides utility functions for this purpose (e.g. qInstallMessageHandler, qSetMessagePattern, ...). qDebug() can not even print to stdout.
  • bliako
    bliako almost 3 years
    @Mike I heard you (re: qDebug() is better suited to log debug messages) but who knows maybe I want to print a newline in my log file? Maybe two newlines. Is there a newline police out there? The question is not whether Qt decided to adopt this policy, the question is why all this zeal to spread conformity. It takes us all years backwards. On to the caves people and newlineless log files.
  • Mike
    Mike almost 3 years
    @bliako, the question is not about generally printing newlines using qDebug(). It is rather about printing a QString that contains newlines without escaping them. I am not aware of any standard for log files, but generally you would manage the layout of your file in a single place (e.g. see here), and you would want to see all the characters of a string when logging it into a log file. Formatting characters in the logged string are usually escaped to avoid messing up the formatting of your file and to make sure white space characters are easy to spot.
  • Mike
    Mike almost 3 years
    I believe that these are the reasons for qDebug's default behavior, and, in case you need so, you always have the option to use .noquote() to disable it...