路径中的 QFileDialog 和德语变音符号
我正在开发一个项目,我正在使用 Python 和 PyQT4 开发该项目。我偶然发现 QFileDialog 的一个有点奇怪的行为,在我的 IDE (Eclipse) 中运行该项目时并没有发生这种情况。
问题是,当文件路径之一包含德语变音符号(ä、ü、ö 等)时,ExistingFiles 模式下的 QFileDialog 确实无法返回所选文件的列表。 QFileDialog 没有提供选项或参数来使其在这种情况下变得合理。 有谁知道如何解决这个问题?
编辑:我发生错误的部署场景如下所示。我正在使用 Py2Exe 构建一个可执行文件,然后使用 Inno Setup 使其可分发。不知道这是否会引发问题,但我认为信息越多越好。
编辑2: 直到周五我才可以访问确切的代码,但我们有一个 if 语句等待对话框完成。像这样:
fileDialog = QFileDialog(...)
if fileDialog.exec_():
# get the choosen files
fileNames = fileDialog.getSelectedFiles()
# test if if-statement is entered
print fileNames
# convert from QStringList to normal list of Strings
fileNames = list(map(lambda x: str(x), fileNames))
# to suffice as an example print each
for fileName in fileNames:
print fileName
第一个打印命令确实被执行,第二个则没有。就好像两者之间的某些东西不愿意终止并且 Python 正在以某种方式悄悄地处理异常。然而,在选择文件并单击“打开”或双击文件后,QFileDialog 将按预期关闭。
i am working on a project, which i am developing with Python and PyQT4. I have stumbled upon a somewhat odd behaviour of the QFileDialog, that is not occuring when running the project within in my IDE (Eclipse).
The problem is that QFileDialog in ExistingFiles-mode does fail to return the list of selected files, when one of the file paths is containing a german umlaut (ä,ü,ö, etc.)
The QFileDialog is not offering options or parameters to make it sensible regarding this scenario.
Does anyone have any ideas of how to tackle this issue?
edit: my deployment scenario in which the error occurs is looking like the following. i am building an executable with Py2Exe and then make it distributable with Inno Setup. don't know if this may have been giving birth to the problem but the more info the better i think.
edit2:
I don't have the exact code accessable until friday, but we're having an if-statement waiting for the dialog to compplete. like this:
fileDialog = QFileDialog(...)
if fileDialog.exec_():
# get the choosen files
fileNames = fileDialog.getSelectedFiles()
# test if if-statement is entered
print fileNames
# convert from QStringList to normal list of Strings
fileNames = list(map(lambda x: str(x), fileNames))
# to suffice as an example print each
for fileName in fileNames:
print fileName
The first print command does get executed the second doesn't. As if something in between is not willing to terminate and Python is handling the exception somehow quietly. The QFileDialog however is closing as supposed after choosing the files and clicking "Open" or double clicking a file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 lambda x: x.toUtf8() 或 toLocal8Bit() 或将 TextCodec 设置为您想要的任何代码页,它应该会有所帮助。这些方法返回正确编码的 python 字符串。避免在 QString 上使用 str(),它不知道您想要的字符映射。
什么是 getSelectedFiles()? Qt 4.5或更高版本的QFileDialog类中没有这样的方法。我认为这是拼写错误或某些旧的 Qt 版本,并在我的测试代码中将其更改为 selectedFiles()。
为什么不使用 QFileDialog 的便捷方法来选择文件:
获取现有目录()
获取打开文件名()
获取打开文件名()
getSaveFileName()
?
Try to use lambda x: x.toUtf8(), or toLocal8Bit() or set TextCodec to any codepage you want, it should help. These methods return properly encoded python strings. Avoid using str() on QString, it is unaware of charmap you want.
What is getSelectedFiles()? There is no such method in Qt 4.5 or higher in QFileDialog class. I assumed, that it was typo or some old Qt version, and changed it to selectedFiles() in my test code.
Why don't you use convenience methods of QFileDialog for file choosing:
getExistingDirectory()
getOpenFileName()
getOpenFileNames()
getSaveFileName()
?
您应该使用 unicode() (而不是 str())将 QString 转换为 Python unicode 字符串。
You should use unicode() (not str()) to convert QString into Python unicode strings.