[Qt-interest] Non-Ascii filenames with QFile

Thiago Macieira thiago at kde.org
Wed Feb 10 16:25:07 CET 2010


Em Quarta-feira 10. Fevereiro 2010, às 15.57.05, Santosh Puranik escreveu:
> Why should QFile have to encode the file path to local8Bit() encoding? I 
> guess it is to achieve platform compatibility. So could there be an API 
> in QFile where I can specify the encoding of the file path, so that it 
> does not try to encode it again?

No.

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.

Anyway, that's not your problem. Your problem is that your source code .cpp 
file is not properly encoded.

When you wrote:
	QFile f("/home/user/áèîõü.mp3");

you actually wrote:
	QFile f(QString::fromAscii("/home/user/áèîõü.mp3"));

And despite the name, fromAscii means actually "pass through the codec for C 
strings", which defaults to Latin1. So you actually told Qt to decode your 
literal as Latin1.

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"));

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100210/3b0d3764/attachment.bin 


More information about the Qt-interest-old mailing list