Linux qfile中文文件名,QFile无法打开包含unicode字符的文件名

博主在使用PySide的QtCore.QImage和QFile处理图像文件时遇到问题,发现当文件名包含Unicode字符时无法正常打开。进一步调查发现,QFile.open()在Unix系统上通过一个函数处理文件名,该函数可能会移除非ASCII字符。此问题影响到文件的读取操作,即使文件实际存在。
摘要由CSDN通过智能技术生成

我和PySide有点问题。我使用QtCore.QImage处理一些图像,注意到路径名中包含unicode字符的图像文件没有被打开。

所以我开始调查,发现QFile也出现了同样的问题。在

我试着给它输入一个由testring编码的'utf8'和一个解码的unicode字符串,两者的区别是一样的。

我也尝试过使用那些QFile.encodeName和QFile.decodeName函数,但所做的只是从文件名中去掉非ascii字符。在

我制作这个脚本是为了演示:#!/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)

输出

^{pr2}$

有人能解释一下吗?在

更新

在看过源代码之后,我发现在unix上,QFile.open()总是通过这个函数传递文件名: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

}

这通常会导致从字符串中删除unicode字符。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值