-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example that uses PySide6 with asyncio compat (#612)
* Add Qt+asyncio example * --amend * add note for later * tweak
- Loading branch information
1 parent
e99702f
commit 1158594
Showing
4 changed files
with
111 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
""" | ||
An example demonstrating a qt app with a wgpu viz inside. | ||
This is the same as the ``gui_qt_embed.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 | ||
""" | ||
|
||
# ruff: noqa: N802 | ||
# run_example = false | ||
|
||
import time | ||
import asyncio | ||
|
||
from PySide6 import QtWidgets, QtAsyncio | ||
from wgpu.gui.qt import WgpuWidget | ||
from triangle 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("wgpu triangle embedded in a qt app") | ||
|
||
splitter = QtWidgets.QSplitter() | ||
|
||
self.button = QtWidgets.QPushButton("Hello world", self) | ||
self.canvas = WgpuWidget(splitter) | ||
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters