qt's getOpenFileName hangs on Cancel

1,104 views
Skip to first unread message

Heath Raftery

unread,
Aug 21, 2017, 8:34:30 PM8/21/17
to Project Jupyter
In the recent past I've used PyQt to show an Open Dialog from Jupyter, like so:

from PyQt4 import QtGui
app
= QtGui.QApplication([dir])
fname
= QtGui.QFileDialog.getOpenFileName(None, "Select a file...", '.', filter="All files (*)")

This seems to work okay. On a new installation, I'm trying the equivalent with PyQt5:

from PyQt5.QtWidgets import QApplication, QFileDialog
app
= QApplication.instance()
if not app:
    app
= QApplication([dir])
fname
= QFileDialog.getOpenFileName(None,  "Select a file...", '.', filter="All files (*)")
print(str(fname))

This version also works okay, except if the user clicks the dialog's Cancel button! The print statement executes but the dialog hangs and the process needs to be force quit.

May or may not be relevant, but oddly the fname return value is not the filename as the documentation suggests. Instead is a tuple of the filename and filter string, as if QFileDialog.getOpenFileNameAndFilter had been called instead.

Unfortunately I can't just install PyQt4 due to a conflict (assumedly because I installed the Python3 version of Anaconda:

UnsatisfiableError: The following specifications were found to be in conflict:

  - pyqt 4* -> python 2.6* -> openssl 1.0.1*

  - python 3.6*


Any ideas why the QFileDialog is hanging on cancel? Any suggestions for a workaround?

This is all on macOS, but I'm looking for a cross-platform solution.

Regards,
Heath
Message has been deleted

Heath Raftery

unread,
Aug 21, 2017, 9:09:06 PM8/21/17
to Project Jupyter
Two updates:

  1. Reading the source code I see that sure enough, getOpenFileName returns a tuple of the name and the filter, contrary to the docs.
  2. Inspired by this, I found a crappy workaround:
fname = QFileDialog.getOpenFileName(None, "Select a file...", '.', filter="All files (*),
                                    options=QFileDialog.DontUseNativeDialog)

Adding the DontUseNativeDialog option shows the ugly Qt dialog, but it doesn't hang!

Heath

Jens Nielsen

unread,
Aug 22, 2017, 3:45:43 AM8/22/17
to Project Jupyter
The docs you are looking at are for qt4 not qt5 thats probably why the api is different. This information http://pyqt.sourceforge.net/Docs/PyQt5/pyqt4_differences.html#qfiledialog may be relevant to you, In brief it looks like qt4's getOpenFileNameAndFilter has been renamed getOpenFileName and the qt4 version of getOpenFileName has been removed. I don't think thats directly relevant to your other issue. 

Is this specific to running from Jupyter? I can't reproduce the issue on OSX 10.12 and Python 3.6, neither with Anaconda and pyqt5.6 or the pip installed pyqt5.9 running a script with your code. If I add a sys.exit(app.exec_()) to the bottom to run the event loop such as here http://zetcode.com/gui/pyqt5/firstprograms/ it does indeed hang but that is independent of if you press cancel or not. That is probably because there is no main widget. 

If I add the File dialog to a widget such as in the following, everything works as expected.

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtWidgets import QApplication, QFileDialog

app = QApplication(sys.argv)

w = QWidget()
w.show()
fname = QFileDialog.getOpenFileName(w,  "Select a file...", '.', filter="All files (*)")
print(str(fname))
sys.exit(app.exec_())





--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/ebc23011-0ae6-40c8-8a9a-05ef48b4c44d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages