Skip to content

Async support for qtbot #250

@vxgmichel

Description

@vxgmichel

Hello,

Is there any plan to support some kind of wrapper on top of qtbot in order to turn the wait* methods into async methods that could be called from a qtrio test? That would be very useful in my opinion, e.g:

@pytest.mark.trio(run=qtrio.run)
async def test(aqtbot):
    w = mywidget()
    aqtbot.addWidget(w)
    async with aqtbot.waitSignal(w.mysignal):
        aqtbot.mouseClick(w, QtCore.Qt.LeftButton)

The problem with using the blocking methods directly is that all trio code is blocked while waiting for the corresponding signal. I experimented with it using the following test:

import pytest
import trio
import qtrio

from PyQt5.QtCore import QTimer


@pytest.mark.trio(run=qtrio.run)
async def test(qtbot):

    async with trio.open_nursery() as nursery:

        timer1 = QTimer()
        timer1.setSingleShot(True)
        timer1.setInterval(200)

        timer2 = QTimer()
        timer2.setSingleShot(True)
        timer2.setInterval(200)

        async def aslot():
            await trio.sleep(.2)
            timer2.start()

        timer1.timeout.connect(
            lambda: nursery.start_soon(aslot)
        )
        with qtbot.wait_signal(timer2.timeout):
            timer1.start()
            # await trio.sleep(1)  # Uncomment to fix

The blocking methods are listed below:

  • wait
  • waitActive
  • waitCallback
  • waitExposed
  • waitForWindowShown
  • waitSignal
  • waitSignals
  • waitUntil

My guess is that there are two ways to approach this:

  • Either wrap blocking qtbot calls with some qtrio API, e.g:
    await qtrio.run_blocking_call(qtbot.wait, 1000)
  • Or re-implement the methods using the qtrio emission API (e.g enter_emissions_channel)

Also, thanks for the library :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions