##// END OF EJS Templates
Create a QApplication for inputhook if one doesn't already exist...
Thomas Kluyver -
Show More
@@ -1,25 +1,30 b''
1 import sys
1 import sys
2 from IPython.external.qt_for_kernel import QtCore, QtGui
2 from IPython.external.qt_for_kernel import QtCore, QtGui
3
3
4 # If we create a QApplication, keep a reference to it so that it doesn't get
5 # garbage collected.
6 _appref = None
7
4 def inputhook(context):
8 def inputhook(context):
9 global _appref
5 app = QtCore.QCoreApplication.instance()
10 app = QtCore.QCoreApplication.instance()
6 if not app:
11 if not app:
7 return
12 _appref = app = QtGui.QApplication([" "])
8 event_loop = QtCore.QEventLoop(app)
13 event_loop = QtCore.QEventLoop(app)
9
14
10 if sys.platform == 'win32':
15 if sys.platform == 'win32':
11 # The QSocketNotifier method doesn't appear to work on Windows.
16 # The QSocketNotifier method doesn't appear to work on Windows.
12 # Use polling instead.
17 # Use polling instead.
13 timer = QtCore.QTimer()
18 timer = QtCore.QTimer()
14 timer.timeout.connect(event_loop.quit)
19 timer.timeout.connect(event_loop.quit)
15 while not context.input_is_ready():
20 while not context.input_is_ready():
16 timer.start(50) # 50 ms
21 timer.start(50) # 50 ms
17 event_loop.exec_()
22 event_loop.exec_()
18 timer.stop()
23 timer.stop()
19 else:
24 else:
20 # On POSIX platforms, we can use a file descriptor to quit the event
25 # On POSIX platforms, we can use a file descriptor to quit the event
21 # loop when there is input ready to read.
26 # loop when there is input ready to read.
22 notifier = QtCore.QSocketNotifier(context.fileno(), QtCore.QSocketNotifier.Read)
27 notifier = QtCore.QSocketNotifier(context.fileno(), QtCore.QSocketNotifier.Read)
23 notifier.setEnabled(True)
28 notifier.setEnabled(True)
24 notifier.activated.connect(event_loop.exit)
29 notifier.activated.connect(event_loop.exit)
25 event_loop.exec_()
30 event_loop.exec_()
General Comments 0
You need to be logged in to leave comments. Login now