Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print QString containing line breaks using qDebug

Tags:

c++

qt

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?

like image 928
Paddre Avatar asked Sep 18 '16 17:09

Paddre


Video Answer


1 Answers

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.

like image 93
Mike Avatar answered Sep 29 '22 17:09

Mike