Create an input hook for running the Qt6 application event loop. Parameters ---------- mgr : an InputHookManager app : Qt Application, optional. Running application to use. If not given, we probe Qt for an existing application object, and create a new one if none i
(mgr, app=None)
| 56 | |
| 57 | |
| 58 | def create_inputhook_qt6(mgr, app=None): |
| 59 | """Create an input hook for running the Qt6 application event loop. |
| 60 | |
| 61 | Parameters |
| 62 | ---------- |
| 63 | mgr : an InputHookManager |
| 64 | |
| 65 | app : Qt Application, optional. |
| 66 | Running application to use. If not given, we probe Qt for an |
| 67 | existing application object, and create a new one if none is found. |
| 68 | |
| 69 | Returns |
| 70 | ------- |
| 71 | A pair consisting of a Qt Application (either the one given or the |
| 72 | one found or created) and a inputhook. |
| 73 | |
| 74 | Notes |
| 75 | ----- |
| 76 | We use a custom input hook instead of PyQt6's default one, as it |
| 77 | interacts better with the readline packages (issue #481). |
| 78 | |
| 79 | The inputhook function works in tandem with a 'pre_prompt_hook' |
| 80 | which automatically restores the hook as an inputhook in case the |
| 81 | latter has been temporarily disabled after having intercepted a |
| 82 | KeyboardInterrupt. |
| 83 | """ |
| 84 | |
| 85 | if app is None: |
| 86 | app = QtCore.QCoreApplication.instance() |
| 87 | if app is None: |
| 88 | from PyQt6 import QtWidgets |
| 89 | |
| 90 | app = QtWidgets.QApplication([" "]) |
| 91 | |
| 92 | # Re-use previously created inputhook if any |
| 93 | ip = InteractiveShell.instance() |
| 94 | if hasattr(ip, "_inputhook_qt6"): |
| 95 | return app, ip._inputhook_qt6 |
| 96 | |
| 97 | # Otherwise create the inputhook_qt6/preprompthook_qt6 pair of |
| 98 | # hooks (they both share the got_kbdint flag) |
| 99 | |
| 100 | def inputhook_qt6(): |
| 101 | """PyOS_InputHook python hook for Qt6. |
| 102 | |
| 103 | Process pending Qt events and if there's no pending keyboard |
| 104 | input, spend a short slice of time (50ms) running the Qt event |
| 105 | loop. |
| 106 | |
| 107 | As a Python ctypes callback can't raise an exception, we catch |
| 108 | the KeyboardInterrupt and temporarily deactivate the hook, |
| 109 | which will let a *second* CTRL+C be processed normally and go |
| 110 | back to a clean prompt line. |
| 111 | """ |
| 112 | try: |
| 113 | allow_CTRL_C() |
| 114 | app = QtCore.QCoreApplication.instance() |
| 115 | if not app: # shouldn't happen, but safer if it happens anyway... |
no test coverage detected