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

后端 未结 1 1598
情深已故
情深已故 2021-01-11 13:07

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 li

相关标签:
1条回答
  • 2021-01-11 13:55

    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.

    0 讨论(0)
提交回复
热议问题