user1598585
user1598585

Reputation:

I lose “unicodeness” when qDebug()ing after instancing a QApplication

I am losing the capability of printing unicode characters right after instancing a QApplication object.

From the following code and having included all the needed libraries:

int main(int argc, char** argv)
{   
    qDebug() << "aeiou áéíóú";
    QApplication app(argc, argv);
    qDebug() << "aeiou áéíóú";
    return 0;
}

I am getting this output:

aeiou áéíóú
aeiou áéíóú

How do I fix this odd behaviour? I need to be able to print unicode strings (coming in UTF-8).

Upvotes: 12

Views: 1088

Answers (1)

2017 UPDATE: This answer from 2011 applies for Qt 4. In Qt 5, the text codecs were eliminated, and all source is expected to be UTF-8. See "Source code must be UTF-8 and QString wants it"

When Qt interprets char * into a string, it uses a text codec. This is set globally, and you can choose what you want for your project:

https://doc.qt.io/qt-4.8/qtextcodec.html#setCodecForCStrings

Note that Qt's default is Latin-1, and it may establish that default in the QApplication constructor call stack somewhere. If you're globally using UTF-8 in your project, you might try:

int main(int argc, char** argv)
{   
    qDebug() << "aeiou áéíóú";

    QApplication app(argc, argv);
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QTextCodec::setCodecForCStrings(codec);

    qDebug() << "aeiou áéíóú";
    return 0;
}

And see if that solves your issue.

Upvotes: 13

Related Questions