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. Replacing calls to QTextStream::setCodec() in Qt 6
Forum Updated to NodeBB v4.3 + New Features

Replacing calls to QTextStream::setCodec() in Qt 6

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 7.8k Views
  • 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.
  • hskoglundH Offline
    hskoglundH Offline
    hskoglund
    wrote on last edited by
    #2

    Hi, don't know about "KOI8-R" but if you want Utf8 you can try:

    QFile f( m_path );
    if ( f.open( QIODevice::ReadOnly ) )
    {
        QTextStream t( &f );
        t.setEncoding(QStringConverter::Utf8);
        QString s = t.readAll();
    
    AndyBriceA 1 Reply Last reply
    1
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #3

      Since you are using QTextStream only to read the entire file as a single blob then you can just read it into a QByteArray and avoid any possible conversion that QTextStream may attempt:

      QTextCodec *codec = QTextCodec::codecForName("KOI8-R");
      QByteArray encodedString = f.readAll();
      QString string = codec->toUnicode(encodedString);
      
      AndyBriceA 1 Reply Last reply
      0
      • hskoglundH hskoglund

        Hi, don't know about "KOI8-R" but if you want Utf8 you can try:

        QFile f( m_path );
        if ( f.open( QIODevice::ReadOnly ) )
        {
            QTextStream t( &f );
            t.setEncoding(QStringConverter::Utf8);
            QString s = t.readAll();
        
        AndyBriceA Offline
        AndyBriceA Offline
        AndyBrice
        wrote on last edited by
        #4

        @hskoglund
        UTF-8 is supported by the new class, so that isn't a problem. It is encodings like "KOI8-R", "windows-1252" and "windows-1256" that are the issue.

        kkoehneK 1 Reply Last reply
        0
        • C ChrisW67

          Since you are using QTextStream only to read the entire file as a single blob then you can just read it into a QByteArray and avoid any possible conversion that QTextStream may attempt:

          QTextCodec *codec = QTextCodec::codecForName("KOI8-R");
          QByteArray encodedString = f.readAll();
          QString string = codec->toUnicode(encodedString);
          
          AndyBriceA Offline
          AndyBriceA Offline
          AndyBrice
          wrote on last edited by AndyBrice
          #5

          @ChrisW67

          And does readLine() work as well? Can QTextStream tell where the line boundaries are without the correct encoding?

          QFile f( m_path );
          if ( f.open( QIODevice::ReadOnly ) )
          {
              QTextStream t( &f );   
              QTextCodec *codec = QTextCodec::codecForName("KOI8-R");
              while ( !t.atEnd() )
              {
                  QString line = t.readLine();
                  line = codec->toUnicode( line );
          
          1 Reply Last reply
          0
          • AndyBriceA AndyBrice

            @hskoglund
            UTF-8 is supported by the new class, so that isn't a problem. It is encodings like "KOI8-R", "windows-1252" and "windows-1256" that are the issue.

            kkoehneK Offline
            kkoehneK Offline
            kkoehne
            Moderators
            wrote on last edited by
            #6

            @AndyBrice said in Replacing calls to QTextStream::setCodec() in Qt 6:

            UTF-8 is supported by the new class, so that isn't a problem. It is encodings like "KOI8-R", "windows-1252" and "windows-1256" that are the issue.

            It seems Qt 6.5 will get support for more encodings via ICU (again). Meanwhile, if you need proper support for KOI8-R, you can directly call ICU.

            And does readLine() work as well? Can QTextStream tell where the line boundaries are without the correct encoding?

            The definition of line end is not part of the encoding, but is rather an operating system thing. Files encoded in UTF-8 might e.g. have both \r\n and \n as line ending. If you check out qtextstream.cpp, you can see that the logic for detecting End of Line is indeed hardcoded to \r\n and \n (see QTextStreamPrivate::scan).

            Director R&D, The Qt Company

            AndyBriceA 1 Reply Last reply
            2
            • kkoehneK kkoehne

              @AndyBrice said in Replacing calls to QTextStream::setCodec() in Qt 6:

              UTF-8 is supported by the new class, so that isn't a problem. It is encodings like "KOI8-R", "windows-1252" and "windows-1256" that are the issue.

              It seems Qt 6.5 will get support for more encodings via ICU (again). Meanwhile, if you need proper support for KOI8-R, you can directly call ICU.

              And does readLine() work as well? Can QTextStream tell where the line boundaries are without the correct encoding?

              The definition of line end is not part of the encoding, but is rather an operating system thing. Files encoded in UTF-8 might e.g. have both \r\n and \n as line ending. If you check out qtextstream.cpp, you can see that the logic for detecting End of Line is indeed hardcoded to \r\n and \n (see QTextStreamPrivate::scan).

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

              @kkoehne
              ICU?

              1 Reply Last reply
              0
              • kkoehneK Offline
                kkoehneK Offline
                kkoehne
                Moderators
                wrote on last edited by
                #8

                ICU (https://icu.unicode.org/) is what Qt uses underneath, at least on Linux. It supports various exotic encodings.

                Director R&D, The Qt Company

                AndyBriceA 1 Reply Last reply
                1
                • kkoehneK kkoehne

                  ICU (https://icu.unicode.org/) is what Qt uses underneath, at least on Linux. It supports various exotic encodings.

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

                  @kkoehne
                  Do you know if/when Qt 6 (without the Qt 5 compatibility layer) is going to get support "KOI8-R", "windows-1252" and "windows-1256" etc on Windows and Mac?

                  kkoehneK 1 Reply Last reply
                  0
                  • hskoglundH Offline
                    hskoglundH Offline
                    hskoglund
                    wrote on last edited by
                    #10

                    For "windows-1252", if you don't need the characters 0x80 -- 0x9F, then you can use "Latin1" in Qt 6..3.1, e.g.:

                    t.setEncoding(QStringConverter::Latin1);
                    
                    1 Reply Last reply
                    1
                    • AndyBriceA AndyBrice

                      @kkoehne
                      Do you know if/when Qt 6 (without the Qt 5 compatibility layer) is going to get support "KOI8-R", "windows-1252" and "windows-1256" etc on Windows and Mac?

                      kkoehneK Offline
                      kkoehneK Offline
                      kkoehne
                      Moderators
                      wrote on last edited by
                      #11

                      @AndyBrice said in Replacing calls to QTextStream::setCodec() in Qt 6:

                      Do you know if/when Qt 6 (without the Qt 5 compatibility layer) is going to get support "KOI8-R", "windows-1252" and "windows-1256" etc on Windows and Mac?

                      The patch for supporting other encodings via ICU is in
                      https://codereview.qt-project.org/c/qt/qtbase/+/393373 , and should be part of Qt 6.4 . Anyhow, this requires Qt to be compiled with ICU support, which is not the case so far on Windows, but on macOS. Unless this changes, you might be out of luck there.

                      Note though that the mentioned encodings are already supported in Qt 6.3, if they are the default Windows encoding.

                      Director R&D, The Qt Company

                      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