[Development] Utf8 as the 8bit encoding on Windows

Lars Knoll lars.knoll at qt.io
Mon May 4 16:17:18 CEST 2020


> * QTextStream still delivers mojibake in all cases. I assume there’s a bug somewhere in the way we handle things in QTextStream, this needs some debugging.

Ok, ignore this bullet point. It was simply a bug in my test app (QFile is using buffered writes, and I was resetting the output CP of the console before they got flushed). It works correctly with the code below. So everything seems to work correctly with utf8 if I set both the console code pages and the manifest file.

Lars

#include <qdebug.h>
#include <Windows.h>
#include <io.h>
#include <qfile.h>

int main(int, char**)
{
    uint cp = GetConsoleOutputCP();
    qDebug() << "COnsole CP" << cp << GetACP();
    SetConsoleOutputCP(CP_UTF8);

    {
        qDebug() << "Hello" << QString::fromUtf8("Ελληνικά");
        printf("Ελληνικά\n");
        QByteArray greek = "Ελληνικά\n";
        fprintf(stdout, greek.constData());
        for (char c : greek)
            fprintf(stdout, "%c", c);
        _write(1, greek.constData(), greek.length());
        for (char c : greek)
            _write(1, &c, 1);

        QFile file;
        file.open(stdout, QFile::WriteOnly);
        file.write(greek);


        qDebug() << "XXX";
        QTextStream ts(stdout);
        ts.setEncoding(QStringConverter::Utf8);
        ts << greek << QString::fromUtf8(greek);
    }

    SetConsoleOutputCP(cp);
    return 0;
}



More information about the Development mailing list