Why is QString printed with quotation marks?
Asked Answered
P

5

26

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?

Pignus answered 16/1, 2015 at 2:54 Comment(1)
possible duplicate of How to call qDebug without the appended spaces and newline?Evaginate
M
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.

Moderato answered 16/1, 2015 at 3:23 Comment(5)
So that's why qPrintable(const QString) removes the quotation marks. Thank you very much.Pignus
@Pignus 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.Moderato
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.Pignus
@Pignus 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 ".Moderato
That's not a "because" reason, anyhow. The most important "because" is to spot leading/trailing spaces in your strings!Highup
A
43

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;
Aggappora answered 16/1, 2015 at 3:33 Comment(5)
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.Pignus
@Pignus I added an example, see if that helps.Aggappora
@Aggappora 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.Pignus
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...)Expire
why do we need to use endl then?Anathema
M
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.

Moderato answered 16/1, 2015 at 3:23 Comment(5)
So that's why qPrintable(const QString) removes the quotation marks. Thank you very much.Pignus
@Pignus 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.Moderato
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.Pignus
@Pignus 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 ".Moderato
That's not a "because" reason, anyhow. The most important "because" is to spot leading/trailing spaces in your strings!Highup
S
6

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

qDebug() << QString("TEST").toLatin1().data();
Strapless answered 16/4, 2015 at 13:20 Comment(1)
This actually is a valid workaround for 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.Sansculotte
T
3

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

Turtle answered 30/11, 2021 at 13:20 Comment(2)
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From ReviewPolenta
it does answers to Is there a method inside QString to change the way it's printed? with a one linerTurtle
C
2

Simply cast to const char *

qDebug() << (const char *)yourQString.toStdString().c_str();
Cumulative answered 6/3, 2017 at 14:9 Comment(2)
Qt includes a function that basically does this: const char * qPrintable ( const QString & str )Labroid
The docs say that, when using qDebug, you should use qUtf8Printable because qDebug expects UTF-8.Bartolomeo

© 2022 - 2024 — McMap. All rights reserved.