views:

157

answers:

2

Hey Everybody,

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.

A: 
  1. 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.

  2. 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.

  3. Why don't you use convenience methods of QFileDialog for file choosing:

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

?

Max
Thats how i got it to work! Cheers!
MB_
A: 

You should use unicode() (not str()) to convert QString into Python unicode strings.

Giovanni Bajo