quamash icon indicating copy to clipboard operation
quamash copied to clipboard

Loop stops after dialog goes away

Open ericfrederich opened this issue 9 years ago • 6 comments

Please disregard if I'm doing something bone-headed. It may be due to my lack, of either asyncio or Qt knowledge.

I'm trying to show two dialogs, one after the other. In this small example I use QWizard's After the first one goes away either by pressing cancel or finish, I get the error shown below. However, if I have a button sitting there in the background it behaves just fine. If instead of running run_until_complete(amain()) I run asyncio.ensure_future(amain()) followed by loop.run_forever() then I don't get any errors but the 2nd wizard never shows up. It just exists as soon as there is nothing left to display on the screen.

#!/usr/bin/env python3
import asyncio
import sys

from PyQt5.QtWidgets import QApplication, QWizard, QWizardPage, QDialog, QPushButton
from quamash import QEventLoop


def async_dialog_exec(dlg: QDialog):
    fut = asyncio.Future()
    dlg.finished.connect(lambda result: fut.set_result(result))
    dlg.open()
    return fut

async def amain():
    # if these two lines are uncommented you'll be able to see both wizards
    # b = QPushButton('hi')
    # b.show()
    wiz1 = QWizard()
    wiz1.addPage(QWizardPage())
    wiz1.setWindowTitle("First Wizard")
    result = await async_dialog_exec(wiz1)
    if result != QDialog.Accepted:
        return

    wiz2 = QWizard()
    wiz2.addPage(QWizardPage())
    wiz2.setWindowTitle("Second Wizard")
    result = await async_dialog_exec(wiz2)
    if result != QDialog.Accepted:
        return

app = QApplication(sys.argv)
loop = QEventLoop(app)
asyncio.set_event_loop(loop)
with loop:
    loop.run_until_complete(amain())
Traceback (most recent call last):
  File "/home/vagrant/PycharmProjects/test/dlg_example.py", line 37, in <module>
    loop.run_until_complete(amain())
  File "/opt/Python-3/lib/python3.5/site-packages/quamash/__init__.py", line 270, in run_until_complete
    raise RuntimeError('Event loop stopped before Future completed.')
RuntimeError: Event loop stopped before Future completed.

ericfrederich avatar Jul 14 '16 14:07 ericfrederich

this looks like it's working as expected. The wizard closes, then the application quits because all windows attached to the application are closed, but the asyncio loop attached to the QApplication hasn't closed yet. You either need the extra QWidget or you need to reorganize your application.

Example reordering:

async def amain():
    global app # not technically needed, but adds clarity
    wiz1 = QWizard()
    wiz1.addPage(QWizardPage())
    wiz1.setWindowTitle("First Wizard")
    result = await async_dialog_exec(wiz1)
    if result != QDialog.Accepted:
        app.quit()
        return

    wiz2 = QWizard()
    wiz2.addPage(QWizardPage())
    wiz2.setWindowTitle("Second Wizard")
    result = await async_dialog_exec(wiz2)
    if result != QDialog.Accepted:
        app.quit()
        return
    app.quit()

app = QApplication(sys.argv)
loop = QEventLoop(app)
asyncio.set_event_loop(loop)
with loop:
    asyncio.ensure_future(amain())
    loop.run_forever()

Just because this is expected doesn't mean it's the best way for quamash to be designed. (perhaps the loop should wrap a QtCore.QEventLoop or QtCore.QThread instead of a QApplication in order to keep app from quitting until the future run with run_until_complete exits

harvimt avatar Jul 14 '16 19:07 harvimt

This is now a sort of feature request that's essentially a duplicate of #33. In all cases so far there's been a workaround, but nested execs are a thing people expect to be able to do since you can do them in vanilla Qt.

harvimt avatar Jul 14 '16 21:07 harvimt

I re-read this and I will need to investigate further.

harvimt avatar Jul 18 '16 23:07 harvimt

hrm. I'm not able to reproduce (I'm on Mac OS X using python 3.5.2 and PyQt5.6) I'll try on windows and linux when I get home.

harvimt avatar Jul 19 '16 20:07 harvimt

replicated on windows. In all cases the problem is that the loop exits when all windows are closed, so a dummy window that keeps the app open and the

do to the magic of duck typing, this will work:

app = QApplication(sys.argv)
wiz1 = QWizard()
wiz1.addPage(QWizardPage(wiz1))
wiz1.setWindowTitle("First Wizard")
loop = QEventLoop(wiz1)
asyncio.set_event_loop(loop)
with loop:
    loop.run_forever()

wiz2 = QWizard()
wiz2.addPage(QWizardPage(wiz2))
wiz2.setWindowTitle("Second Wizard")
loop = QEventLoop(wiz2)
asyncio.set_event_loop(loop)
with loop:
    loop.run_forever()

harvimt avatar Jul 20 '16 03:07 harvimt

adding app.setQuitOnLastWindowClosed(False) makes everything work correctly.

harvimt avatar Jul 20 '16 04:07 harvimt