Generally, UTF-8 encoding is used by default in Linux, So we write Qt in Linux.ProgramThe following two sentences are generally added:
Qtextcodec: setcodecforlocale (qtextcodec: codecforname ("UTF-8"); qtextcodec: setcodecforcstrings (qtextcodec: codecforname ("UTF-8 "));
This sentence may also be added:
Qtextcodec: setcodecfortr (qtextcodec: codecforname ("UTF-8 "));
For more information, see.
After the above settings, it is okay to display basic Chinese in Linux. However, when we need to transmit strings with programs in windows, we can directly transmit the qstring, in Windows, Chinese characters are garbled, because Windows uses GBK encoding by default. Therefore, we need to convert the qstring encoding before sending:
Qtextcodec * gbkcodec = qtextcodec: codecforname ("GBK"); qtextcodec: setcodecforlocale (gbkcodec); smessage = gbkcodec-> tounicode (smessage. tolocal8bit (); // smessage is the string to be sent
Of course, we want the program in Linux to be displayed normally in the future. After sending the program, we need to make up the following sentence:
Qtextcodec: setcodecforlocale (qtextcodec: codecforname ("UTF-8 "));