Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QT Can not write to a unicode file unicode strings

Tags:

qt

qt4

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();
}
like image 753
user440297 Avatar asked Nov 29 '10 03:11

user440297


2 Answers

  1. 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.

  2. 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" );
  1. Before writing the string to a file (which is another source for possible errors, although your code looks correct), check if a qt widget (QLineEdit, for example) displays it correctly.

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.

like image 177
Frank Osterfeld Avatar answered Oct 05 '22 19:10

Frank Osterfeld


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 ;-)

like image 36
hmuelner Avatar answered Oct 05 '22 19:10

hmuelner