aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorAdrian Herrmann <adrian.herrmann@qt.io>2024-02-16 18:55:06 +0100
committerAdrian Herrmann <adrian.herrmann@qt.io>2024-02-16 19:09:09 +0100
commitbe94c0d2fe10524bd29264b0ce448a8b492eb97e (patch)
treeab437005c86697f29398f1602956c51779243f60 /examples
parent9b240cd08c69b511271c33df07028b41e0a528c9 (diff)
examples: Simplify minimal_asyncio
For historical reasons, the minimal asyncio example was unnecessarily complicated (as it tried to follow the trio example's approach of an AsyncHelper class). Simplify the code to make it more straightforward and understandable. Pick-to: 6.6 Task-number: PYSIDE-769 Change-Id: I376b73356f7e46f640db482a9e149f1a6fa54dd6 Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/async/minimal/minimal_asyncio.py26
1 files changed, 2 insertions, 24 deletions
diff --git a/examples/async/minimal/minimal_asyncio.py b/examples/async/minimal/minimal_asyncio.py
index 4545f35d5..1b72f44c1 100644
--- a/examples/async/minimal/minimal_asyncio.py
+++ b/examples/async/minimal/minimal_asyncio.py
@@ -1,7 +1,7 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-from PySide6.QtCore import (Qt, QObject, Signal, Slot)
+from PySide6.QtCore import Qt
from PySide6.QtWidgets import (QApplication, QLabel, QMainWindow, QPushButton, QVBoxLayout, QWidget)
import PySide6.QtAsyncio as QtAsyncio
@@ -12,8 +12,6 @@ import sys
class MainWindow(QMainWindow):
- start_signal = Signal()
-
def __init__(self):
super().__init__()
@@ -26,37 +24,17 @@ class MainWindow(QMainWindow):
layout.addWidget(self.text, alignment=Qt.AlignmentFlag.AlignCenter)
async_trigger = QPushButton(text="What is the question?")
- async_trigger.clicked.connect(self.async_start)
+ async_trigger.clicked.connect(lambda: asyncio.ensure_future(self.set_text()))
layout.addWidget(async_trigger, alignment=Qt.AlignmentFlag.AlignCenter)
- @Slot()
- def async_start(self):
- self.start_signal.emit()
-
async def set_text(self):
await asyncio.sleep(1)
self.text.setText("What do you get if you multiply six by nine?")
-class AsyncHelper(QObject):
-
- def __init__(self, worker, entry):
- super().__init__()
- self.entry = entry
- self.worker = worker
- if hasattr(self.worker, "start_signal") and isinstance(self.worker.start_signal, Signal):
- self.worker.start_signal.connect(self.on_worker_started)
-
- @Slot()
- def on_worker_started(self):
- asyncio.ensure_future(self.entry())
-
-
if __name__ == "__main__":
app = QApplication(sys.argv)
main_window = MainWindow()
- async_helper = AsyncHelper(main_window, main_window.set_text)
-
main_window.show()
QtAsyncio.run()