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. how to put the unicode text into the const char * with QString
QtWS25 Last Chance

how to put the unicode text into the const char * with QString

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 268 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.
  • nicker playerN Offline
    nicker playerN Offline
    nicker player
    wrote on last edited by
    #1

    Here's the problem :
    I used a function to read the file with the params type 'const char *filename ',but you know it is hard to solve the unicode text such as chinese or japanese.
    even though I used the code below,but it always comes the errors.

        QString t_path = QString::fromStdString(path);
        QTextCodec *codec = QTextCodec::codecForLocale();
        QByteArray byteArray = codec->fromUnicode(t_path);
        const char * filename = byteArray.constData();
    

    I also tried the way to transform the ofstream to the FILE stream ,but I couldnt find the way.
    ps:I used the qt 5.14 & mingw 7 & windows os (7)

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Use QString::toLocale8Bit() to convert it to your local encoding.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      4
      • S Offline
        S Offline
        SimonSchroeder
        wrote on last edited by
        #3

        Most likely QString::fromStdString already uses a locale. Is the std::string in UTF-8? In that case I would write:

        QString t_path = QString::fromUtf8(path.c_str());
        

        For the inverse direction I then use t_path.toUtf8().data(). (Carefully when you want to assign this to a const char*. You should then store the result of toUtf8() in a QByteArray variable.)

        I try to have everything set up for UTF-8. On Windows calling setlocale(LC_ALL, ".utf8"); turns on UTF-8 in most places. Even for using fopen() or fstream filenames are then interpreted as UTF-8. However, I also have my Windows 11 machine set up to use the UTF-8 codepage. One can change the codepage programatically as well. For a more involved example see https://github.com/tronkko/dirent (scroll down the page a little). I personally don't use his wmain approach but stick to the good old main instead. To get the proper command line arguments I then call CommanLineToArgvW(GetCommandLineW(), &argc);.

        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