QFile cannot open filenames containing Unicode characters

I have a problem with PySide. I have dealt with some images using QtCore.QImage

, and noticed that image files where Unicode characters in path names were not open.
So I started researching and found that QFile

presents the same problem.

I've tried feeding it "utf8" encoded byte and also decoded unicode string, same difference.
I also tried using those functions QFile.encodeName

and QFile.decodeName

, but all it did was remove the non-ascii characters from the filename.

I made this script to demonstrate:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os

from PySide.QtCore import QFile, QIODevice

try:
    os.makedirs(u'/tmp/qttest')
except:
    pass #probably dir just exists
os.chdir(u'/tmp/qttest')

def make_file(fn):
    f = open(fn, 'w')
    f.close()

def check_file(fn):
    f = QFile(fn)
    f.open(QIODevice.ReadOnly)
    return f.isReadable()    

fna = u'somefile.txt'
fnu = u'einhverskrá.txt'

make_file(fna)
make_file(fnu)

print fna+u' was opened successfully: ', check_file(fna)
print fnu+u' was opened successfully: ', check_file(fnu)

print fna+u' exists: ', os.path.exists(fna)
print fnu+u' exists: ', os.path.exists(fnu)

      

Output

somefile.txt was opened successfully:  True
einhverskrá.txt was opened successfully:  False
somefile.txt exists:  True
einhverskrá.txt exists:  True

      

Can someone explain this?

UPDATE After examining the source code, I found that I QFile.open()

always pass the filename via this function in unix:

static QString locale_decode(const QByteArray &f)
{
#if defined(Q_OS_DARWIN)
    // Mac always gives us UTF-8 and decomposed, we want that composed...
    return QString::fromUtf8(f).normalized(QString::NormalizationForm_C);
#elif defined(Q_OS_SYMBIAN)
    return QString::fromUtf8(f);
#else
    return QString::fromLocal8Bit(f);
#endif
}

      

This always results in unicode characters being removed from the string.

+3


source to share


1 answer


Yes, I believe I have found a solution to my problem.

from PySide.QtCore import QTextCodec
QTextCodec.setCodecForLocale(QTextCodec.codecForName('UTF-8'))

      



After that, the Unicode filenames seem to be eliminated.

I have to check if this fix does not affect other platforms.

+2


source







All Articles