12

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).

  • @HostileFork Yes they are all in UTF-8, I learned to do that when Web-Developing, it is not fun to serve UTF-8 encoded documents with files being in latin1 and not knowing it. lol –  Oct 06 '11 at 16:12

1 Answers1

13

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.