Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. the qt app shows Chinese in messy code on windows when using UTF-8 encoding-format

the qt app shows Chinese in messy code on windows when using UTF-8 encoding-format

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Princein
    wrote on last edited by
    #1

    Hi:
    I developed a simple qt app on windows to test the qt Chinese UTF-8 encoding-format:

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
       QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
        QString strMessage = QString::fromLocal8Bit("我是UTF8编码的文件:");
        qDebug() << strMessage;
        return a.exec();
    }
    
    

    and my main.cpp file encoding format is UTF-8 without BOM, but when I run the app on windows, the app print string is "鎴戞槸UTF8缂栫爜鐨勬枃浠讹細" which I expect is "我是UTF8编码的文件:",it seems the string "我是UTF8编码的文件:" is converted to GB2312 encoding-format so shows the wrong string "鎴戞槸UTF8缂栫爜鐨勬枃浠讹細" in runtime,and the string "我是UTF8编码的文件:" shows right string "我是UTF8编码的文件:'' when the app runs on macos, I don't know why?
    how to let the string "我是UTF8编码的文件:" show right on windows platform, thanks a lot!

    jsulmJ 1 Reply Last reply
    0
    • P Princein

      Hi:
      I developed a simple qt app on windows to test the qt Chinese UTF-8 encoding-format:

      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
         QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
          QString strMessage = QString::fromLocal8Bit("我是UTF8编码的文件:");
          qDebug() << strMessage;
          return a.exec();
      }
      
      

      and my main.cpp file encoding format is UTF-8 without BOM, but when I run the app on windows, the app print string is "鎴戞槸UTF8缂栫爜鐨勬枃浠讹細" which I expect is "我是UTF8编码的文件:",it seems the string "我是UTF8编码的文件:" is converted to GB2312 encoding-format so shows the wrong string "鎴戞槸UTF8缂栫爜鐨勬枃浠讹細" in runtime,and the string "我是UTF8编码的文件:" shows right string "我是UTF8编码的文件:'' when the app runs on macos, I don't know why?
      how to let the string "我是UTF8编码的文件:" show right on windows platform, thanks a lot!

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Princein said in the qt app shows Chinese in messy code on windows when using UTF-8 encoding-format:

      QString::fromLocal8Bit

      this function expects 8bit ASCII encoded string (which can't represent Chinese) and not UTF8!
      Use https://doc.qt.io/qt-5/qstring.html#fromUtf8 instead.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply
      3
      • jsulmJ jsulm

        @Princein said in the qt app shows Chinese in messy code on windows when using UTF-8 encoding-format:

        QString::fromLocal8Bit

        this function expects 8bit ASCII encoded string (which can't represent Chinese) and not UTF8!
        Use https://doc.qt.io/qt-5/qstring.html#fromUtf8 instead.

        P Offline
        P Offline
        Princein
        wrote on last edited by
        #3

        @jsulm
        ok, now I change the main.cpp code and here is my main.cpp code

        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
           QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
            QString strMessage = "我是UTF8编码的文件:";
            qDebug() << strMessage;
           std:: string str = "我是UTF8编码的文件:";
            cout << str <<endl;
            return a.exec();
        }
        

        the strMessage print "我是UTF8编码的文件:" on windows which just as I expected,but the str print "鎴戞槸UTF8缂栫爜鐨勬枃浠讹細" which is wrong, it seems the std::string not support utf-8 chinese,
        how can I set to get the right utf-8 chinese "我是UTF8编码的文件:" when using the std::string? thanks a lot!

        jsulmJ 1 Reply Last reply
        0
        • P Princein

          @jsulm
          ok, now I change the main.cpp code and here is my main.cpp code

          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
             QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
              QString strMessage = "我是UTF8编码的文件:";
              qDebug() << strMessage;
             std:: string str = "我是UTF8编码的文件:";
              cout << str <<endl;
              return a.exec();
          }
          

          the strMessage print "我是UTF8编码的文件:" on windows which just as I expected,but the str print "鎴戞槸UTF8缂栫爜鐨勬枃浠讹細" which is wrong, it seems the std::string not support utf-8 chinese,
          how can I set to get the right utf-8 chinese "我是UTF8编码的文件:" when using the std::string? thanks a lot!

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Princein You should read documentation: https://doc.qt.io/qt-5/qstring.html#toStdString
          "The Unicode data is converted into 8-bit characters using the toUtf8()".

          Try to use https://doc.qt.io/qt-5/qstring.html#toStdU16String

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          P 1 Reply Last reply
          2
          • jsulmJ jsulm

            @Princein You should read documentation: https://doc.qt.io/qt-5/qstring.html#toStdString
            "The Unicode data is converted into 8-bit characters using the toUtf8()".

            Try to use https://doc.qt.io/qt-5/qstring.html#toStdU16String

            P Offline
            P Offline
            Princein
            wrote on last edited by
            #5

            @jsulm

             std:: string str = "我是UTF8编码的文件:";
             cout << str <<endl;
            

            I mean the std:: string is not related to QString, but print "鎴戞槸UTF8缂栫爜鐨勬枃浠讹細" which is wrong, if I want to print the str right that show "我是UTF8编码的文件:" when using std::string,
            what should I do?

            jsulmJ 1 Reply Last reply
            0
            • P Princein

              @jsulm

               std:: string str = "我是UTF8编码的文件:";
               cout << str <<endl;
              

              I mean the std:: string is not related to QString, but print "鎴戞槸UTF8缂栫爜鐨勬枃浠讹細" which is wrong, if I want to print the str right that show "我是UTF8编码的文件:" when using std::string,
              what should I do?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Princein Do you store your source code as UTF8? I mean, the string you want to print is in your source code, so the source code needs to be stored as UTF8 also. You can check in QtCreator settings.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              Christian EhrlicherC 1 Reply Last reply
              0
              • jsulmJ jsulm

                @Princein Do you store your source code as UTF8? I mean, the string you want to print is in your source code, so the source code needs to be stored as UTF8 also. You can check in QtCreator settings.

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @jsulm said in the qt app shows Chinese in messy code on windows when using UTF-8 encoding-format:

                Do you store your source code as UTF8?

                Even today I would suggest to not store anything else than latin1 in the source code and use a proper translation for the rest - then one would never have such problems like here (which will maybe get even worse when using more than on os/compiler)

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                jsulmJ 1 Reply Last reply
                3
                • Christian EhrlicherC Christian Ehrlicher

                  @jsulm said in the qt app shows Chinese in messy code on windows when using UTF-8 encoding-format:

                  Do you store your source code as UTF8?

                  Even today I would suggest to not store anything else than latin1 in the source code and use a proper translation for the rest - then one would never have such problems like here (which will maybe get even worse when using more than on os/compiler)

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher You're probably right. I never use anything else than English strings in my/our sources, so never had such issues :-)

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved