在使用QT编程时,有时会遇到代码中添加中文的时候提示 “常量中有换行符”,而将中文替换成英文时就不会出现此类错误,出现该错误的原因时因为文件的编码问题导致的。
解决此类问题方法:
1.点击工具 =》 文本编辑器 =》行为 =》 将默认编码设置成UTF-8 在将下面方框设置成:如果编码是UTF-8则添加!
2.编辑 =》 select encoding... =》UTF-8 按编码保存
这样 编译错误错误就不会在出现了,但是这出现另一个问题,你会应该是中文的地方出现的确是乱码
出现这个情况的时候我们只需要在你输入中文的地方,用 QStringLiteral
宏包裹一下就可以了
例如:
QStringLiteral("乱码");
QStringLiteral
The macro generates the data for a QString out of str at compile time if the compiler supports it. Creating a QString from it is free in this case, and the generated string data is stored in the read-only segment of the compiled object file.
For compilers not supporting the creation of compile time strings, QStringLiteral will fall back to QString::fromUtf8().。。。。。。。。。
具体可参考官方文档