Repository navigation
inputhook_qt4: Use QEventLoop instead of starting up the QCoreApplication - #2294
Conversation
|
The event loop integration stuff is extremely subtle and brittle. To make
IOW, our model is that if one if this is present, the other has to be. On Sun, Aug 12, 2012 at 2:16 PM, Bradley M. Froehle <
Brian E. Granger |
|
@ellisonbg Yes, it is exceptionally subtle. You'll notice that a The main issue I see here are:
|
|
I have just tested this PR, that is - I tested your bfroehle/inputhook_qt4_alt branch. I am not 100% sure whether that is the right way to test a PR, whether this always gives the right code, and (in this case) why there is an inputhook_qt4_alt and an inputhook_qt4 branch. (I also tried with https://github.com/ipython/ipython/pull/2294.patch but that patch looked too small?) Anyhow, I tested a complex application of mine (which allows to be started from within IPython) with and without %gui qt, and with %pylab, and noticed no problems. I could also verify that it does fix a problem that I have with modal dialogs started from the menu (only) with %gui qt with ipython master. |
|
Thanks a lot @hmeine, that's very useful feedback. I'd also like to hear from @jdmarch, @epatters and other Enthought folks who are heavy-duty Qt users, whether this improves things or causes any trouble. Once we're a bit more confident with in-the-field testing of this, it would be great to have it go in. |
|
After looking at the QCoreApplication source code, I agree with @bfroehle that this change seems unlikely to break anything. Even so, I have not tested it and it makes me a bit nervous. From what I undertand, the main (only?) goal of this pull request is to suppress the |
|
@epatters: You are correct that the goal is to avoid the I'm not really sure what that means... |
|
I'm not sure what that means either. I wouldn't be surprised if its a bug in PyQt or PySide. Given that my approach evidently doesn't work, I'm in favor of using a QEventLoop directly, rather than calling QCoreApplication::exec. Other than setting some state variables that don't seem to be used anywhere, the only thing QCoreApplication adds is the |
|
Oh, but I do I think it is important to add a detailed comment explaining the motivation for this change. This really is an obscure thing... |
|
OK, I finally got around to testing this myself. I am now convinced that this PR won't break anything---but equally convinced that it will not fix issues like #2380. I'm not sure why the reporter of that issue cannot reproduce the problem; for me, modal dialogs are consistently broken under the conditions described in #2080, when running under either master or this PR. Modal dialogs are typically opened by a blocking call to From this it is clear that the logic of the present input hook is wrong. When a blocking call is made from a Qt event handler, IPython must respect this. This means that the terminal prompt will hang until the call completes, but that simply cannot be avoided in the single process model. After several false starts, I hit upon what appears to be a very simple solution to this problem. Rather than starting and stopping an event loop every 50 ms, just call Under normal circumstances, this seems to work as before, but blocking calls started from event handlers are not interrupted. In particular, issue #2380 is resolved. I think this approach is promising, but more testing is required. |
|
@epatters Funny how this all comes full circle. The original implementation of the PyQt4 input hook did nothing more than call |
|
@bfroehle: Well, this is a nightmare. For reference, here is the current PyQt4 input hook implementation, which seems to include all the suggestions in the linked email: More to the point, the linked email indicates that |
|
@epatters wrote:
I was quite surprised, because I tested @bfroehle's inputhook_qt4_alt branch, and it made a difference for me.
I tried it multiple times, and had no problems with bfroehles fix(es). |
|
@hmeine, you're probably right, then, that the behavior is platform specific. Here is an illuminating comment from the So interrupting the top-level event loop is a no-no on OS X. In general, I think we should, if possible, move away from this approach of constantly starting and stopping the event loop. |
|
@epatters It'd be nice if Qt provided some guidance here. Perhaps we should move back to a |
|
@bfroehle, yes, my feeling is that we should move back to I couldn't tell you how the present implementation performs when readline is absent. If it's also pretty broken, we should consider just telling people that readline is a dependency for GUI integration. Don't most users have readline anyway, even on Windows? |
|
Should this PR be closed then? |
|
@hmeine, this PR fixes a problem for you, right? Then my vote would actually be to merge this, since it won't break anything that wasn't already broken. There are deeper problems that remain unsolved, but I can't afford to spend any more time on this right now. |
|
That's right. On Linux, it does improve the situation. |
|
It seems to be a fix on Linux. On OS X, modal dialogs are still broken. But yes, this should definitely be merged. |
|
As this seem to improve most cases I vote for merging this in. |
inputhook_qt4: Use QEventLoop instead of starting up the QCoreApplication
…up the QCoreApplication I referenced this branch in #2080 and was letting it sit for a little while, but I have decided to make it a full pull request to get some additional visibility. Essentially our Qt event loop mechanism repeatedly starts and quits a `QCoreApplication` object. Unfortunately the `QCoreApplication::quit` slot has a lot of unintended side effects (like emitting an `aboutToQuit` signal which closes all open file dialogs). For our input hook, we _might_ be able to get by with just using a `QEventLoop` whose quit slot is much simpler and less destructive. For a little bit of background on why one might want to just use `QEventLoop::exec`, let's examine what `QCoreApplication::exec` does: ```c++ int QCoreApplication::exec() { if (!QCoreApplicationPrivate::checkInstance("exec")) return -1; // ... [some assertions] threadData->quitNow = false; QEventLoop eventLoop; self->d_func()->in_exec = true; self->d_func()->aboutToQuitEmitted = false; int returnCode = eventLoop.exec(); threadData->quitNow = false; if (self) { self->d_func()->in_exec = false; if (!self->d_func()->aboutToQuitEmitted) emit self->aboutToQuit(); self->d_func()->aboutToQuitEmitted = true; sendPostedEvents(0, QEvent::DeferredDelete); } return returnCode; } ``` As far as I can tell, it's a small wrapper around `QEventLoop::exec` which also: * Sets some variables regarding the current status * Emits an `aboutToQuit` signal right before the function returns (which is the root cause of @denisri's problem in #2080). Historically, our Qt event loop is a python implementation of the (win 32) input hook supplied with the PyQt4 source (see qtcore_input_hook` in `python-qt4/sip/QtCore/qcoreapplication.sip`), which more or less dates to a [mailing list post](http://www.riverbankcomputing.com/pipermail/pyqt/2007-July/016512.html) from July 2007.
inputhook_qt4: Use QEventLoop instead of starting up the QCoreApplication
I referenced this branch in #2080 and was letting it sit for a little while, but I have decided to make it a full pull request to get some additional visibility.
Essentially our Qt event loop mechanism repeatedly starts and quits a
QCoreApplicationobject. Unfortunately theQCoreApplication::quitslot has a lot of unintended side effects (like emitting anaboutToQuitsignal which closes all open file dialogs).For our input hook, we might be able to get by with just using a
QEventLoopwhose quit slot is much simpler and less destructive.For a little bit of background on why one might want to just use
QEventLoop::exec, let's examine whatQCoreApplication::execdoes:As far as I can tell, it's a small wrapper around
QEventLoop::execwhich also:aboutToQuitsignal right before the function returns (which is the root cause of @denisri's problem in Event loop issues with IPython 0.12 and PyQt4 (QDialog.exec_ and more) #2080).Historically, our Qt event loop is a python implementation of the (win 32) input hook supplied with the PyQt4 source (see qtcore_input_hook
inpython-qt4/sip/QtCore/qcoreapplication.sip`), which more or less dates to a mailing list post from July 2007.