[Solved]QTextStream and UTF-8 Problem



  • 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



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



  • 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();
    }



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



  • @
    // 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();
    }
    @


  • Lifetime Qt Champion

    Hi,

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



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



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



  • Thank you! I'll try with your suggestion.



  • 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!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.