Search code examples
c++qtutf-8qstringqdebug

qDebug outputs QString UTF-8 non-Ascii symbols as like \uxxxx


I am trying to convert string (QString) in unicode to utf-8.

qDebug prints string like this:

"Fault code soap:Client: \u041F\u043E\u043B\u044C\u0437\u043E\u0432\u0430\u0442\u0435\u043B\u044C \u0441 \u0438\u0434\u0435\u043D\u0442\u0438\u0444\u0438\u043A\u0430\u0442\u043E\u0440\u043E\u043C \u00AB16163341545811\u00BB \u043D\u0435 \u043D\u0430\u0439\u0434\u0435\u043D"

I have tried using QTextCodec like this but it outputs same unreadable string:

QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QString readableStr = QString::fromUtf8(codec->fromUnicode(str));

What am I doing wrong?

EDIT:

I wonder what is going on but it happens when qDebug prints QString...

The following code

qDebug() << QString::fromUtf8("тест") << "тест" << QString::fromUtf8("тест").toUtf8().data();

prints out:

"\u0442\u0435\u0441\u0442" тест тест


Solution

  • I don't know the exact thread on the Qt mailing list, but this behaviour was recently introduced, as qDebug is originally meant to debug an objects internal state. Non ASCII characters are now put out like this, which most people seem to dislike, but the developer or maintainer responsible wants to keep it this way.