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. Charset problems with Qt 5.* on Windows
QtWS25 Last Chance

Charset problems with Qt 5.* on Windows

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.1k Views
  • 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.
  • N Offline
    N Offline
    naviset
    wrote on last edited by
    #1

    Need advice now to use Qt 5.* in Visual Studio without setCodecForTr ().

    Since Qt 5.0, all string operation must be in utf-8 charset:
    "Source code must be UTF-8 and QString wants it":http://www.macieira.org/blog/2012/05/source-code-must-be-utf-8-and-qstring-wants-it/

    Visual Studio IDE fully supporting utf-8 source code, all ok, but only until you want to compile them.
    When compilation started, compiler convert all strings to current locale codepage:
    char *pText="Some text in any language except English";
    When you run your programm, even if source code was in utf-8 charset, pointer pText will be in current codepage of your system, in case of Russia, it is cp1251.

    If you want to write code like this in your utf-8 source code file:
    QString myText("Some text in Russian language encoded utf-8");
    It will not work propertly, because QString want utf-8, but pointer will be to text in cp1251.

    Visual Studio compiler don't support utf-8 locale even in Visial Studio 2013:
    "setlocale in Visual Studio":http://msdn.microsoft.com/en-US/library/x99tb11d.aspx

    • If you provide a code page value of UTF-7 or UTF-8, setlocale will fail, returning NULL.*

    How to workaround this?

    1 Reply Last reply
    0
    • _ Offline
      _ Offline
      _Andrey_
      wrote on last edited by
      #2

      When I used Qt 4.8, I wrote in main.cpp:

      QTextCodec::setCodecForTr( QTextCodec::codecForName( "Windows-1251" ) );

      and then every string I wrapped in tr() function. And that worked fine.

      Now, when I proceed to Qt 5.3 I create a global variable in my project:

      QTextCodec *__codec = QTextCodec::codecForName( "Windows-1251" );

      Replace every "tr(" or "QObject::tr(" with "__codec->toUnicode(" in the project and make __codec variable avaliable in each file where it is used by

      extern QTextCodec *__codec;

      This is not a nice way to workaround, but it works well.

      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