title: IPython's Qt event-loop integration doesn't consistently launch a QApplication
body: <p>I'm developing a Qt application with a non-blocking IPython console through which the user can bypass the GUI if needed. I've prepared a simple example called qt_app.py which checks for an already-existing QApplication:</p>
  
  <pre><code>from PyQt5.QtWidgets import QApplication
  app = QApplication.instance()
  print(app)
  </code></pre>
  
  <p>Let's try running the following:</p>
  
  <pre><code>ipython --gui qt5
  %run qt_app.py
  </code></pre>
  
  <p>The output displays a handle to a QApplication:</p>
  
  <pre><code>&lt;PyQt5.QtWidgets.QApplication object at 0x000001323D112678&gt;
  </code></pre>
  
  <p>However, if I try to launch IPython and run the script in one line,</p>
  
  <pre><code>ipython --gui qt5 qt_app.py
  </code></pre>
  
  <p>QApplication.instance() returns None.</p>
  
  <p>As a second example, consider the script qt_app2.py:</p>
  
  <pre><code>from IPython import get_ipython
  ipython = get_ipython()
  ipython.magic('gui qt5')
  from PyQt5.QtWidgets import QApplication
  app = QApplication.instance()
  print(app)
  </code></pre>
  
  <p>If I open an IPython console and run this once, no QApplication is created, but it appears if I run it again:</p>
  
  <pre><code>In [1]: %run qt_app2.py
  None
  
  In [2]: %run qt_app2.py
  &lt;PyQt5.QtWidgets.QApplication object at 0x0000020008606C18&gt;
  </code></pre>
  
  <p>It seems that IPython is creating the QApplication <em>after</em> executing the rest of the code - maybe in the next step of its internal event loop? My application is run from a separate launcher script which uses a call to os.system to run the application in an IPython console, and this inconsistency breaks this functionality. Is there a way to resolve this? </p>
  
  <p>I'm using IPython 7.3.0 and PyQt5 5.11.3.</p>