QTextEdit does not display german Umlauts (öÖüÜäÄß) after loading a textfile

  • I'm working on a small app that saves and opens reports in HTML format from within a QTextEdit. I'm on Windows, using Qt 5.7 with Qt Creator v4.1.0.

    Problem: Even though Umlauts are shown correctly in my QTextedit, they are replaced by diamonts an question marks after beein reloaded into the QTextEdit. I'm using QTextStream->toHtml to save the file and file.readAll().constData() to get them back, putting the gathered string back to QTextEdit with setHtml.

    Here's part of my code:


    // Datei speichern
    void MainWindow::on_actionSpeichern_triggered()
        QString fileName = QFileDialog::getSaveFileName(this, trUtf8("Datei speichern"), "",
         trUtf8("Reportdatei (*.bmi)"));
        if (fileName != "") {
         QFile file(fileName);
         if (!file.open(QIODevice::WriteOnly)) {
          // error message
         } else {
          QTextStream stream(&file);
          stream << ui->textEdit_Report->toHtml();


    // Report laden
    void MainWindow::on_actionReport_importieren_triggered()
        QString fileName = QFileDialog::getOpenFileName(this, trUtf8("Datei öffnen"), "",
          trUtf8("Reportdatei (*.bmi)"));
         if (fileName != "") {
          QFile file(fileName);
          if (!file.open(QIODevice::ReadOnly)) {
           QMessageBox::critical(this, tr("Fehler"),
           tr("Report konnte nicht geöffnet werden"));
          QString contents = file.readAll().constData();

    I researched on Codec and Locale a bit, but can't figure out what to do. Any help appreciated!

  • QTextEdit doesn't display these characters because of extended ASCII codes, I mean greater than 127.
    When you encounter such characters you can convert them to QChar and add them to the QString.

  • @mulfycrowh Thanks for your hint! :)

    QTextEdit displays äÄöÖüÜß when I type them in or put them via QString. it does NOT display them anymore after I saved the text into a file and reload this file. The saved file looks like this:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
    <html><head><meta name="qrichtext" content="1" /><title>Report</title><style type="text/css">
    p, li { white-space: pre-wrap; }
    </style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;">
    <p style=" margin-top:14px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:large; font-weight:600; color:#0000ff;">Haus &quot;An Mühlendiek&quot; - Gewichte und BMI der Kunden</span></p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" color:#0000ff;">bearbeitet am Sonntag, 25.09.2016 für </span><span style=" font-weight:600; color:#0000ff;">September 2016</span></p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">=================================================================================<br /></p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-weight:600;">Musterfrau, Frieda Annakatrin</span> | Gewicht: <span style=" font-weight:600;">103.20</span> kg | BMI: <span style=" font-weight:600;">27.71</span> | erfasst am 25.09.2016</p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" color:#0000ff;">------------------------------------------------------------------------------------------------------------------------------------------------------------------</span></p></body></html>

    By the way - the same app displays everything well when compiled under Linux. so I guess it has to to with encoding the charset. Is there a Way to force QTextEdit to use Windows system charset?

  • I think the problem you have is that when using QTextStream you write the content you store in an encoding that is different from what you get when calling file.readAll().constData() (because that gives you a plain ASCII-char-Array).

    Just call file.readAll(). QString has a contructor that knows a QByteArray and if you don't use a special encoding when writing your content with QTextStream, that should do the trick.
    You could also use the static public QString-functions to interpret the QByteArray received by file.readAll() in the encoding you want if you use a special encoding or a QTextStream again.

  • Qt Champions 2017

    @raspe88 is almost certainly correct. My advice is to stick to utf-8 and when saving enforce the encoding with QString::toUtf8, consequently when loading you convert the utf8 stream to string with QString::fromUtf8. And never do file.readAll().constData(), what you do is to leave QString guessing what you've given it as raw byte data. Do the reading/writing consistently - if you use QTextStream to write, then by all means, use it read the file as well.

  • @raspe88 Thanks for your hint. Unfortunally, this doesn't work for me on Windows, since I have to use

    stream << ui->textEdit_Report->toHtml();

    in order to save formatted text. when reloading with

    QString contents = file.readAll();

    the result is still without umlauts.

  • Qt Champions 2017

    As I said, you should rather use:

    QByteArray data = ui->textEdit_Report->toHtml().toUtf8();

    and for reading:

    QString contents = QString::fromUtf8(file.readAll());

  • @raspe88 Here's to illustrade what's happening:
    wrong encoding

    I'll try to change the routines accomplishing to kshegunow and'll be back later. :)

  • @kshegunov said in QTextEdit does not display german Umlauts (öÖüÜäÄß) after loading a textfile:

    QString contents = QString::fromUtf8(file.readAll());

    PERFECT! This solves my issu. Thanks a lot! :)

  • Qt Champions 2017

    You're welcome.
    Btw, Mrs. Onan is rather plump, isn't she? :D

  • @kshegunov Thanks, man. This was the solution. Works like a charm now. I guess I should dive deeper into Qt encoding and file operations ;)

  • Qt Champions 2017

    @mbergmann-sh said in QTextEdit does not display german Umlauts (öÖüÜäÄß) after loading a textfile:

    I guess I should dive deeper into Qt encoding and file operations

    Qt's documentation is pretty good, so that's a good place to start.

  • @kshegunov oh yes, she is :D

  • Another thing...

    (this, trUtf8("Datei öffnen"), "",

    Be careful using anything other than 7-bit ASCII for your source files as not all compilers might support that.

Log in to reply

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