Skip to content

Where to call async method before handing control over to Qt event loop? #109

Open
@manuel-wagesreither

Description

@manuel-wagesreither

Hello,

I hope the issues facility can also be used to ask questions.

The Readme currently lists the following qasync usage example:

import sys
import asyncio

from qasync import QEventLoop, QApplication
from PySide6.QtWidgets import QWidget, QVBoxLayout

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.setLayout(QVBoxLayout())
        self.lbl_status = QLabel("Idle", self)
        self.layout().addWidget(self.lbl_status)

    @asyncClose
    async def closeEvent(self, event):
        pass

    @asyncSlot()
    async def onMyEvent(self):
        pass


if __name__ == "__main__":
    app = QApplication(sys.argv)

    event_loop = QEventLoop(app)
    asyncio.set_event_loop(event_loop)

    app_close_event = asyncio.Event()
    app.aboutToQuit.connect(app_close_event.set)

    main_window = MainWindow()
    main_window.show()

    with event_loop:
        event_loop.run_until_complete(app_close_event.wait())

If this examples MainWindow class would have an additonal async def do_something() function (decorated with @asyncSlot) , what would be the proper place to call it?

This wouldn't work obviously:

    [...]
    main_window = MainWindow()
    main_window.show()

    main_window.do_something() # can't call here, as we're not inside an async function.

    with event_loop:
        event_loop.run_until_complete(app_close_event.wait())

I'm a beginner to Python as well as Asyncio and Qt, so I'm a bit helpless here. I don't know where event_loop.run_until_complete() (or event_loop.run_forever() fwiw) start executing. Hence I'm unable to inject my code to run prior.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions