QT messagebox 乱码解决
2015-06-04 09:46
429 查看
http://www.mutouxiaogui.cn/blog/?p=277
By yhluo 2015年5月20日 Qt No
Comments
使用 Qt 经常可能遇到中文乱码的问题,这里介绍一下 Qt 中文窗口和路径乱码的解决方法。我的环境是 Qt5.2 VS2012 Windows8.1 64bit。
Qt 中文乱码原因
关于 Qt 中文乱码的原因以及在 Qt4 中解决中文乱码问题,推荐参考博文:qt中文乱码问题
Qt4 中 QTextCodec 的很多函数在 Qt5 中都没有了,因此适用与 Qt4 的解决方法对 Qt5 无效。具体可以查看 Qt 的官方文档:
Qt4
Qt5
Qt5 中文乱码解决
下面分别介绍无 BOM UTF-8、有 BOM UTF-8 和 GBK 编码下 Qt 窗口、路径以及 cout 输出中文路径乱码的解决方法。
无 BOM UTF-8
由于 Qt5 内部使用 Unicode 编码,对于无BOM UTF-8 编码的源文件,可以直接使用字符串而不需特殊转换。
测试程序如下:
程序运行结果如下图:
有 BOM UTF-8
对于带 BOM 的 UTF-8 文件,可以使用 QString::fromLocal8Bit 的方法解决乱码问题,测试程序如下:
程序运行结果如下图:
GBK
对于 GBK 编码的文件,同样可以使用 QString::fromLocal8Bit 的方法解决,测试程序如下:
程序运行结果如下图:
对于 Qt5 中的 QTextCodec 貌似并不能解决乱码问题,测试程序如下:
程序运行结果如下图:
关于 Qt 中文乱码的问题就简单介绍到这里,有任何问题或建议欢迎在下面的评论框中留言共同探讨。thx
Qt 中文乱码
By yhluo 2015年5月20日 Qt NoComments
使用 Qt 经常可能遇到中文乱码的问题,这里介绍一下 Qt 中文窗口和路径乱码的解决方法。我的环境是 Qt5.2 VS2012 Windows8.1 64bit。
Qt 中文乱码原因
关于 Qt 中文乱码的原因以及在 Qt4 中解决中文乱码问题,推荐参考博文:qt中文乱码问题
Qt4 中 QTextCodec 的很多函数在 Qt5 中都没有了,因此适用与 Qt4 的解决方法对 Qt5 无效。具体可以查看 Qt 的官方文档:
Qt4
Qt5
Qt5 中文乱码解决
下面分别介绍无 BOM UTF-8、有 BOM UTF-8 和 GBK 编码下 Qt 窗口、路径以及 cout 输出中文路径乱码的解决方法。
无 BOM UTF-8
由于 Qt5 内部使用 Unicode 编码,对于无BOM UTF-8 编码的源文件,可以直接使用字符串而不需特殊转换。
测试程序如下:
1 | #include "mainwindow.h" |
2 | #include <QApplication> |
3 | #include <QMessageBox> |
4 | #include <QFileDialog> |
5 | #include <iostream> |
6 | using namespace std; |
7 |
8 | int main( int argc, char *argv[]) |
9 | { |
10 | QApplication |
11 | MainWindow w; |
12 | w.setWindowTitle( "无BOM UTF8 中文标题" ); |
13 | w.show(); |
14 | QString dir = QFileDialog::getExistingDirectory(&w); |
15 | cout << dir.toLocal8Bit().data() << endl; |
16 | QMessageBox::information(0, "MessageBox 文件路径" , dir); |
17 |
18 | return a.exec(); |
19 | } |
有 BOM UTF-8
对于带 BOM 的 UTF-8 文件,可以使用 QString::fromLocal8Bit 的方法解决乱码问题,测试程序如下:
1 | #include "mainwindow.h" |
2 | #include <QApplication> |
3 | #include <QMessageBox> |
4 | #include <QFileDialog> |
5 | #include <iostream> |
6 | using namespace std; |
7 |
8 | int main( int argc, char *argv[]) |
9 | { |
10 | QApplication |
11 | MainWindow w; |
12 | w.setWindowTitle(QString::fromLocal8Bit( "有BOM UTF8 中文标题" )); |
13 | w.show(); |
14 | QString dir = QFileDialog::getExistingDirectory(&w); |
15 | cout << dir.toLocal8Bit().data() << endl; |
16 | QMessageBox::information(0, QString::fromLocal8Bit( "MessageBox 文件路径" ), dir); |
17 |
18 | return a.exec(); |
19 | } |
GBK
对于 GBK 编码的文件,同样可以使用 QString::fromLocal8Bit 的方法解决,测试程序如下:
1 | #include "mainwindow.h" |
2 | #include <QApplication> |
3 | #include <QMessageBox> |
4 | #include <QFileDialog> |
5 | #include <iostream> |
6 | using namespace std; |
7 |
8 | int main( int argc, char *argv[]) |
9 | { |
10 | QApplication |
11 | MainWindow w; |
12 | w.setWindowTitle(QString::fromLocal8Bit( "GBK 中文标题" )); |
13 | w.show(); |
14 | QString dir = QFileDialog::getExistingDirectory(&w); |
15 | cout << dir.toLocal8Bit().data() << endl; |
16 | QMessageBox::information(0, QString::fromLocal8Bit( "MessageBox 文件路径" ), dir); |
17 |
18 | return a.exec(); |
19 | } |
对于 Qt5 中的 QTextCodec 貌似并不能解决乱码问题,测试程序如下:
1 | #include "mainwindow.h" |
2 | #include <QApplication> |
3 | #include <QMessageBox> |
4 | #include <QFileDialog> |
5 | #include <iostream> |
6 | #include <QTextCodec> |
7 | using namespace std; |
8 |
9 | int main( int argc, char *argv[]) |
10 | { |
11 | QTextCodec::setCodecForLocale(QTextCodec::codecForName( "GBK" )); |
12 | QApplication |
13 | MainWindow w; |
14 | w.setWindowTitle( "GBK 中文标题" ); |
15 | w.show(); |
16 | QString dir = QFileDialog::getExistingDirectory(&w); |
17 | cout << dir.toLocal8Bit().data() << endl; |
18 | QMessageBox::information(0, QString::fromLocal8Bit( "MessageBox 文件路径" ), dir); |
19 |
20 | return a.exec(); |
21 | } |
关于 Qt 中文乱码的问题就简单介绍到这里,有任何问题或建议欢迎在下面的评论框中留言共同探讨。thx
相关文章推荐
- Qt tableWidget 空单元格 获取选中行行号
- 十六、Qt 2D绘图(六)坐标系统
- QT项目笔记
- win7下配置OpenCV的Qt开发环境
- 大量的QT控件及示例发放
- qt字体设置
- Qt(2):MOC文件解析
- Ubuntu14.04 LTS安装 OpenCV-3.0.0-rc1 + QT5.4.1
- Tiny6410 && ubuntu 14.04 运行qt程序
- QTableWidget
- QML和C++混合编程--Qt声明式用户界面运行环境
- qt按键焦点切换
- Qt多线程学习:创建多线程
- 解决QT用着用着就不好使的问题
- opencv & qt study-(3)-图像的容器--Mat
- Qt5.3 for vs2013 中文乱码解决办法
- Ubuntu环境下Qt软件deb包制作
- 使用Qt5.4安装Netanim 3.106
- QT对话框中show和exec的区别
- Qt开发:打开外部程序