Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loop stops after dialog goes away #65

Open
ericfrederich opened this issue Jul 14, 2016 · 6 comments
Open

Loop stops after dialog goes away #65

ericfrederich opened this issue Jul 14, 2016 · 6 comments
Labels

Comments

@ericfrederich
Copy link

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.
@harvimt
Copy link
Owner

harvimt commented Jul 14, 2016

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
Copy link
Owner

harvimt commented Jul 14, 2016

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
Copy link
Owner

harvimt commented Jul 18, 2016

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

@harvimt
Copy link
Owner

harvimt commented Jul 19, 2016

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
Copy link
Owner

harvimt commented Jul 20, 2016

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
Copy link
Owner

harvimt commented Jul 20, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants