QT5.2中文乱码解决问题
2017-06-26 19:04
330 查看
转自
http://blog.163.com/qimo601@126/blog/static/1582209320143115334438/
总结:之前Qt4写的代码用QTextCodec::codecForTr()来解决中文输入问题,为了防止错误的滥用,在Qt5中已经取消了这个方法。
Qt5+VS2010中文乱码问题的解决:【请记住VS2010必须打普定vs2010sp1,下面代码才有效】
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
Qt+Visualstudio的中文乱码总结:
1、Qt4.7+VS2008,通过如下方法:
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// 以下部分解决中文乱码
QTextCodec *codec = QTextCodec::codecForName("GBK");
QTextCodec::setCodecForTr(codec);
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
}
2、Qt5.2+VS2010打入补丁VS2010sp1,也会支持 #pragma execution_character_set ("utf-8"),顺利解决qt中文乱码问题
在main函数之前加入:
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
3、Qt5.2+ VS2012 不支持 #pragma execution_character_set ("utf-8") 导致utf-8编码的无法直接输出中文,qt首当其冲受害。 微软官方回应,再下一个版本中会修正这个问题:
由 Microsoft 在
2013/1/3 於 10:24 公佈
Hi Seek:
Thanks for reporting the issue.
A fix for this issue has been checked into the compiler sources. The fix should show up in the next release of Visual C++.
Xiang Fan
Visual C++ Team
4、Qt5.2+VS2013支持 #pragma
execution_character_set ("utf-8"),所以qt也赶紧发布了一个
Visual
Studio Add-in 1.2.3 Alpha for Qt5 (supports versions 2008, 2010, 2012 and 2013, ess edition)
在main函数之前加入:
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
5、回到 Qt5 的中文输出问题。
Qt默认支持 VS2010/MinGW/Gcc 等编译器,而它们现在都已经真正支持UTF8了。当然,VS2010 对UTF8的支持会入侵代码(#pragma execution_character_set("utf-8"))。 看看Qt官方论坛别人是怎么说的:
http://qt-project.org/forums/viewthread/17617
Nothing special need to do, it will works by default.
If the exec-charset of your your compiler is UTF-8.
简单的说,从Qt5开始,源代码就是默认UTF8编码的。
当然,VC2010编辑器对带BOM的UTF8也是认识,只可惜VC2010编译器根本承认它是UTF8!
在继续看官方论坛的回复:
You can write a simple example like this
You can write a simple example like this
#include <QApplication>
#include <QLabel>
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel label("???ó??ń??");
label.show();
return a.exec();
}
If other people can reproduce your problem, you can file a bug.
If other people can reproduce your problem, you can file a bug.
较完整的解决方案(增加了Qt4/Qt5和非VC环境的判断):
// Coding: UTF-8(BOM)
#if defined(_MSC_VER) && (_MSC_VER >= 1600)
# pragma execution_character_set("utf-8")
#endif
#include <QApplication>
#include <QTextCodec>
#include <QLabel>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
#if defined(_MSC_VER) && (_MSC_VER < 1600)
QTextCodec::setCodecForTr(QTextCodec::codecForName("GB18030-0"));
#else
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
#endif
#endif
QLabel *label = new QLabel(QObject::tr("你好!"));
label->show();
return app.exec();
}
另外:Qt4/Qt5/Linux: 只要是默认的UTF8环境, 应该都没问题
其实这个问题不是Qt特有的, 追根溯源还是C/C++和编译器的问题.即使是支持UTF16的Java也同样难逃此问题.
http://blog.163.com/qimo601@126/blog/static/1582209320143115334438/
总结:之前Qt4写的代码用QTextCodec::codecForTr()来解决中文输入问题,为了防止错误的滥用,在Qt5中已经取消了这个方法。
Qt5+VS2010中文乱码问题的解决:【请记住VS2010必须打普定vs2010sp1,下面代码才有效】
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
Qt+Visualstudio的中文乱码总结:
1、Qt4.7+VS2008,通过如下方法:
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// 以下部分解决中文乱码
QTextCodec *codec = QTextCodec::codecForName("GBK");
QTextCodec::setCodecForTr(codec);
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
}
2、Qt5.2+VS2010打入补丁VS2010sp1,也会支持 #pragma execution_character_set ("utf-8"),顺利解决qt中文乱码问题
在main函数之前加入:
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
3、Qt5.2+ VS2012 不支持 #pragma execution_character_set ("utf-8") 导致utf-8编码的无法直接输出中文,qt首当其冲受害。 微软官方回应,再下一个版本中会修正这个问题:
由 Microsoft 在
2013/1/3 於 10:24 公佈
Hi Seek:
Thanks for reporting the issue.
A fix for this issue has been checked into the compiler sources. The fix should show up in the next release of Visual C++.
Xiang Fan
Visual C++ Team
4、Qt5.2+VS2013支持 #pragma
execution_character_set ("utf-8"),所以qt也赶紧发布了一个
Visual
Studio Add-in 1.2.3 Alpha for Qt5 (supports versions 2008, 2010, 2012 and 2013, ess edition)
在main函数之前加入:
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
5、回到 Qt5 的中文输出问题。
Qt默认支持 VS2010/MinGW/Gcc 等编译器,而它们现在都已经真正支持UTF8了。当然,VS2010 对UTF8的支持会入侵代码(#pragma execution_character_set("utf-8"))。 看看Qt官方论坛别人是怎么说的:
http://qt-project.org/forums/viewthread/17617
Nothing special need to do, it will works by default.
If the exec-charset of your your compiler is UTF-8.
简单的说,从Qt5开始,源代码就是默认UTF8编码的。
当然,VC2010编辑器对带BOM的UTF8也是认识,只可惜VC2010编译器根本承认它是UTF8!
在继续看官方论坛的回复:
You can write a simple example like this
You can write a simple example like this
#include <QApplication>
#include <QLabel>
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel label("???ó??ń??");
label.show();
return a.exec();
}
If other people can reproduce your problem, you can file a bug.
If other people can reproduce your problem, you can file a bug.
较完整的解决方案(增加了Qt4/Qt5和非VC环境的判断):
// Coding: UTF-8(BOM)
#if defined(_MSC_VER) && (_MSC_VER >= 1600)
# pragma execution_character_set("utf-8")
#endif
#include <QApplication>
#include <QTextCodec>
#include <QLabel>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
#if defined(_MSC_VER) && (_MSC_VER < 1600)
QTextCodec::setCodecForTr(QTextCodec::codecForName("GB18030-0"));
#else
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
#endif
#endif
QLabel *label = new QLabel(QObject::tr("你好!"));
label->show();
return app.exec();
}
另外:Qt4/Qt5/Linux: 只要是默认的UTF8环境, 应该都没问题
其实这个问题不是Qt特有的, 追根溯源还是C/C++和编译器的问题.即使是支持UTF16的Java也同样难逃此问题.
相关文章推荐
- QT5.2中文乱码解决问题
- 完美解决QT->setText()中文乱码问题
- 解决QT中文字符串在osg中显示乱码的问题
- 关于TI ControlSUITE中文乱码和CCS5.2添加ControlSUITE问题的解决
- Qt做发布版,解决声音和图片、中文字体乱码问题(转)
- windows下Qt代码移植到linux下的中文乱码问题解决
- Qt中文乱码问题解决办法【转】
- Qt做发布版,解决声音和图片、中文字体乱码问题
- Qt下解决中文乱码的问题
- QT-解决中文显示乱码问题
- Qt解决中文乱码问题----记录
- qt 解决中文乱码问题
- 解决了QT网络编程中的中文乱码问题
- QT 解决中文处理乱码的问题
- Qt中文乱码问题解决办法
- Qt做发布版,解决声音和图片、中文字体乱码问题(转)
- QT中解决中文乱码问题
- 使用Qt Visual Studio Add-in 实现中文ts文件,解决中文乱码问题
- Qt中文乱码问题解决办法
- Qt解决中文乱码问题