Important: Please read the 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:

    set text eidtor encoding utf-8:

    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


    Update 4:

    I save with UTF-8

    Update5: qUtf8Printable

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

    thanks, but not work


    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: 六六六");
    //"qu: ������"


    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.


    You can also setup codec for C-Strings:

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

    ==> Sorry is not working/supported with Qt5

Log in to reply