我学习Qt,使用Qt5.1,我在重音方面有编码问题。我用葡萄牙语编写输出程序,并且总是要使用口音。这个问题可以在以下代码中演示:
\#include < QDebug>
\#include < QTextStream>
\#include < QString>
int main(){
QTextStream out(stdout);
qDebug() << "Olá Mundo!";
out << QString("Olá Mundo!") << endl;
out << "Olá Mundo!" << endl;
}产出如下:
奥兰蒙多!
奥兰蒙多!
圣保罗!
第三个"OláMundo“是错的。我总是必须使用QString来正确地打印输出,或者有什么方法可以正确地打印最后一行(没有Qstring声明)?为什么qDebug显示正确?
发布于 2013-08-25 01:51:46
您可以将out的编解码器设置为ISO8859-1 (out.setCodec("ISO 8859-1")).然后您可以编写out << "Olá Mundo!" << endl;,但是out << QString("Olá Mundo!") << endl;就不再起作用了。这仅仅是因为QTextStream operator<<(const * string)在ISO8859-1中需要字符串,而operator<<(const QString & string)是UTF-8中的QString。QTextStream的默认编解码器是UTF-8.
qDebug在内部将字符串转换为UTF-8。这就是为什么你可以写qDebug() << "Olá Mundo!";
https://stackoverflow.com/questions/18424778
复制相似问题