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. [Solved]QTextStream and UTF-8 Problem
Forum Updated to NodeBB v4.3 + New Features

[Solved]QTextStream and UTF-8 Problem

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 16.0k Views 1 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.
  • O Offline
    O Offline
    obst8
    wrote on last edited by
    #1

    Hi all,
    I use QTextStream to read in a whole file in UTF-8 codec into a QString, make some modification in the QString, and write it to another QTextStream. But I find that the byte sequence E2 80 9C is output as C3 A2 C2 80 C2 9C, and E2 80 9D as C3 A2 C2 80 C2 9D (Actually this should be the double quotation marks). I've tried with setCodec("UTF-8") to

    1. both the input and output stream
    2. only the input stream
    3. only the output stream
    4. none of them
      but the result remains the same. I think this must have been caused by UTF-8 codec, and using pure C++ steam with string or C FILE with char* might solve this problem. But how to solve this problem with pure Qt code? Thanks in advance!

    Obst

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      Hi, please show us your code, and tell us what you are trying to do.

      We know that, QTextStream is used to load unicode string from encoded-raw-bytes-stream or save unicode string to encoded-raw-bytes-stream.

      If what you want is the similar behavior of C FILE/C++ iostream, you should use QFile directly.

      1 Reply Last reply
      0
      • O Offline
        O Offline
        obst8
        wrote on last edited by
        #3

        Hi all,
        Here is the code snippet dealing QFile and QTextStream:

        // src_path, dest_path, file (is the filename) are QStrings
        QFile fi(src_path + QDir::separator() + file);
        if (!fi.open(QIODevice::ReadOnly)) {
        cerr << "Cannot open file for reading: "
        << qPrintable(file) << " "
        << qPrintable(fi.errorString()) << endl;
        } else {
        QTextStream in(&fi);
        in.setCodec("UTF-8"); // with(out) this line
        QString content = in.readAll();
        // manipulating content
        // writing content to a new file
        QFile fo(dest_path + QDir::separator() + file);
        if (!fo.open(QIODevice::WriteOnly)) {
        cerr << "Cannot open file for writing: "
        << qPrintable(file) << " "
        << qPrintable(fo.errorString()) << endl;
        } else {
        QTextStream out(&fo);
        out.setCodec("UTF-8"); // with(out) this line
        out << qPrintable(content);
        fo.close();
        }
        fi.close();
        }

        1 Reply Last reply
        0
        • O Offline
          O Offline
          obst8
          wrote on last edited by
          #4

          I don't know why all the indentations are gone after posting! I've tried two times, the second time with preview, without luck.

          1 Reply Last reply
          0
          • O Offline
            O Offline
            obst8
            wrote on last edited by
            #5

            @
            // src_path, dest_path, file (is the filename) are QStrings
            QFile fi(src_path + QDir::separator() + file);
            if (!fi.open(QIODevice::ReadOnly)) {
            cerr << “Cannot open file for reading: “ << qPrintable(file)
            << “ “ << qPrintable(fi.errorString()) << endl;
            } else {
            QTextStream in(&fi);
            in.setCodec(“UTF-8”); // with(out) this line
            QString content = in.readAll();
            // manipulating content
            // writing content to a new file
            QFile fo(dest_path + QDir::separator() + file);
            if (!fo.open(QIODevice::WriteOnly)) {
            cerr << “Cannot open file for writing: “ << qPrintable(file)
            << “ “ << qPrintable(fo.errorString()) << endl;
            } else {
            QTextStream out(&fo);
            out.setCodec(“UTF-8”); // with(out) this line
            out << qPrintable(content);
            fo.close();
            }
            fi.close();
            }
            @

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              You need to put your code between coding tags (first icon on the right) so it will show properly

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • O Offline
                O Offline
                obst8
                wrote on last edited by
                #7

                Yes, after a little searching I found that, too. Thank you.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dbzhang800
                  wrote on last edited by
                  #8

                  Hi, you should not encode your QString to local8bit-bytes using qPrintable before you pass them to QTextSteam.

                  --

                  BTW, qDebug()/qWarning() will be easier to use than std::cerr, and qPrintable can be omited if you use them.

                  1 Reply Last reply
                  0
                  • O Offline
                    O Offline
                    obst8
                    wrote on last edited by
                    #9

                    Thank you! I'll try with your suggestion.

                    1 Reply Last reply
                    0
                    • O Offline
                      O Offline
                      obst8
                      wrote on last edited by
                      #10

                      Hi all,
                      The problem is solved. The reason is, as Robot indicated, the use of qPrintable(). I just used it because I find it in a Qt textbook and thought that a QString cannot be passed to a stream directly. Now I think I must learn more about qPrintable(). Where can the documentation for qPrintable() be found in "Qt Creator --> Help"? Thanks!

                      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