[PyQt] A suggestion for solving a bug in PyQt's input hook

Noam Raphael noamraph at gmail.com
Wed May 7 11:03:21 BST 2008


Hello,

I am writing a sort of Qt program which will is run from the Python
interactive shell. I have encountered a problem with how the input
hook is made: If the program uses a function which displays a modal
dialog, the dialog is immediately closed.

You can test it by running these commands in an interactive shell:

>>> from PyQt4 import QtCore, QtGui
>>> app = QtGui.QApplication([])
>>> c = QtGui.QPushButton(None)
>>> def clicked():
...     print repr(str(QtGui.QFileDialog.getOpenFileName(None)))
...
>>> QtCore.QObject.connect(c, QtCore.SIGNAL('clicked(bool)'), clicked)
True
>>> c.show()

Then, when you click the button, you get an open dialog which blinks
and disappears immediately. The getOpenFileName() function will return
an empty string.

The reason is that the input hook sets up a timer which calls
QCoreApplication.quit() after a short time. quit() closes all modal
windows.

I suggest that the input hook will call quit() only after all modal
dialogs were closed. This will stop processing Python commands while a
modal dialog is displayed, but it's much better than closing a modal
dialog immediately without getting a response from the user. Also,
most chances are that no one will notice that, since they will be busy
answering the dialog...

I implemented this method in my version of IDLE (which mostly I use)
called idlespoon, and it seems to work nicely. You can see what I did
in http://idlespoon.python-hosting.com/file/trunk/run.py (look for the
handle_gui_events() function).

Thanks,
Noam


More information about the PyQt mailing list