[Development] Should we change the default codec of QTextStream to UTF-8?

1+1=2 dbzhang800 at gmail.com
Thu May 24 21:05:47 CEST 2012


On Thu, May 24, 2012 at 10:26 AM, Thiago Macieira
<thiago.macieira at intel.com> wrote:
> On quinta-feira, 24 de maio de 2012 10.05.50, 1+1=2 wrote:
>> At present, when QT_NO_TEXTCODEC is defined, QTextStream use
>> QString::fromLatin1() /QString::toLocal8Bit()
>> to convert from/to bytes which looks like not very well. So I think it
>> will be better to replace them with QUtf8::convertFromUnicode() and
>> QUtf8::convertToUnicode().
>
> Keep them Latin1. At least the tools will not screw up user data if they can't
> figure out the locale.
>

The problem is that, QTextStream has different behavior depending on
whether  QT_NO_TEXTCODEC is defined or not.

When QT_NO_TEXTCODEC is defined, default codec is
QTextCodec::codecForLocale(), but can be changed using
QTextStream::setCodec().

When QT_NO_TEXTCODEC isn't defined, QString::fromLatin1()
/QString::toLocal8Bit() is used, and they can't canged by users. I
think this is broken for non-latin1-locale users. If they want to
read/write the same file, it indeed will screw up user data ;-)

Even if we want need to keep qmake project file latin1 only, we can
directly using QFile instead of QTextStream, as the features of
QTextStream aren't used by qmake. For example

Line 1825 of qmake/project.cpp:

            QFile qfile(file);
            if(qfile.open(QIODevice::ReadOnly)) {
                QTextStream stream(&qfile);
                while(!stream.atEnd()) {
                    ret += split_value_list(stream.readLine().trimmed());
                    if(!singleLine)
                        ret += "\n";
                }
                qfile.close();
            }



More information about the Development mailing list