I learning Qt, with Qt5.1 and I'm have coding problems in accentuation. I write programs with outputs in Portuguese and always have to use accents. The problem can be demonstrated in the following code:
\#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" is Hello World in Portuguese.
The output is:
Olá Mundo!
Olá Mundo!
Olá Mundo!
This third "Olá Mundo" is wrong. I always have to use QString to print the output correctly, or is there some way to make the last line print correctly (without Qstring declaration)? And why qDebug shows correctly?
You can set the codec for out to ISO 8859-1 (
out.setCodec("ISO 8859-1")
). Then you can writeout << "Olá Mundo!" << endl;
But thenout << QString("Olá Mundo!") << endl;
won't function anymore. This is just because the QTextStream operator<<(const char * string) expects a string in ISO 8859-1 and operator<<(const QString & string) a QString in UTF-8. The default codec for QTextStream is UTF-8.qDebug converts the string internally into UTF-8. That's why you can write
qDebug() << "Olá Mundo!";