QString global variable and coding problem

  • I have a problem with QString which is a global variable. Coding of this variable is wrong. File is in UTF8. Here is example code:
    #include <QtCore/QCoreApplication>
    #include <QTextCodec>
    #include <QDebug>

    QString global( "ąęśćź" );

    int main(int argc, char *argv[])
    QCoreApplication a(argc, argv);

    QString inside( "ąęśćź" );
    qDebug() << "Global: " << global; // print Global:  "Ä

    ęśćź" - it's wrong
    qDebug() << "Inside: " << inside; // print Inside: "ąęśćź" - good

    return a.exec&#40;&#41;;

    How to solve it?

  • Hello,

    How about utf-8 BOM.
    if you select "add if encoding is UTF-8" (on windows), it may work correctly.

  • Your global QString is created before you set codec for c strings to UTF8 so default Latin1 is used. Hence text corruption.

  • sidewinder your are right. To print correct global string this is required:

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.