Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

qt-qml-Qt Creator use msvc2017 qDebug print text messy on windows7



  • I use MSVC 2017

    set env encoding utf-8: https://i.stack.imgur.com/BK70l.png

    set text eidtor encoding utf-8: https://i.stack.imgur.com/6sYw6.png

    I add #pragma execution_character_set("utf-8") to cpp file then

    cout<< "六六六" <<endl;
    qDebug() << "qu: 六六六";
    // output:
    // 六六六
    // qu: ������
    

    Update1: fromUtf8

    I remove #pragma execution_character_set("utf-8") and add this to .pro file

    msvc {
        QMAKE_CFLAGS += /utf-8
        QMAKE_CXXFLAGS += /utf-8
    }
    

    then run:

    cout<< "六六六" <<endl;
    qDebug() << QString::fromUtf8("qu: 六六六");
    // output:
    // 六六六
    // "qu: ������"
    

    The question remains

    Update2: QStringLiteral

    cout<< "六六六" <<endl;
    qDebug() << QStringLiteral("qu: 六六六");
    // output:
    // 六六六
    // "qu: ��\uE15E����?"
    

    Update3: waring this

    d167db56-c182-4213-994d-0a6f2d7aeb24-image.png

    Update 4:

    I save with UTF-8
    c17ef3ef-a7b2-4cd0-9802-416f1ecad129-image.png

    Update5: qUtf8Printable

    cout<< "六六六" <<endl;
    qDebug() << qUtf8Printable("qu: 六六六");
    // output:
    // 六六六
    // qu: ������
    

    thanks, but not work

    Update6:

    I want to use WebEngineView in MSVC, so I use UTF-8

    Update7: toUnicode

    QTextCodec* codecUtf8 = QTextCodec::codecForName("UTF-8");
    cout<< "六六六" <<endl;
    qDebug() << codecUtf8->toUnicode("qu: 六六六");
    //output:
    //六六六
    //"qu: ������"
    

    Update8:

    I change env encoding to system, then it works, cout can not print text to console in Visual Studio

    qDebug() << "qu: 六六六";
    // output:
    // qu: 六六六
    


  • @make2 Did you store you source code file as UTF-8 ?

    qDebug() << qUtf8Printable("qu: 六六六"); should do the job.

    EDIT:

    You can also setup codec for C-Strings:

     QTextCodec *codec = QTextCodec::codecForName("UTF-8");
     QTextCodec::setCodecForCStrings(codec);
    
    qDebug() << "qu: 六六六";
    

    ==> Sorry is not working/supported with Qt5


Log in to reply