how to put the unicode text into the const char * with QString
-
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) -
Use QString::toLocale8Bit() to convert it to your local encoding.
-
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 aconst char*
. You should then store the result oftoUtf8()
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 usingfopen()
orfstream
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 hiswmain
approach but stick to the good oldmain
instead. To get the proper command line arguments I then callCommanLineToArgvW(GetCommandLineW(), &argc);
.