[Qt-interest] Non-Ascii filenames with QFile
Santosh Puranik
santosh.puranik at nokia.com
Wed Feb 10 17:35:36 CET 2010
Hello,
Thanks for your mail.
ext Thiago Macieira wrote, On Wednesday 10 February 2010 05:23 PM, UTC
+0530:
> When trying to access the file, Qt converts from UTF-16 to the locale's
> encoding, which from what you described is UTF-8. And that failed.
>
> The reason why it failed is because your source code is ALSO in UTF-8. And you
> told Qt to decode your UTF-8 literal as Latin 1. That's your error.
>
> Try instead:
> QFile f(QString::fromUtf8("/home/user/áèîõü.mp3"));
>
I do not think that is the problem, even if I use fromUtf8() as above, I
still have the same issue. I'll explain my actual use case in more detail:
1) We get the filename from a PC over USB.
2) The strings that we receive are encoded in UTF-16 LE. So, we make a
path out of this: QString filePath = QString(<Root path>) +
QString::fromUtf16(<raw UTF-16 LE data>).
3) Then create that file with QFile: QFile f(filePath);
f.open(QIODevice::ReadWrite).
4) And this still fails.
The problem IMO is in when QT tries to encode the file path to the
system locale's charset ... which is not UTF-8 on my system:
~ $ locale charmap
ANSI_X3.4-1968
This basically causes QString::toLocal8Bit() to lose data.
Which was my question, and I guess you answered that too:
> In Qt, filenames are represented by Unicode strings, in UTF-16. We cannot give
> you a non-encoded filename access, because there are far too many places in Qt
> that take filenames. They all take QString. And on Windows, filenames are
> encoded in UTF-16.
My reason for asking (also) a QFile constructor where we can provide the
path encoding along with the path name was so that this would still work
if the locale settings on my system were missing/incorrect... but
probably that is out of QT's scope.
Best Regards,
Santosh
More information about the Qt-interest-old
mailing list