QT Unable to write to unicode unicode

I use QT Creator, I create a console application. All in the know. OS - Windows XP.

I created a QString that contains several Hungarian characters. Most Hungarian characters do not require Unicode, but characters that have double slashes for emphasis require Unicode.

I am trying to write the contents of a QString to a file, but my unicode characters lose their accents in the file. In other words, Unicode information is lost along the way.

My code is below.

#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();
}
+5
source share
2 answers
  • ? non-ascii , , . , MSVC .

  • QString foo = "unicode string" ascii unicode, . , , . QLatin1String(), latin1:

    QString foo = QLatin1String ( "some latin1 string" );

, utf-8, , QString:: fromUtf8():

QString foo = QString::fromUtf8( "funny characters" );
  1. ( , ), , (QLineEdit).

, ascii, , Qt.

: . UTF-8 MSVC 2008.

+6

, szqLine ? : QString line = QString:: fromStdWString (L "NON-UnicodeIsOK:\x00E1\x00E9... NEED-:\x + 0171\x0151";

... ; -)

+1

All Articles