Make Qt4 event loop integration successfully handle modal dialogs.
Based on the solution for IPython.