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

Giuseppe D'Angelo dangelog at gmail.com
Sun Apr 22 11:55:23 CEST 2012


On 22 April 2012 10:36, Loaden <loaden at gmail.com> wrote:
> The question is: I don't want use utf-8 code in source files, like this:
>>
>> QString str = QString::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 str = QString::fromUtf8("÷");

You can do that -- as I said in my link, that requires that the
compiler emits UTF-8 as the execution charset for that literal. gcc
simply leaves the source bytes untrasformed if you don't pass the
various -fexec-charset options, so all it's required is that your
source code is encoded using UTF8-8. (I don't have any clue about
MSVC).

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

Again: stop using fromAscii, tr, etc.; use the equivalents that take
UTF-8 (fromUtf8, trUtf8, etc.).

-- 
Giuseppe D'Angelo



More information about the Development mailing list