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

Loaden loaden at gmail.com
Sun Apr 22 11:36:46 CEST 2012


The question is: I don't want use utf-8 code in source files, like this:

> QString <http://qt-project.org/doc/QString.html> str = QString<http://qt-project.org/doc/QString.html>
> ::fromUtf8("\xc3\xb7");


I just want directly use the "÷" in source file. And I don't need know what
is the utf-8 code of "÷". Like this:

> QString <http://qt-project.org/doc/QString.html> str = QString<http://qt-project.org/doc/QString.html>
> ::fromUtf8("÷");


 This code works fine with Qt4, and the source files use 'UTF-8 with bom'.
(B.T.W ASCII/UNICODE format of the source file works well too).

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("÷");
>     QTextStream out(stdout);
>     qDebug() << "÷";
>     out << str << endl;
>     QLabel *label = new QLabel(str + QObject::tr("÷"));
>     label->show();
>     return app.exec();
> }


 But it's does't work with Qt5.
Does you mean we have to use the *real / base* utf-8 code like below?

> int main(int argc, char *argv[])
> {
>     QApplication app(argc, argv);
>     QString str = QString::fromUtf8("\xc3\xb7");
>     QTextStream out(stdout);
>     qDebug() << QString::fromUtf8("\xc3\xb7");
>     out << str << endl;
>     QLabel *label = new QLabel(str + QObject::trUtf8("\xc3\xb7"));
>     label->show();
>     return app.exec();
> }


Oh, my God! It's no readability!

2012/4/22 Giuseppe D'Angelo <dangelog at gmail.com>

> 2012/4/22 Loaden <loaden at gmail.com>:
> > Thanks for reply! I don't care the reason now, I just want know how to
> make
> > it work.
> > I am try UTF-8 of the source file, with or not with UTF-8 BOM, and try
> > fromLatin1/Utf8/Ascii too.
> > And all tried is failed!
> >
> > In MSVC2010, If use some Chinese (CJK) strings, We have to save the
> source
> > file as UTF-8 with BOM format.
> > I am also try UNICODE (with or without BOM) too.
> > It's does't work too.
>
> For the same reasons, you shouldn't be using tr() but trUtf8().
> See also the random suggestions I put together when helping people on
> devnet:
> http://qt-project.org/wiki/Strings_and_encodings_in_Qt
> (unfinished, help is welcome, also not up-to-date with Qt 5 but if you
> stick to the short recipe everything will just work).
>
> --
> Giuseppe D'Angelo
>



-- 
Best Regards
Yuchen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20120422/7cd7102c/attachment.html>


More information about the Development mailing list