QTextCodec unsets codec
-
Hi everybody. I've got an odd issue with using QTextCodec to output some cyrillic strings. I use a code like this in my main() function:
@
QTextCodec *Codec_win = QTextCodec::codecForName("CP1251");
QTextCodec::setCodecForCStrings(Codec_win);
QTextCodec::setCodecForTr(Codec_win);
@
And it worked pretty good before, but in the current project I mentioned, that it only affects strings set in main() function, but not in a constructor in my main window. When i call, for example QTextCodec::codecForCStrings() it returns 0, though I've set it explicitly in main(). When I set it again right before using a cyrillic string it works ok.Do you have any ideas how to solve this?
-
Well, I guess no one knows?
-
First of all, it's not a really good idea to use CP1251 to insert some cyrillic strings to your code. It's much better to use UTF8 everywhere (esp. if you want your application to be able to compile and work on the other platforms :)
I guess you are compiling your application for Windows? Is there any other places in your code where the codecs may be changed? Maybe you need to try to build debug version of Qt from sources, toggle a breakpoint somewhere inside the QTextCodec::setCodecForCStrings and QTextCodec::setCodecForTr functions and take a look if there is any other code in your project toggling this functions.
Here is a fragment of code used by our current project and it works like a charm:
@void setupLocale()
{
// Установка локали по умолчанию
QLocale russianLocale(QLocale::Russian, QLocale::RussianFederation);
QLocale::setDefault(russianLocale);// Установка текстовых кодеков
QTextCodec* defaultCodec = QTextCodec::codecForName("UTF-8");
QTextCodec::setCodecForTr(defaultCodec);
QTextCodec::setCodecForLocale(defaultCodec);
QTextCodec::setCodecForCStrings(defaultCodec);// Устанока QTranslator для Qt на текущую локаль
QTranslator* qtTranslator = new QTranslator(QCoreApplication::instance());
qtTranslator->load("qt_ru_RU", QLibraryInfo::location(QLibraryInfo::TranslationsPath));
QCoreApplication::instance()->installTranslator(qtTranslator);
}
@
(I'm sorry for the comments written in russian, but your profile shows me that it will be no problem for you to understand it ;)