Getting Started
This section shows the essentials of qtinter and gets you up
and running in five minutes.
Requirements
qtinter supports the following:
Python version: 3.7 and higher
Qt binding: PyQt5, PyQt6, PySide2, PySide6
Operating system: Linux, MacOS, Windows
Installation
qtinter is installed via pip:
$ pip install qtinter
The above command does not install any Qt bindings because it is
assumed that you already have one. If that’s not the case, you may
install a Qt binding together with qtinter using the
following command:
$ pip install qtinter[PyQt6]
Replace PyQt6 with one of PyQt5, PyQt6, PySide2 or
PySide6 of your choice.
Using asyncio from Qt
If your code uses Qt as its entry point (e.g. by calling app.exec())
and you want to use an asyncio-based library, follow these steps.
Step 1 — import qtinter:
import qtinter
Step 2 — enclose the Qt entry point inside
qtinter.using_asyncio_from_qt() context manager:
app = QtWidgets.QApplication([])
with qtinter.using_asyncio_from_qt():
app.exec()
Step 3 — (optionally) connect coroutine functions to Qt signals by
wrapping them with qtinter.asyncslot():
my_signal.connect(qtinter.asyncslot(my_coroutine_function))
And that’s it!
To see these in action, check out the Digital Clock example and the Http Client example. For usage details, see Using using_asyncio_from_qt() and Using asyncslot().
Using Qt from asyncio
If your code uses asyncio as its entry point (e.g. by calling
asyncio.run()) and you want to use a Qt component, follow these steps.
Step 1 — import qtinter:
import qtinter
Step 2 — enclose the asyncio entry point inside
qtinter.using_qt_from_asyncio() context manager:
app = QtWidgets.QApplication([])
with qtinter.using_qt_from_asyncio():
asyncio.run(my_coro())
Step 3 — (optionally) wait for Qt signals by wrapping them with
qtinter.asyncsignal():
await qtinter.asyncsignal(button.clicked)
And that’s it!
To see these in action, check out the Read Out example and the Where am I example. For usage details, see Using using_qt_from_asyncio() and Using asyncsignal().
Using modal dialogs
By default, opening a modal dialog from a coroutine or callback blocks the asyncio event loop until the dialog is closed.
To get the asyncio event loop running without the hazard of potential
re-entrance, wrap the dialog entry-point in modal() and await
on it. For example:
await qtinter.modal(QtWidgets.QMessageBox.warning)(self, "Title", "Message")
What’s great about the above is that the asyncio event loop remains
free of nesting, and the presence of await makes the suspension
point crystal clear.
For further details, see Using modal().
License
BSD License.
Contributing
The source code is hosted on GitHub. Please raise an issue if you have any questions. Pull requests are more than welcome!
Credits
qtinter is derived from qasync but rewritten from scratch. qasync
is derived from asyncqt, which is derived from quamash.