Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTextCodec unsets codec
Forum Updated to NodeBB v4.3 + New Features

QTextCodec unsets codec

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kxyu
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      Kxyu
      wrote on last edited by
      #2

      Well, I guess no one knows?

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cyberbobs
        wrote on last edited by
        #3

        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 ;)

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved