[Qt-interest] Non-latin characters in QString and char * on Windows

Pavel Koshevoy pavel at aragog.com
Thu Mar 3 17:39:37 CET 2011


On 3/3/11 1:48 AM, Konstantin Tokarev wrote:
> Thanks for all replies!
>
> I've modified my code but it still works improperly on Windows:
>
>
> int main(int argc, char *argv[])
> {
>      QApplication a(argc, argv);
>      QString fileName = QFileDialog::getOpenFileName();
>      QByteArray fileNameArr = QFile::encodeName(fileName);
>      const char * fileNameStr = fileNameArr.constData();
>
>      if(!QFile::exists(fileName))
>          return 0;
>
>      cout<<  fileNameArr.data()<<  '\n'
>              <<  fileNameStr<<  endl;
>
>      ifstream ifs;
>      ifs.open(fileNameStr);
>      if (!ifs)
>        return 0;
>      cout<<  "OK"<<  endl;
>
>      return 0;
> }
>
>
> On Windows I get two (identical) lines with broken Cyrillic characters and no "OK"
> On Linux I get two (identical) correct lines and "OK".
>
> Any ideas?
>
>

I would recommend using the wchar_t filename to open std::ifstream on 
Windows:
http://msdn.microsoft.com/en-us/library/70bb7saf%28v=VS.90%29.aspx

This avoids all the guesswork about what non-unicode encoding should be 
used for the filename.

     Pavel.




More information about the Qt-interest-old mailing list