16,202
社区成员
发帖
与我相关
我的任务
分享
其实用QStringLiteral 就可以,测试几个,还未发现乱码,感觉每次要打中文,要多打几个字母,不太好。 版本是 QT5.3.2(MSVC 2010,32BIT),只有一个函数 QTextCodec::setCodecForLocale(),您说不能用。 用你们说的设置编码,我都还没成功过。
1. 一个很简单的问题,往往很难几乎话说清楚。对 Qt4 和 GCC 来说,默认情况下,它的执行字符集 可以认为和源码字符集一样,也就是说,setCodecForCStrings("utf-8") 还是 setCodecForCStrings("gtk"),还要取决于 用户 的源码字符集 2. 不好意思,没注意到Qt文档组何时加上的这个表述。移除这个东西的初衷是https://codereview.qt-project.org/#/c/14626/ Remove custom text codec for C strings. This setting is extremely harmful, as code cannot know whether or not to expect it. It also made the behaviour of QString::fromAscii and ::toAscii unintuitive, and caused a lot of people to make mistakes with it.
Remove custom text codec for C strings. This setting is extremely harmful, as code cannot know whether or not to expect it. It also made the behaviour of QString::fromAscii and ::toAscii unintuitive, and caused a lot of people to make mistakes with it.
[quote=引用 10 楼 Kenier 的回复:]
Qt4把字面字符串常量传给QString的构造函数时,使用的是QString::fromAscii,QString::fromAscii实际使用QTextCodec::codecForCStrings来解析,Qt4中QTextCodec::codecForCStrings默认为utf-8。在gcc中,运行字符集默认为utf-8,所以用gcc构建程序时中文可以直接QString("中文"),但是用MSVC时,运行字符集默认为ANSI(即GBK),所以只需要调用QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());即可。
Qt5中因为性能方面的考虑去掉了QString::fromAscii、QTextCodec::codecForCStrings和QTextCodec::setCodecForCStrings,把字面字符串常量传给QString的构造函数时,使用的是QString::fromUtf8,用gcc没问题。MSVC需要用QString::fromLocal8Bit("中文")或者用QStringLiteral("中文")产生一个编译时的QString。
Qt4把字面字符串常量传给QString的构造函数时,使用的是QString::fromAscii,QString::fromAscii实际使用QTextCodec::codecForCStrings来解析,Qt4中QTextCodec::codecForCStrings默认为utf-8。在gcc中,运行字符集默认为utf-8,所以用gcc构建程序时中文可以直接QString("中文"),但是用MSVC时,运行字符集默认为ANSI(即GBK),所以只需要调用QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());即可。 Qt5中因为性能方面的考虑去掉了QString::fromAscii、QTextCodec::codecForCStrings和QTextCodec::setCodecForCStrings,把字面字符串常量传给QString的构造函数时,使用的是QString::fromUtf8,用gcc没问题。MSVC需要用QString::fromLocal8Bit("中文")或者用QStringLiteral("中文")产生一个编译时的QString。
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GBK")); 这句报错啊,怎么找不到函数setCodecForCStrings,错误信息如下: error: C2039: “setCodecForCStrings”: 不是“QTextCodec”的成员
// 解决中文乱码 QTextCodec *codec = QTextCodec::codecForLocale(); QTextCodec::setCodecForTr(codec); QTextCodec::setCodecForLocale(codec); QTextCodec::setCodecForCStrings(codec);
// 解决中文乱码
QTextCodec *codec = QTextCodec::codecForLocale();
QTextCodec::setCodecForTr(codec);
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);