Important: Please read the Qt Code of Conduct -

PySide: QFileDialog.getOpenFileName returns string of tuple instead of just string

  • I just posted this to the Qt General forum on Forum.Nokia, but I think this might be a more appropriate forum for PySide questions...

    I'm trying to convert an application from PyQt4 to Qt and have found what appears to be a difference in the return results from QFileDialog.getOpenFileName

    I'm calling QFileDialog with the following call:

    filepath = unicode(QFileDialog.getOpenFileName(self, "Species Explorer - Open Species", dir, "Species Files (*.species)"))

    When I use PyQt4 I just get a string with the file selected using the dialog box. For example:


    When I use PySide I get a string that contains a tuple with two sub-strings: the filename and the value of the filter. For instance:

    (u'C:/production/species/testSpecies.species', u'Species Files (*.species)')

    Is this the expected behaviour?

    I'm using PySide for Windows, pyside-0.4.0-py2.6

  • <a href="">Documentation</a> shows it should return a string

    File a bug: "":

    [edit: text to link / $chetankjain ]

  • Thanks for the link to the bug reporting site. I've filed a report.

  • They're returning the selected filter as well, which fits the behavior of QFileDialog::getOpenFileName(). You might not be aware of it, but the C++ implementation has a QString pointer argument that you can use to get the selected filter. Python doesn't have such a system (not explicitly at least) and you get a tuple containing the file and the filter. getOpenFileNames would (and should) probably return a tuple of a QStringList and a string.

  • I guess it makes sense having a mechanism to get hold of the selected filter, but it does mean that it has broken API compatibility with PyQt which just returns a string with the filename.

    One idea, I guess, would be to have an additional optional parameter to the Python call, say selectedFilter=True, that if you set then you get the tuple with the selected filter in the result. That would at least keep consistency with PyQt and allow the user to get hold of which filter was used if they want to.

  • Since type checking is a big nono in Python, the API functions preferably return one and only one type of argument. It makes sense then to do what PyQt has already done: a function QFileDialog.getOpenFileNameAndFilter().

    Other than that I wasn't aware of the fact that PySide is trying to stay interface compatible with PyQt. I for one couldn't really care about it as long as it stays consistent with the C++ implementation. I can imagine other people will, though.

  • It remains compatible with PyQt, but with PyQt API2, not API1.

    If you use PyQt with API2 you will get the same behaviour because there's no QString and python string and unicode objects are immutable.

Log in to reply