10

I've recently updated to the new version of Qt5 for Python. In doing so, I've been having to alter my code in accordance to some of the notable changes that have occurred. I wanted to get some insight into this line of code that I've created. It feels like a dirty way of solving the problem of getting a string instead of a tuple from the function. (Note the [0] at the end of the line)

filename = QtWidgets.QFileDialog.getOpenFileName(None, "Open " + key + " Data File", '.', "(*.csv)")[0]

I want filename = {str}'C:/.././.../format.csv'

not filename = <class 'tuple'>: ('C:/.././.../format.csv', '(*.csv)')

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
tisaconundrum
  • 2,156
  • 2
  • 22
  • 37
  • 1
    I don't understand why this is a difference between Qt4 and Qt5. I use Qt4 and the function `QFileDialog.getOpenFileName` has always returned a tuple, not a string. You can't control what the function returns. The filename is the first element of the tuple and you have to access it somehow. What's dirty about that? – Paul Cornelius Apr 20 '17 at 02:46
  • Wanting to know if placing a [0] at the end is programmaticly correct, or if there is in fact a better solution. Like a parameter I'm supposed to place inside the function. – tisaconundrum Apr 20 '17 at 03:03
  • @PaulCornelius It could be a API1 vs. API2 change as discussed [here](https://forum.qt.io/topic/757/pyside-qfiledialog-getopenfilename-returns-string-of-tuple-instead-of-just-string) and [here](https://srinikom.github.io/pyside-bz-archive/343.html). – Jérôme Sep 04 '17 at 22:25
  • 1
    For the record I was hit by this after porting PyQt4 code to PyQt5 using [pyqt4topyqt5](https://github.com/rferrazz/pyqt4topyqt5). I just opened [issue #17](https://github.com/rferrazz/pyqt4topyqt5/issues/17). – Jérôme Sep 04 '17 at 22:45

1 Answers1

16

After looking through a repository that was being updated to Qt5 as well; I found they used a line like this.

filename, _filter = QtWidgets.QFileDialog.getOpenFileName(None, "Open " + key + " Data File", '.', "(*.csv)")

filename is a string and the (*.csv) gets discarded in the _filter variable

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
tisaconundrum
  • 2,156
  • 2
  • 22
  • 37