- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个项目,我正在使用 Python 和 PyQT4 进行开发。我偶然发现了 QFileDialog 的一个有点奇怪的行为,在我的 IDE (Eclipse) 中运行项目时不会发生这种行为。
问题是,当其中一个文件路径包含德语变音符号(ä、ü、ö 等)时,现有文件模式下的 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 在选择文件并单击“打开”或双击文件后按预期关闭。
最佳答案
尝试使用 lambda x: x.toUtf8() 或 toLocal8Bit() 或将 TextCodec 设置为您想要的任何代码页,这应该会有所帮助。这些方法返回正确编码的 python 字符串。避免在 QString 上使用 str(),它不知道您想要的字符映射。
什么是 getSelectedFiles()? QFileDialog 类中的 Qt 4.5 或更高版本中没有此类方法。我假设这是拼写错误或一些旧的 Qt 版本,并在我的测试代码中将其更改为 selectedFiles()。
为什么不使用 QFileDialog 的便捷方法来选择文件:
获取现有目录()getOpenFileName()getOpenFileNames()getSaveFileName()
?
关于python - QFileDialog 和路径中的德语元音变音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2585694/
我是一名优秀的程序员,十分优秀!