简体   繁体   English

QFileDialog和德语在一条路径中的变元

[英]QFileDialog and german umlaute within a path

i am working on a project, which i am developing with Python and PyQT4. 我正在开发一个项目,我正在使用Python和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). 我偶然发现了一个奇怪的QFileDialog行为,这在我的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. 问题是,当其中一个文件路径包含德语变音符号(ä,ü,ö等)时,在ExistingFiles模式下的QFileDialog无法返回所选文件的列表.QFileDialog不提供选项或参数这种情况很明智。 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. 我正在使用Py2Exe构建一个可执行文件,然后使用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. edit2:我没有确切的代码可以访问,直到星期五,但我们有一个if语句等待对话框完成。 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. 好像介于两者之间的东西不愿意终止,而Python正以某种方式静静地处理异常。 The QFileDialog however is closing as supposed after choosing the files and clicking "Open" or double clicking a file. 然而,在选择文件并单击“打开”或双击文件后,QFileDialog将按照假设关闭。

  1. Try to use lambda x: x.toUtf8(), or toLocal8Bit() or set TextCodec to any codepage you want, it should help. 尝试使用lambda x:x.toUtf8()或toLocal8Bit()或将TextCodec设置为您想要的任何代码页,它应该有所帮助。 These methods return properly encoded python strings. 这些方法返回正确编码的python字符串。 Avoid using str() on QString, it is unaware of charmap you want. 避免在QString上使用str(),它不知道你想要的charmap。

  2. What is getSelectedFiles()? 什么是getSelectedFiles()? There is no such method in Qt 4.5 or higher in QFileDialog class. 在QFileDialog类的Qt 4.5或更高版本中没有这样的方法。 I assumed, that it was typo or some old Qt version, and changed it to selectedFiles() in my test code. 我假设,这是错字或一些旧的Qt版本,并在我的测试代码中将其更改为selectedFiles()。

  3. Why don't you use convenience methods of QFileDialog for file choosing: 为什么不使用QFileDialog的便捷方法来选择文件:

    getExistingDirectory() getOpenFileName() getOpenFileNames() getSaveFileName() getExistingDirectory()getOpenFileName()getOpenFileNames()getSaveFileName()

?

您应该使用unicode()(而不是str())将QString转换为Python unicode字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM