Skip to content

Commit 1ec3d80

Browse files
committed
Fix README.md basic example to work on python<=3.9
1 parent e5f4214 commit 1ec3d80

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

README.md

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,58 @@ If you need some CPU-intensive tasks to be executed in parallel, `qasync` also g
2121
import asyncio
2222
import sys
2323

24-
from PySide6.QtWidgets import QVBoxLayout, QWidget
24+
from PySide6.QtGui import QCloseEvent
25+
from PySide6.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget
2526

26-
from qasync import QApplication, QEventLoop
27+
import qasync
28+
from qasync import QEventLoop, asyncClose, asyncSlot
2729

2830

2931
class MainWindow(QWidget):
3032
def __init__(self):
3133
super().__init__()
3234

33-
self.setLayout(QVBoxLayout())
34-
self.lbl_status = QLabel("Idle", self)
35-
self.layout().addWidget(self.lbl_status)
36-
37-
@asyncClose
38-
async def closeEvent(self, event):
39-
pass
35+
layout = QVBoxLayout()
36+
self.button = QPushButton("Load", self)
37+
self.button.clicked.connect(self.onButtonClicked)
38+
layout.addWidget(self.button)
39+
self.setLayout(layout)
4040

4141
@asyncSlot()
42-
async def onMyEvent(self):
43-
pass
42+
async def onButtonClicked(self):
43+
"""
44+
Use async code in a slot by decorating it with @asyncSlot.
45+
"""
46+
self.button.setText("Loading...")
47+
await asyncio.sleep(1)
48+
self.button.setText("Load")
4449

50+
@asyncClose
51+
async def closeEvent(self, event: QCloseEvent):
52+
"""
53+
Use async code in a closeEvent by decorating it with @asyncClose.
54+
"""
55+
self.button.setText("Closing...")
56+
await asyncio.sleep(1)
4557

46-
if __name__ == "__main__":
47-
app = QApplication(sys.argv)
4858

59+
async def main(app):
4960
app_close_event = asyncio.Event()
5061
app.aboutToQuit.connect(app_close_event.set)
51-
5262
main_window = MainWindow()
5363
main_window.show()
64+
await app_close_event.wait()
5465

55-
# for 3.11 or older use qasync.run instead of asyncio.run
56-
# qasync.run(app_close_event.wait())
57-
asyncio.run(app_close_event.wait(), loop_factory=QEventLoop)
66+
67+
if __name__ == "__main__":
68+
app = QApplication(sys.argv)
69+
# for python 3.11 or newer
70+
asyncio.run(main(app), loop_factory=QEventLoop)
71+
# for python 3.10 or older
72+
# qasync.run(main(app))
5873
```
5974

60-
More detailed examples can be found [here](https://github.com/CabbageDevelopment/qasync/tree/master/examples).
75+
More detailed examples can be found in the [examples](./examples/) directory.
6176

6277
### The Future of `qasync`
6378

@@ -76,9 +91,15 @@ If you need Python 3.6 or 3.7 support, use the [v0.25.0](https://github.com/Cabb
7691

7792
## Installation
7893

79-
To install `qasync`, use `pip`:
94+
To install using `uv`:
8095

96+
```bash
97+
uv add qasync
8198
```
99+
100+
To install using `pip`:
101+
102+
```bash
82103
pip install qasync
83104
```
84105

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ classifiers = [
2828
"Operating System :: Microsoft :: Windows",
2929
"Operating System :: POSIX",
3030
"Operating System :: POSIX :: Linux",
31+
"Programming Language :: Python :: 3",
32+
"Programming Language :: Python :: 3.8",
33+
"Programming Language :: Python :: 3.9",
34+
"Programming Language :: Python :: 3.10",
35+
"Programming Language :: Python :: 3.11",
36+
"Programming Language :: Python :: 3.12",
37+
"Programming Language :: Python :: 3.13",
3138
"Topic :: Software Development :: Libraries :: Python Modules",
3239
]
3340
version = "0.28.0"

0 commit comments

Comments
 (0)