I am using QT Creator, created a console app. Everything is up to date. OS is Windows XP.
I have created a QString that holds some Hungarian chars. Most of the Hungarian chars do not require unicode but the chars that have double slashes for accents require unicode.
I try to write the QString contents to a file but my unicode chars lose their accents in the file. In other words, the unicode info is lost along the way.
My code is bellow.
#include <QtCore/QCoreApplication>
#include <QString>
#include <QTextStream>
#include <QDate>
#include <QFile>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QString szqLine = "NON-UnicodeIsOK: áéüúöóí NEED-Unicode: űő";
//These are Hungarian chars and require unicode. Actually, only the u & o, each having double
//slashes for eccents require unicode encoding.
//Open file for writing unicode chars to.
QFile file("out.txt");
if ( !file.open(QIODevice::WriteOnly | QIODevice::Text) ){
return 1;
}
//Stream the QString text to the file.
QTextStream out(&file);
out.setCodec("UTF-8");
out << szqLine << endl; //Use endl for flush. Does not properly write ű and ő chars.
//Accents missing in file.
file.close(); //Done with file.
return app.exec();
}
What is the encoding of your file? Using non-ascii encodings in source files often causes problems, at least when working cross-platform. I think MSVC has some problems there.
QString foo = "unicode string" uses the implicit conversion from ascii to unicode, which will also cause problems. Always explicitely specify what encoding the literal uses, e.g. by wrapping the literal using QLatin1String() if it's latin1:
QString foo = QLatin1String("some latin1 string");
or, utf-8, as it should be in your case, QString::fromUtf8():
QString foo = QString::fromUtf8( "funny characters" );
To avoid such errors, I prefer to keep source files pure ascii, with english strings, and then translate them using Qt's internationalization tools.
Edit: Also see the accepted answer to this question about UTF-8 literals in MSVC 2008.
Are you sure that szqLine really contains the correct characters? Try this: QString line = QString::fromStdWString(L"NON-UnicodeIsOK: \x00E1\x00E9... NEED-Unicode: \x+0171\x0151";
... and don't use Hungarian notation ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With