qt.py 1.0 KB

123456789101112131415161718192021222324252627282930
  1. import sys
  2. from IPython.external.qt_for_kernel import QtCore, QtGui
  3. # If we create a QApplication, keep a reference to it so that it doesn't get
  4. # garbage collected.
  5. _appref = None
  6. def inputhook(context):
  7. global _appref
  8. app = QtCore.QCoreApplication.instance()
  9. if not app:
  10. _appref = app = QtGui.QApplication([" "])
  11. event_loop = QtCore.QEventLoop(app)
  12. if sys.platform == 'win32':
  13. # The QSocketNotifier method doesn't appear to work on Windows.
  14. # Use polling instead.
  15. timer = QtCore.QTimer()
  16. timer.timeout.connect(event_loop.quit)
  17. while not context.input_is_ready():
  18. timer.start(50) # 50 ms
  19. event_loop.exec_()
  20. timer.stop()
  21. else:
  22. # On POSIX platforms, we can use a file descriptor to quit the event
  23. # loop when there is input ready to read.
  24. notifier = QtCore.QSocketNotifier(context.fileno(), QtCore.QSocketNotifier.Read)
  25. notifier.setEnabled(True)
  26. notifier.activated.connect(event_loop.exit)
  27. event_loop.exec_()