25

So when you use qDebug() to print a QString, quotation marks appears suddenly in the output.

int main()
{
    QString str = "hello world"; //Classic
    qDebug() << str; //Output: "hello world"
    //Expected Ouput: hello world
}

I know we can solve this with qPrintable(const QString), but I was just wondering why does QString work like that?, and Is there a method inside QString to change the way it's printed?

AAEM
  • 1,837
  • 2
  • 18
  • 26
Michael
  • 1,018
  • 4
  • 14
  • 30
  • possible duplicate of [How to call qDebug without the appended spaces and newline?](http://stackoverflow.com/questions/5209823/how-to-call-qdebug-without-the-appended-spaces-and-newline) – Michael Petch Jan 16 '15 at 03:00

5 Answers5

41

Qt 5.4 has a new feature that lets you disable this. To quote the documentation:

QDebug & QDebug::​noquote()

Disables automatic insertion of quotation characters around QChar, QString and QByteArray contents and returns a reference to the stream.

This function was introduced in Qt 5.4.

See also quote() and maybeQuote().

(Emphasis mine.)

Here's an example of how you'd use this feature:

QDebug debug = qDebug();
debug << QString("This string is quoted") << endl;
debug.noquote();
debug << QString("This string is not") << endl;

Another option is to use QTextStream with stdout. There's an example of this in the documentation:

QTextStream out(stdout);
out << "Qt rocks!" << endl;
Nejat
  • 31,784
  • 12
  • 106
  • 138
MrEricSir
  • 8,044
  • 4
  • 30
  • 35
  • Cool to know, thanks. But somehow when compiling I get that QDebug::noquote() is not defined as member of QDebug in Linux version of Qt. Maybe because It's just based on Qt 5.4. – Michael Jan 16 '15 at 03:46
  • @Michael I added an example, see if that helps. – MrEricSir Jan 16 '15 at 04:15
  • 5
    @MrEricSir I am using `qDebug().noquote() << QStringRef(back, i, cols);`... I think it's the same, and also tried your example, but I keep getting the error saying noquote() is not a member of QDebug, thanks for help but it's probably because of my Qt version. It's Linux version and It's not really 5.4 instead just based on 5.4, maybe that's the reason. – Michael Jan 16 '15 at 04:23
  • This does work and may be helpful in some situations, but has a major drawback: The debug output (at least on my system) is not printed until the `QDebug` object is destroyed, so you possibly get no debug output until the application is shut down (at which point it technically is no longer debug / log output...) – ssc Dec 15 '16 at 15:25
  • why do we need to use `endl` then? – Martin Delille Dec 27 '18 at 14:35
16

Why?

It's because of the implementation of qDebug().

From the source code:

inline QDebug &operator<<(QChar t) { stream->ts << '\'' << t << '\''; return maybeSpace(); }
inline QDebug &operator<<(const char* t) { stream->ts << QString::fromAscii(t); return maybeSpace(); }
inline QDebug &operator<<(const QString & t) { stream->ts << '\"' << t  << '\"'; return maybeSpace(); }

Therefore,

QChar a = 'H';
char b = 'H';
QString c = "Hello";

qDebug()<<a;
qDebug()<<b;
qDebug()<<c;

outputs

'H' 
 H 
"Hello"

Comment

So why Qt do this? Since qDebug is for the purpose of debugging, the inputs of various kinds of type will become text stream output through qDebug.

For example, qDebug print boolean value into text expression true / false:

inline QDebug &operator<<(bool t) { stream->ts << (t ? "true" : "false"); return maybeSpace(); }

It outputs true or false to your terminal. Therefore, if you had a QString which store true, you need a quote mark " to specify the type.

Community
  • 1
  • 1
Tay2510
  • 5,748
  • 7
  • 39
  • 58
  • 1
    So that's why qPrintable(const QString) removes the quotation marks. Thank you very much. – Michael Jan 16 '15 at 03:30
  • @Michael Nope. Just some aesthetic issue I think. `qDebug` just want to emphasize difference in type so it use `"` to **kindly** remind you the type of QString. After all, it's for the purpose of debugging, not for printing. – Tay2510 Jan 16 '15 at 03:33
  • Yes, but qPrintable returns a const char* and according to the source code you provided when a const char* is passed as argument to << operator QDebug doesn't append quotation marks. Correct me if I'm wrong. – Michael Jan 16 '15 at 03:37
  • 1
    @Michael You are right about that. You may also refer to MrEricSir's answer to remove the quote mark and my last edit to see the motivation behind `"`. – Tay2510 Jan 16 '15 at 03:40
  • That's not a "because" reason, anyhow. The most important "because" is **to spot leading/trailing spaces in your strings!** – peppe Jan 16 '15 at 12:08
6

Qt 4: If the string contains just ASCII, the following workaround helps:

qDebug() << QString("TEST").toLatin1().data();
  • 3
    This actually is a valid workaround for https://bugreports.qt.io/browse/QTBUG-48517. `.data()` will echo the String contents in Qt5 as they were in Qt4. I'm sure it's downvoted because of the unecessary `toLatin1`, but this could be changed to `toUtf8`, etc. – tresf Oct 10 '16 at 23:44
2

Simply cast to const char *

qDebug() << (const char *)yourQString.toStdString().c_str();
hfrmobile
  • 1,213
  • 16
  • 16
1

one liner no quotes: qDebug().noquote() << QString("string");

GO.exe
  • 646
  • 7
  • 13
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/30469956) – ankita patel Dec 03 '21 at 19:38
  • it does answers to Is there a method inside QString to change the way it's printed? with a one liner – GO.exe Dec 06 '21 at 20:44