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. How to save file in specific encoding?
Forum Updated to NodeBB v4.3 + New Features

How to save file in specific encoding?

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 5 Posters 5.2k Views 3 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.
  • Y Offline
    Y Offline
    Yet Zio
    wrote on 14 Apr 2020, 14:42 last edited by
    #1

    I tried the examples and referred the docs, but I am still not able to resolve this issue. I used the setCodec method to set the encoding to QTextStream output but when I open the document in another text editor, it shows UTF-8 although I have not set the codec to UTF-8. How can I save a file in different encoding other than UTF-8, is it possible in Qt?

    ```Securing is more important than developing```

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 14 Apr 2020, 14:44 last edited by
      #2

      @Yet-Zio said in How to save file in specific encoding?:

      I have not set the codec to UTF-8

      You have: QTextStream::setCodec()

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • Y Offline
        Y Offline
        Yet Zio
        wrote on 14 Apr 2020, 14:52 last edited by
        #3

        But the docs say this for setCodec() : Sets the codec for this stream to the QTextCodec for the encoding specified by codecName. Common values for codecName include "ISO 8859-1", "UTF-8", and "UTF-16". If the encoding isn't recognized, nothing happens.

        You mean I should not use setCodec() ?

        ```Securing is more important than developing```

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 14 Apr 2020, 14:54 last edited by
          #4

          @Yet-Zio said in How to save file in specific encoding?:

          You mean I should not use setCodec() ?

          No, I meant that you have to use setCodec() when you want something != UTF-8 since UTF-8 is the default: By default, QTextCodec::codecForLocale() is used, and automatic unicode detection is enabled.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • Y Offline
            Y Offline
            Yet Zio
            wrote on 14 Apr 2020, 15:03 last edited by
            #5

            I used setCodec("Windows-1250") for test but that doesn't seem to work. It shows UTF-8 in another text editor.

            ```Securing is more important than developing```

            1 Reply Last reply
            0
            • F Offline
              F Offline
              fcarney
              wrote on 14 Apr 2020, 15:13 last edited by
              #6

              Not recognized then:

              for(auto codecstr: QTextCodec::availableCodecs())
                          if(codecstr.startsWith("windows-"))
                              qInfo() << codecstr;
              

              Apparently it starts with lower case. Man there are a LOT of codecs in there (806 on linux).

              C++ is a perfectly valid school of magic.

              1 Reply Last reply
              0
              • Y Offline
                Y Offline
                Yet Zio
                wrote on 14 Apr 2020, 15:16 last edited by
                #7

                I tried several aliases before too and with Other encodings for with setCodec(codecName)., it doesn't work even if its lower-case. You can try for yourself.

                ```Securing is more important than developing```

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 14 Apr 2020, 15:21 last edited by
                  #8

                  Please provide a minimal testcase.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  0
                  • Y Offline
                    Y Offline
                    Yet Zio
                    wrote on 14 Apr 2020, 15:34 last edited by
                    #9
                    // This is the code used for save method.
                    QString fileName;
                        // If we don't have a filename from before, get one.
                        if (currentFile.isEmpty()) {
                            fileName = QFileDialog::getSaveFileName(this, "Save");
                            currentFile = fileName;
                        } else {
                            fileName = currentFile;
                        }
                        QFile file(fileName);
                        if (!file.open(QIODevice::WriteOnly)) { // If i use QIODevice::Text, then it can't handle line endings.
                            warnMsgBox *openWarnBox = new warnMsgBox(this);
                            openWarnBox->setText("Cannot save file");
                            openWarnBox->setDetailedText(file.errorString());
                            QSpacerItem* warnhorizontalSpacer = new QSpacerItem(500, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
                            QGridLayout* warnlayout = (QGridLayout*)openWarnBox->layout();
                            warnlayout->addItem(warnhorizontalSpacer, warnlayout->rowCount(), 0, 1, warnlayout->columnCount());
                            openWarnBox->exec();
                            return;
                        }
                        QFileInfo fileInfo(file);
                        ui->tabWidget->setTabText(ui->tabWidget->currentIndex(), fileInfo.fileName());
                        CodeEditor* tempEditor = (CodeEditor*)ui->tabWidget->widget(ui->tabWidget->currentIndex());
                        tempEditor->changesSet = false;
                    
                        QTextStream out(&file);
                        QByteArray tempCodec;
                        tempCodec.append(tempEditor->codecToUse);
                        QTextCodec *fileCodec = QTextCodec::codecForName(tempCodec);
                        out.setCodec(fileCodec);
                        out << tempEditor->toPlainText();
                        file.close();
                    

                    The codecToUse is a personal variable used for knowing the encoding used for current editor from when the file was opened. This is then set to save the file with QTextStream output with codecToUse.

                    If this was a problem with codecToUse, then i tried manually with "windows-1250" and others such as "ISO-..." and still the same prob

                    ```Securing is more important than developing```

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      fcarney
                      wrote on 14 Apr 2020, 15:52 last edited by
                      #10

                      Make sure you are writing non trivial text that contains encoded characters. If not it will not have anything to determine what the file type is. Even then it might get it wrong. I inserted an emoji into the text to get a linux program called "uchardet" to check file types. If I didn't include an emoji in the file it would always return ASCII. If I insert an emoji it will return UTF-8 when I encode it UTF-8. If I encode windows-1250 the program returns ISO-8859-3. I have no idea what that means. However, the encoding it definitely different if non trivial data is in the file.

                              QFile testfile("output.txt");
                              if (testfile.open(QFile::WriteOnly | QFile::Truncate)){
                                  QTextStream out(&testfile);
                                  out.setCodec("windows-1250");
                                  //out.setCodec("UTF-8");
                                  qInfo() << out.codec()->name();
                                  out << "💩Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
                                  out << "\n";
                                  out << "💩Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.";
                                  out << "\n";
                              }
                      

                      C++ is a perfectly valid school of magic.

                      1 Reply Last reply
                      2
                      • Y Offline
                        Y Offline
                        Yet Zio
                        wrote on 14 Apr 2020, 15:58 last edited by
                        #11

                        Thanks. However I can convert characters easily between encodings, it seems there's no way to figure out the exact encoding which was used to write the file.

                        ```Securing is more important than developing```

                        1 Reply Last reply
                        1
                        • C Offline
                          C Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on 14 Apr 2020, 16:23 last edited by
                          #12

                          @Yet-Zio said in How to save file in specific encoding?:

                          there's no way to figure out the exact encoding which was used to write the file.

                          no, since e.g. ascii is a subset of utf-8 and others also have common stuff so how should it be possible.

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          Pablo J. RoginaP 1 Reply Last reply 14 Apr 2020, 17:37
                          1
                          • C Christian Ehrlicher
                            14 Apr 2020, 16:23

                            @Yet-Zio said in How to save file in specific encoding?:

                            there's no way to figure out the exact encoding which was used to write the file.

                            no, since e.g. ascii is a subset of utf-8 and others also have common stuff so how should it be possible.

                            Pablo J. RoginaP Offline
                            Pablo J. RoginaP Offline
                            Pablo J. Rogina
                            wrote on 14 Apr 2020, 17:37 last edited by
                            #13

                            @Christian-Ehrlicher said in How to save file in specific encoding?:

                            no, since e.g. ascii is a subset of utf-8

                            you could eventually detect the BOM for Unicode encoded files

                            Upvote the answer(s) that helped you solve the issue
                            Use "Topic Tools" button to mark your post as Solved
                            Add screenshots via postimage.org
                            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                            C 1 Reply Last reply 14 Apr 2020, 17:41
                            0
                            • Pablo J. RoginaP Pablo J. Rogina
                              14 Apr 2020, 17:37

                              @Christian-Ehrlicher said in How to save file in specific encoding?:

                              no, since e.g. ascii is a subset of utf-8

                              you could eventually detect the BOM for Unicode encoded files

                              C Offline
                              C Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 14 Apr 2020, 17:41 last edited by
                              #14

                              @Pablo-J-Rogina But it's not mandatory. If there is a BOM then QTextStream will respect it.

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                SimonSchroeder
                                wrote on 15 Apr 2020, 06:51 last edited by
                                #15

                                Maybe to extend on what @fcarney has said: The encoding of a file is usually not stored in the file itself and needs to be guessed. Even for UTF8 the BOM (which would help with detection) is optional. Most text editors look at the full text and then try to guess the encoding. So, one question would be if the text editor you are using for verification is able to guess your encoding.

                                Then, obviously, the text file you are looking at needs something in there to be able to distinguish encodings. If you use plain ASCII characters (only 128 characters, the first 128 in Unicode) then the editor is allowed to guess any encoding it wants. So, in order for us to be able to help you should also show the text you are trying to save.

                                One last thing is that I am not sure how Qt would handle text (which is Unicode internally) with characters that are not in the encoding you are using for saving. One possibility would be that it just writes the unicode character as UTF8 (though I don't know). In that case it could happen that the editor guesses UTF8 again (though I find this unlikely).

                                Anyway, you should create text specific to the encoding you are trying out. You cannot have one text (hopefully with non-ASCII characters as otherwise it would be a futile exercise) and try to save it with any encodings just for fun. Encoding and text have to be a match.

                                1 Reply Last reply
                                1

                                1/15

                                14 Apr 2020, 14:42

                                • Login

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