[SOLVED] QTextStream don't likes umlauts

  • Hey,
    I try to write into a csv-file, which works perfectly for normal characters but it doesn't work with german umlauts. setCodec("UTF-8") worked for importing csv-files, seems to not fit here.

    Here is the code I using
    QString filename = QFileDialog::getSaveFileName(0, "DialogTitle", "filename.csv", "CSV files (.csv);;Zip files (.zip, *.7z)", 0, 0);
    QFile data(filename);
    if(data.open(QFile::WriteOnly |QFile::Truncate |QFile::))
    QTextStream output(&data);
    output << "ßäÄÖöÜü";
    maybe someone of you could give me a hint. :)

    Thanks in advance

  • Moderators

    setCodec sets encoding of the stream, not the input data. You pass your string as a const char* string literal which is "assumed to be Latin-1 encoded":http://qt-project.org/doc/qt-5/qtextstream.html#operator-lt-lt-16.
    You need to tell it what encoding it is really in.
    @output << QString::fromUtf8("ßäÄÖöÜü");
    //or just
    output << QString("ßäÄÖöÜü"); //this ctor assumes utf-8@
    In any case it's a bad idea to put national characters in source code.

  • Later it shouldn't really be in the code, It's just for testing. It's the input from the form which should take umlauts (streets in germany are called Straße; and so on...)

    I wasn't precise enough...my code snippet is only for testing and if I take the form input your solution will not work, wouldn't it?

    Edit: Problem solved

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.