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. Can I read/write Windows-1252 and other legacy encodings in Qt 6?
Forum Updated to NodeBB v4.3 + New Features

Can I read/write Windows-1252 and other legacy encodings in Qt 6?

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 7 Posters 4.0k 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.
  • AndyBriceA Offline
    AndyBriceA Offline
    AndyBrice
    wrote on last edited by
    #10

    I guess my other option is to build a command line encoding converter in Qt 5 and call it from my Qt 6 application. Hardly ideal though.

    B 1 Reply Last reply
    0
    • A Offline
      A Offline
      ankou29666
      wrote on last edited by
      #11

      seems like the QTextCodec in Qt5 compatibility module supports Win1250 to 1258 endodings.
      https://doc.qt.io/qt-6/qtextcodec.html

      AndyBriceA 1 Reply Last reply
      1
      • AndyBriceA AndyBrice

        I guess my other option is to build a command line encoding converter in Qt 5 and call it from my Qt 6 application. Hardly ideal though.

        B Offline
        B Offline
        Bonnie
        wrote on last edited by Bonnie
        #12

        @AndyBrice Or you can link to icu and use its api your self just like what Qt did in its internal codes, or even use other thirdparty codec libraries.

        AndyBriceA 1 Reply Last reply
        3
        • A ankou29666

          seems like the QTextCodec in Qt5 compatibility module supports Win1250 to 1258 endodings.
          https://doc.qt.io/qt-6/qtextcodec.html

          AndyBriceA Offline
          AndyBriceA Offline
          AndyBrice
          wrote on last edited by
          #13

          @ankou29666 said in Can I read/write Windows-1252 and other legacy encodings in Qt 6?:

          seems like the QTextCodec in Qt5 compatibility module supports Win1250 to 1258 endodings.
          https://doc.qt.io/qt-6/qtextcodec.html

          Ok, I didn't spot that. Thanks.

          So I can install handle these encodings, but to read windows-1252 encoding where I used to do this in Qt 5:

          QFile f( path );
          if ( f.open( QIODevice::ReadOnly ) )
          {
            QTextStream t( &f );
            QTextCodec* codec = QTextCodec::codecForName( "windows-1252" );
            t.setCodec( codec );
            ...
          }
          
          

          I have to do this in Qt 6:

          QByteArray encodedString = "..."; // read from file
          QTextCodec* codec = QTextCodec::codecForName("windows-1252");
          QString unencodedString = codec->toUnicode(encodedString);
          

          Is that right?

          1 Reply Last reply
          0
          • I Offline
            I Offline
            IgKh
            wrote on last edited by
            #14

            If willing to use a sub process, you can always use a iconv binary to pre/post process input or output to/from UTF-16. There are many ways to tackle this issue, which is probably part of the reason no one was motivated enough so far to push the patch to extend Qt 6's QTextCodec to the finish line.

            AndyBriceA 1 Reply Last reply
            1
            • B Bonnie

              @AndyBrice Or you can link to icu and use its api your self just like what Qt did in its internal codes, or even use other thirdparty codec libraries.

              AndyBriceA Offline
              AndyBriceA Offline
              AndyBrice
              wrote on last edited by
              #15

              @Bonnie said in Can I read/write Windows-1252 and other legacy encodings in Qt 6?:

              Or you can link to icu and use its api your self just like what Qt did in its internal codes, or even use other thirdparty codec libraries.

              Are their prebuilt ICU binaries for Windows and Mac? I had a quick look on https://unicode-org.github.io/icu/, but didn't see them.

              Do you know what the licensing of the binaries is? If they are GPL, I won't be able to use them in my commercial product.

              S 1 Reply Last reply
              0
              • I IgKh

                If willing to use a sub process, you can always use a iconv binary to pre/post process input or output to/from UTF-16. There are many ways to tackle this issue, which is probably part of the reason no one was motivated enough so far to push the patch to extend Qt 6's QTextCodec to the finish line.

                AndyBriceA Offline
                AndyBriceA Offline
                AndyBrice
                wrote on last edited by
                #16

                @IgKh said in Can I read/write Windows-1252 and other legacy encodings in Qt 6?:

                If willing to use a sub process, you can always use a iconv binary to pre/post process input or output to/from UTF-16.

                Is iconv related to the ICU libraries, or completely different?

                I 1 Reply Last reply
                0
                • AndyBriceA AndyBrice

                  @IgKh said in Can I read/write Windows-1252 and other legacy encodings in Qt 6?:

                  If willing to use a sub process, you can always use a iconv binary to pre/post process input or output to/from UTF-16.

                  Is iconv related to the ICU libraries, or completely different?

                  I Offline
                  I Offline
                  IgKh
                  wrote on last edited by
                  #17

                  @AndyBrice iconv isn't related to ICU, it is a very old POSIX API and a corresponding CLI binary that's included in every UNIX-like/Linux system and it is not hard to find compatible Windows versions of it. Usually can work with any text encoding ever known to mankind.

                  It can be integrated using QProcess, i.e. something like:

                  QProcess* proc = new QProcess(parent);
                  proc->setStandardInputFile("path/to/input/file");
                  proc->start("path/to/iconv", QStringList() << "-f" << "WINDOWS-1252" << "-t" << "UTF16");
                  

                  A then the QProcess can be used as source device for QTextStream, since it is a kind of QIODevice. Likewise for the output.

                  AndyBriceA 1 Reply Last reply
                  1
                  • I IgKh

                    @AndyBrice iconv isn't related to ICU, it is a very old POSIX API and a corresponding CLI binary that's included in every UNIX-like/Linux system and it is not hard to find compatible Windows versions of it. Usually can work with any text encoding ever known to mankind.

                    It can be integrated using QProcess, i.e. something like:

                    QProcess* proc = new QProcess(parent);
                    proc->setStandardInputFile("path/to/input/file");
                    proc->start("path/to/iconv", QStringList() << "-f" << "WINDOWS-1252" << "-t" << "UTF16");
                    

                    A then the QProcess can be used as source device for QTextStream, since it is a kind of QIODevice. Likewise for the output.

                    AndyBriceA Offline
                    AndyBriceA Offline
                    AndyBrice
                    wrote on last edited by
                    #18

                    @IgKh Ok, thanks for the explanation.

                    1 Reply Last reply
                    0
                    • AndyBriceA AndyBrice

                      @Bonnie said in Can I read/write Windows-1252 and other legacy encodings in Qt 6?:

                      Or you can link to icu and use its api your self just like what Qt did in its internal codes, or even use other thirdparty codec libraries.

                      Are their prebuilt ICU binaries for Windows and Mac? I had a quick look on https://unicode-org.github.io/icu/, but didn't see them.

                      Do you know what the licensing of the binaries is? If they are GPL, I won't be able to use them in my commercial product.

                      S Offline
                      S Offline
                      SimonSchroeder
                      wrote on last edited by
                      #19

                      @AndyBrice said in Can I read/write Windows-1252 and other legacy encodings in Qt 6?:

                      Are their prebuilt ICU binaries for Windows and Mac? I had a quick look on https://unicode-org.github.io/icu/, but didn't see them.

                      Binaries are located on their GitHub page under release: https://github.com/unicode-org/icu/releases/tag/release-76-rc. Upon a quick glance I'm not sure if any of these are for macOS, though.

                      @AndyBrice said in Can I read/write Windows-1252 and other legacy encodings in Qt 6?:

                      Do you know what the licensing of the binaries is? If they are GPL, I won't be able to use them in my commercial product.

                      They have a lilst of all the licenses (including 3rd party) that apply: https://github.com/unicode-org/icu?tab=License-1-ov-file. ICU itself seems to be very permissive. Some of the 3rd party libs seem to require a mention with their copyright notice. Overall it should be useable for commercial products.

                      1 Reply Last reply
                      1
                      • AndyBriceA Offline
                        AndyBriceA Offline
                        AndyBrice
                        wrote on last edited by
                        #20

                        Thanks, Simon.

                        1 Reply Last reply
                        0

                        • Login

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