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?
Qt 5.4 has a new feature that lets you disable this. To quote the documentation:
(Emphasis mine.)
Here's an example of how you'd use this feature:
Another option is to use
QTextStream
withstdout
. There's an example of this in the documentation:Why?
It's because of the implementation of
qDebug()
.From the source code:
Therefore,
outputs
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 throughqDebug
.For example,
qDebug
print boolean value into text expressiontrue
/false
:It outputs
true
orfalse
to your terminal. Therefore, if you had aQString
which storetrue
, you need a quote mark"
to specify the type.Qt 4: If the string contains just ASCII, the following workaround helps:
Simply cast to
const char *