11

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?

AAEM
  • 1,657
  • 1
  • 12
  • 25
Paddre
  • 728
  • 7
  • 17

1 Answers1

25

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.

Mike
  • 7,220
  • 1
  • 23
  • 39
  • 1
    Beat me to it! I was about to post the same answer. – MrEricSir Sep 18 '16 at 18:46
  • Worked perfectly well :-) Thanks to both of you – Paddre Sep 18 '16 at 18:56
  • @Paddre , Just a side note: `qDebug` is meant for debugging purposes, you shouldn't rely on it for your program's output. – Mike Sep 18 '16 at 19:01
  • 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. – Paddre Sep 18 '16 at 19:07
  • 1
    @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. – Mike Sep 18 '16 at 21:51
  • 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 ;-) – Paddre Sep 19 '16 at 11:30
  • Could you explain why you consider that all qDebug's messages are not meant to display something to the user in production code ? – Fryz Nov 09 '18 at 08:41
  • @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`](https://doc.qt.io/qt-5/qtglobal.html#qInstallMessageHandler), [`qSetMessagePattern`](https://doc.qt.io/qt-5/qtglobal.html#qSetMessagePattern), ...). `qDebug()` [can not even print to stdout](https://stackoverflow.com/a/3886128/2666212). – Mike Nov 09 '18 at 15:35
  • @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. – bliako Feb 12 '20 at 20:59
  • @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](https://stackoverflow.com/a/4954188)), 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 Feb 12 '20 at 21:14
  • 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... – Mike Feb 12 '20 at 21:16