the qt app shows Chinese in messy code on windows when using UTF-8 encoding-format
-
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! -
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!@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. -
@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.@jsulm
ok, now I change the main.cpp code and here is my main.cpp codeint 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! -
@jsulm
ok, now I change the main.cpp code and here is my main.cpp codeint 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!@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
-
@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
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? -
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? -
@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.
@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)
-
@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)
@Christian-Ehrlicher You're probably right. I never use anything else than English strings in my/our sources, so never had such issues :-)