Uploaded image for project: 'Qt Creator'
  1. Qt Creator
  2. QTCREATORBUG-32698

使用Qt Creator 16的Artistic Style格式化代码时,中文字符变成乱码//When using Qt Creator 16 to format code with Artistic Style, Chinese characters become garbled.

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P3: Somewhat important
    • Qt Creator 16.0.1
    • Qt Creator 16.0.0
    • Editors
    • None
    • Windows 11 24H2
      Qt Creator 16.0.0
      Qt 6.8.3
      Artistic Style 3.6.8
      MinGW 13.1.0
    • Windows
    • 4d8a4e97bf2b661e78aa0b929e1838215c101a2b

    Description

      当我使用Qt Creator 16.0.0中Artistic Style插件格式化代码时,代码中的中文字符变成了乱码。

      When I formatted the code using the Artistic Style plugin in Qt Creator 16.0.0, the Chinese characters in the code became garbled.

       

      以下是一个简单的例子:

      Here's a simple example:

      //QtCBugExample.cpp
      #include <stdio.h>
      int main(int argc, char *argv[])
      {
          printf("这是一段中文");
          return 0;
      }

       

      当我在Qt Creator 16.0.0中使用Artistic Style插件进行格式化之后,它变成了这样:

      When I formatted it with the Artistic Style plugin in Qt Creator 16.0.0, it turns out like this:

      //QtCBugExample.cpp
      #include <stdio.h>
      int main(int argc, char* argv[])
      {	
          printf("杩欐槸涓€娈典腑鏂�");	
          return 0;
      } 

      为了验证故障,我使用cmd运行astyle.exe格式化相同的一个源文件,它工作正常,结果输出完全正确,而在Qt Creator中使用又会变成乱码,当我继续对此文件进行格式化时,乱码又会变成别的乱码,且越来越长。

      In order to verify the fault, I use cmd to run astyle.exe to format the same source file, it works fine, the output is completely correct, but use in Qt Creator will become garbled, as I continue to format this file, garbled will become other garbled, and more and more long.

       

      我意识到这是关于编码的问题,于是我查看并调试了Qt Creator 16.0.0的源码,想要找出问题所在。

      I realized it was a encoding issue, so I looked at and debugged the Qt Creator 16.0.0 source code to figure out what the problem was.

       

      我发现在formattexteditor.cpp (in src\plugins\texteditor) 的format方法中创建了一个Process,process启动了astyle.exe,格式化后的文本来自processreadAllStandardOutput方法(行 117),检查返回的文本可知从readAllStandardOutput方法返回的数据就是错误的。所以,错误发生在从输出的char*数据到QString的转换过程中。

      I found that it created a Process object in the format method in formattexteditor.cpp (in src\plugins\texteditor), process launches astyle.exe, The formatted data comes from the readAllStandardOutput (line 117) method of process , and checking the returned data shows that the data returned from the readAllStandardOutput method is incorrect. So, the error occurs during the conversion from the output char* data to QString.

       

      我尝试在Process创建(行 99)之后设置输出编码为UTF-8,如下:

      I tried to set the output encoding to UTF-8 after Process creation (line 99), as follows:

      //代码占位符
      ...
      Process process;
      process.setUtf8Codec();  // new added
      QStringList options = input.command.options(); 
      ...

      再次编译并尝试格式化代码,它完全工作正常,似乎问题解决了。

      Compile again and try to format that source code, it works perfectly and seems to solve the problem.

       

      另外,其它的代码格式化插件未做测试。

      In addition, other code formatting plug-ins are not tested.

       

      我希望在以后的版本中修复此错误。

      I hope to fix this bug in a future release.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            davschul David Schulz
            chenlinfeng 林烽 陈
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes