Linux 下QT 5中中文乱码问题

oathupdate 2015-02-11 11:39:47
看网上的方法是
直接将 QTextCodec::setCodecForLocale(QTextCodec::codecForName(“UTF8”)) 至于QApplication实例之前即可解决;
但是我放了之后没有解决。 汉字还是会乱码。
我的汉字是char*类型的 并且汉字是从windows下用Socket发过来的。
我的是Ubuntu 文字是支持简体中文的。
...全文
270 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhxianbin 2015-02-12
  • 打赏
  • 举报
回复
http://qt-project.org/doc/qt-4.8/QTextCodec.html QTextCodec::setCodecForCStrings QTextCodec::setCodecForTr
zhxianbin 2015-02-12
  • 打赏
  • 举报
回复
void QTextCodec::setCodecForCStrings ( QTextCodec * codec ) [static] Sets the codec used by QString to convert to and from const char * and QByteArrays. If the codec is 0 (the default), QString assumes Latin-1. Warning: Some codecs do not preserve the characters in the ASCII range (0x00 to 0x7F). For example, the Japanese Shift-JIS encoding maps the backslash character (0x5A) to the Yen character. To avoid undesirable side-effects, we recommend avoiding such codecs with setCodecsForCString(). Warning: This function is not reentrant. See also codecForCStrings() and setCodecForTr(). void QTextCodec::setCodecForLocale ( QTextCodec * c ) [static] Set the codec to c; this will be returned by codecForLocale(). If c is a null pointer, the codec is reset to the default. This might be needed for some applications that want to use their own mechanism for setting the locale. See also codecForLocale(). void QTextCodec::setCodecForTr ( QTextCodec * c ) [static] Sets the codec used by QObject::tr() on its argument to c. If c is 0 (the default), tr() assumes Latin-1. If the literal quoted text in the program is not in the Latin-1 encoding, this function can be used to set the appropriate encoding. For example, software developed by Korean programmers might use eucKR for all the text in the program, in which case the main() function might look like this: int main(int argc, char *argv[]) { QApplication app(argc, argv); QTextCodec::setCodecForTr(QTextCodec::codecForName("eucKR")); ... } Note that this is not the way to select the encoding that the user has chosen. For example, to convert an application containing literal English strings to Korean, all that is needed is for the English strings to be passed through tr() and for translation files to be loaded. For details of internationalization, see Internationalization with Qt. Warning: This function is not reentrant. See also codecForTr() and setCodecForCStrings().
zhxianbin 2015-02-12
  • 打赏
  • 举报
回复
把收到的数据以 16 进制打印出来看看是什么编码
zhxianbin 2015-02-12
  • 打赏
  • 举报
回复
无论是 GB2312 还是 UTF8 都可以正常显示的,不一定非要转换成 UTF8
oathupdate 2015-02-12
  • 打赏
  • 举报
回复
引用 13 楼 zhxianbin 的回复:
引用 12 楼 oathupdate 的回复:
引用 8 楼 zhxianbin 的回复:
把收到的数据以 16 进制打印出来看看是什么编码
吧十六进制的打印出来了 可以就是几个数字怎么看是什么编码呢
http://ipseeker.cn/tools/pywb.php
我弄好了 谢谢 是根据这个代码弄好的。可能是我没表达清楚 谢谢了 int GB2312ToUTF8(char* srcStr, char* desBuff, size_t desBuffLength) { assert(strlen(srcStr)>0); size_t iLen = strlen(srcStr); iconv_t cd; cd = iconv_open("utf-8","gb2312"); assert(cd != 0); iconv(cd, &srcStr, &iLen, &desBuff, &desBuffLength); iconv_close(cd); return desBuffLength; }
oathupdate 2015-02-12
  • 打赏
  • 举报
回复
分还可以追加啊
oathupdate 2015-02-12
  • 打赏
  • 举报
回复
引用 5 楼 nidexiaojiaoya 的回复:
顶 起
谢谢
oathupdate 2015-02-12
  • 打赏
  • 举报
回复
有木有人啊。求帮助
oathupdate 2015-02-12
  • 打赏
  • 举报
回复
引用 1 楼 zhxianbin 的回复:
发送时也要使用 UTF8 编码
我把那个字符集换成GBK了 我看了下我windows下默认的字符编码就是GBK 可是还是不行啊。问题还没有解决
zhxianbin 2015-02-12
  • 打赏
  • 举报
回复
引用 12 楼 oathupdate 的回复:
引用 8 楼 zhxianbin 的回复:
把收到的数据以 16 进制打印出来看看是什么编码
吧十六进制的打印出来了 可以就是几个数字怎么看是什么编码呢
http://ipseeker.cn/tools/pywb.php
oathupdate 2015-02-12
  • 打赏
  • 举报
回复
引用 8 楼 zhxianbin 的回复:
把收到的数据以 16 进制打印出来看看是什么编码
吧十六进制的打印出来了 可以就是几个数字怎么看是什么编码呢
oathupdate 2015-02-12
  • 打赏
  • 举报
回复
05442188
引用 10 楼 zhxianbin 的回复:
http://qt-project.org/doc/qt-4.8/QTextCodec.html QTextCodec::setCodecForCStrings QTextCodec::setCodecForTr
我也看了 网上都说在QT5中这两句代码已经被去掉了 只要加哪一句代码就行了
zhxianbin 2015-02-11
  • 打赏
  • 举报
回复
发送时也要使用 UTF8 编码
oathupdate 2015-02-11
  • 打赏
  • 举报
回复
引用 1 楼 zhxianbin 的回复:
发送时也要使用 UTF8 编码
我去试试

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧