.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/qt_app_asyncio.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_qt_app_asyncio.py: Qt app with asyncio ------------------- An example demonstrating a qt app with a wgpu viz inside. This is the same as the ``qt_app.py`` example, except this uses the asyncio compatible mode that was introduced in Pyside 6.6. For more info see: * https://doc.qt.io/qtforpython-6/PySide6/QtAsyncio/index.html * https://www.qt.io/blog/introducing-qtasyncio-in-technical-preview .. GENERATED FROM PYTHON SOURCE LINES 16-90 .. code-block:: Python # ruff: noqa: N802 import time import asyncio from PySide6 import QtWidgets, QtAsyncio from rendercanvas.qt import QRenderWidget from rendercanvas.utils.cube import setup_drawing_sync def async_connect(signal, async_function): # Unfortunately, the signal.connect() methods don't detect # coroutine functions, so we have to wrap it in a function that creates # a Future for the coroutine (which will then run in the current event-loop). # # The docs on QtAsyncio do something like # # self.button.clicked.connect( # lambda: asyncio.ensure_future(self.whenButtonClicked() # ) # # But that's ugly, so we create a little convenience function def proxy(): return asyncio.ensure_future(async_function()) signal.connect(proxy) class ExampleWidget(QtWidgets.QWidget): def __init__(self): super().__init__() self.resize(640, 480) self.setWindowTitle("Rendering in Qt with asyncio") splitter = QtWidgets.QSplitter() self.button = QtWidgets.QPushButton("Hello world", self) self.canvas = QRenderWidget(splitter, update_mode="continuous") self.output = QtWidgets.QTextEdit(splitter) # self.button.clicked.connect(self.whenButtonClicked) # see above :( async_connect(self.button.clicked, self.whenButtonClicked) splitter.addWidget(self.canvas) splitter.addWidget(self.output) splitter.setSizes([400, 300]) layout = QtWidgets.QHBoxLayout() layout.addWidget(self.button, 0) layout.addWidget(splitter, 1) self.setLayout(layout) self.show() def addLine(self, line): t = self.output.toPlainText() t += "\n" + line self.output.setPlainText(t) async def whenButtonClicked(self): self.addLine("Waiting 1 sec ...") await asyncio.sleep(1) self.addLine(f"Clicked at {time.time():0.1f}") app = QtWidgets.QApplication([]) example = ExampleWidget() draw_frame = setup_drawing_sync(example.canvas) example.canvas.request_draw(draw_frame) # Enter Qt event-loop the asyncio-compatible way QtAsyncio.run() .. _sphx_glr_download_gallery_qt_app_asyncio.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: qt_app_asyncio.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: qt_app_asyncio.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: qt_app_asyncio.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_