Skip to content

Commit f8bdd58

Browse files
committed
add example for call_sync
1 parent c64522d commit f8bdd58

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

examples/modal_example.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import asyncio
2+
import sys
3+
4+
# from PyQt6.QtWidgets import
5+
from PySide6.QtWidgets import QApplication, QProgressBar, QMessageBox
6+
from qasync import QEventLoop, call_sync
7+
8+
9+
async def master():
10+
progress = QProgressBar()
11+
progress.setRange(0, 99)
12+
progress.show()
13+
await first_50(progress)
14+
15+
16+
async def first_50(progress):
17+
for i in range(50):
18+
progress.setValue(i)
19+
await asyncio.sleep(0.1)
20+
21+
# Schedule the last 50% to run asynchronously
22+
asyncio.create_task(
23+
last_50(progress)
24+
)
25+
26+
# create a notification box, use helper to make entering event loop safe.
27+
result = await call_sync(
28+
lambda: QMessageBox.information(
29+
None, "Task Completed", "The first 50% of the task is completed."
30+
)
31+
)
32+
assert result == QMessageBox.StandardButton.Ok
33+
34+
35+
async def last_50(progress):
36+
for i in range(50, 100):
37+
progress.setValue(i)
38+
await asyncio.sleep(0.1)
39+
40+
41+
if __name__ == "__main__":
42+
app = QApplication(sys.argv)
43+
44+
event_loop = QEventLoop(app)
45+
asyncio.set_event_loop(event_loop)
46+
47+
event_loop.run_until_complete(master())
48+
event_loop.close()

0 commit comments

Comments
 (0)