What is the right way to determine if a %gui qt event loop is running?

I need to determine if something is running in one of the following scenarios:

  • in jupyter with no running Qt event loop, i.e. the user has NOT called %gui qt
  • in jupyter with a running Qt event loop, the user has called %gui qt
  • in a regular Qt event loop outside of jupyter

I could not find the answer here: Built-in magic commands — IPython 8.16.0 documentation

I found get_ipython().kernel.app but I’m wondering if this is the recommended way to do it.


    ip = get_ipython()
    if ip.has_trait("kernel"):
        # we are in jupyter
        if hasattr(ip.kernel, "app"):
            if ip.kernel.app.__class__.__name__ == "QApplication":
                # Qt event loop is running in jupyter, i.e. user has called %gui qt
            # Qt event loop not running in jupyter, user has NOT called %guit
except NameError:
    # we are not in jupyter or ipython