Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QFile cannot open filenames containing unicode characters

Tags:

python

qt

pyside

I am having a problem with PySide. I was dealing with some images, using QtCore.QImage and noticed that image files which had unicode characters in their path names were not being opened.
So I started investigating and found that QFile presents the same problem.

I've tried feeding it a 'utf8' encoded bytestring, and a decoded unicode string, same difference.
I also tried using those QFile.encodeName and QFile.decodeName functions but all that did was strip 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 looking through the source code, I have found out that QFile.open() always passes the filename through this function on 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 stripped from the string.

like image 971
Steinthor.palsson Avatar asked Mar 24 '26 04:03

Steinthor.palsson


1 Answers

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

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

Unicode filenames seem to be resolved properly after that.

I have to see if this fix affects other platforms negatively though.

like image 86
Steinthor.palsson Avatar answered Mar 25 '26 18:03

Steinthor.palsson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!