Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTextEdit does not display german Umlauts (öÖüÜäÄß) after loading a textfile

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

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 5 Posters 6.4k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mbergmann-sh
    wrote on last edited by
    #1

    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:

    SAVE:

    // 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.setAutoDetectUnicode(true);
          stream << ui->textEdit_Report->toHtml();
          stream.flush();
          file.close();
         }
        }
    }
    

    LOAD:

    // 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"));
           return;
          }
    
          QString contents = file.readAll().constData();
          ui->textEdit_Report->setHtml(contents);
          file.close();
         }
    }
    

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

    1 Reply Last reply
    0
    • kshegunovK kshegunov

      As I said, you should rather use:

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

      and for reading:

      QString contents = QString::fromUtf8(file.readAll());
      
      M Offline
      M Offline
      mbergmann-sh
      wrote on last edited by
      #9

      @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! :)

      kshegunovK 1 Reply Last reply
      0
      • M Offline
        M Offline
        mulfycrowh
        wrote on last edited by
        #2

        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.

        M 1 Reply Last reply
        0
        • M mulfycrowh

          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.

          M Offline
          M Offline
          mbergmann-sh
          wrote on last edited by
          #3

          @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?

          kshegunovK 1 Reply Last reply
          0
          • R Offline
            R Offline
            raspe88
            wrote on last edited by
            #4

            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.

            M 2 Replies Last reply
            1
            • M mbergmann-sh

              @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?

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #5

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

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              1
              • R raspe88

                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.

                M Offline
                M Offline
                mbergmann-sh
                wrote on last edited by
                #6

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

                kshegunovK 1 Reply Last reply
                0
                • M mbergmann-sh

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

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #7

                  As I said, you should rather use:

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

                  and for reading:

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

                  Read and abide by the Qt Code of Conduct

                  M 2 Replies Last reply
                  2
                  • R raspe88

                    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.

                    M Offline
                    M Offline
                    mbergmann-sh
                    wrote on last edited by
                    #8

                    @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. :)

                    1 Reply Last reply
                    0
                    • kshegunovK kshegunov

                      As I said, you should rather use:

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

                      and for reading:

                      QString contents = QString::fromUtf8(file.readAll());
                      
                      M Offline
                      M Offline
                      mbergmann-sh
                      wrote on last edited by
                      #9

                      @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! :)

                      kshegunovK 1 Reply Last reply
                      0
                      • M mbergmann-sh

                        @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! :)

                        kshegunovK Offline
                        kshegunovK Offline
                        kshegunov
                        Moderators
                        wrote on last edited by
                        #10

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

                        Read and abide by the Qt Code of Conduct

                        M 1 Reply Last reply
                        0
                        • kshegunovK kshegunov

                          As I said, you should rather use:

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

                          and for reading:

                          QString contents = QString::fromUtf8(file.readAll());
                          
                          M Offline
                          M Offline
                          mbergmann-sh
                          wrote on last edited by
                          #11

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

                          kshegunovK 1 Reply Last reply
                          0
                          • M mbergmann-sh

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

                            kshegunovK Offline
                            kshegunovK Offline
                            kshegunov
                            Moderators
                            wrote on last edited by
                            #12

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

                            Read and abide by the Qt Code of Conduct

                            1 Reply Last reply
                            1
                            • kshegunovK kshegunov

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

                              M Offline
                              M Offline
                              mbergmann-sh
                              wrote on last edited by
                              #13

                              @kshegunov oh yes, she is :D

                              1 Reply Last reply
                              0
                              • ? Offline
                                ? Offline
                                A Former User
                                wrote on last edited by
                                #14

                                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.

                                1 Reply Last reply
                                2

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved