[Development] Why we *have to* remove codecFor... ?

1+1=2 dbzhang800 at gmail.com
Mon Apr 23 00:43:48 CEST 2012


IMO,  codecForXXX() is really a good news for China users. In the past
three years, I noticed that too many newcomers using these functions
in a wrong way.

such as

{{{
QTextCodec *codec = QTextCodec::codecForName("System");
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
}}}

or

{{{
QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
}}}

or

{{{
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));
}}}

or

...

The reason why they use this is quite simply: as they want to put
Chinese characters in to narrow string " const char* " , but not
familiar with

 *  the set in which source files are written (the source character set),
 *  and the set interpreted in the execution environment (the execution
character set).

So they google/google/google, ask on local Qt forums, talk with other
newcomer, then they satisfied with such a "workable" solution.

Most of them even don't know their source code can only be compiled by
MSVC under Simplified-Chinese-Windows


In addition, at present, the fact is,  once Chinese characters are
directly used in C++ application, your source code will refuse to work
in a cross-platform way, and  codecForXXX()  can not give any help for
this.

Debao



2012/4/22 Loaden <loaden at gmail.com>:
> Hi, All! I am only a physics teacher from China.  It makes me confused  that
> I just noticed the codecFor... function is removed for now.
> See:
> https://codereview.qt-project.org/#change,14626
> https://codereview.qt-project.org/#change,14999
>
> But please let's me know how to make this work with *MSVC2010*?
>
>> #include <QApplication>
>> #include <QLabel>
>> #include <QTextCodec>
>> #include <QTextStream>
>> #include <QDebug>
>> int main(int argc, char *argv[])
>> {
>> #ifdef Q_OS_WIN
>>     QTextCodec *codec = QTextCodec::codecForName("GBK");
>>     QTextCodec::setCodecForLocale(codec);
>> #if QT_VERSION < 0x050000
>>     QTextCodec::setCodecForCStrings(codec);
>>     QTextCodec::setCodecForTr(codec);
>> #endif
>> #endif
>>     QApplication app(argc, argv);
>>     QString str = QString::fromAscii("Hello世界World!你好!");
>>     QTextStream out(stdout);
>>     qDebug() << "Hello世界World!你好!";
>>     out << str << endl;
>>     QLabel *label = new QLabel(str + QObject::tr("Hello世界World!你好!"));
>>     label->show();
>>     return app.exec();
>> }
>
> In Qt4, It's works fine!!
> That so sad, It's not work in Qt5!
>
> In currently, I does't test it on Linus yet, But It's should works well
> whether use Qt4 or Qt5.
> But don't abandoned Windows, and *MSVC* compiler.
>
> --
> Best Regards
> Loaden
>
>
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development
>



More information about the Development mailing list