Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QtAsyncIO conflict with QDialog.exec()
Forum Updated to NodeBB v4.3 + New Features

QtAsyncIO conflict with QDialog.exec()

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 2 Posters 306 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dayongxie
    wrote on last edited by
    #1

    When I use QtAsyncIO, I meet this problem,the code:

    # Copyright (C) 2022 The Qt Company Ltd.
    # SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
    from __future__ import annotations
    
    from PySide6.QtCore import Qt
    from PySide6.QtWidgets import (QApplication, QLabel, QMainWindow, QPushButton, QVBoxLayout, QWidget, QMessageBox)
    
    import PySide6.QtAsyncio as QtAsyncio
    
    import asyncio
    import sys
    
    
    class MainWindow(QMainWindow):
    
        def __init__(self):
            super().__init__()
    
            widget = QWidget()
            self.setCentralWidget(widget)
    
            layout = QVBoxLayout(widget)
    
            self.text = QLabel("The answer is 42.")
            layout.addWidget(self.text, alignment=Qt.AlignmentFlag.AlignCenter)
    
            async_trigger = QPushButton(text="What is the question?")
            async_trigger.clicked.connect(lambda: asyncio.ensure_future(self.set_text()))
            layout.addWidget(async_trigger, alignment=Qt.AlignmentFlag.AlignCenter)
    
        async def set_text(self):
            await asyncio.sleep(1)
            asyncio.ensure_future(self.set_text_2())
            # Dialog().exec() 
            QMessageBox.information(self, "Info", "This is an async message box.")
            self.text.setText("What do you get if you multiply six by nine?")
    
        async def set_text_2(self):
            await asyncio.sleep(1)
            print("hello world")
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        main_window = MainWindow()
        main_window.show()
    
        QtAsyncio.run(handle_sigint=True)
    

    Create a new async task without await,then call QMessageBox.information() or QDialog.exec()
    below exception will occur:

    Traceback (most recent call last):
    File "E:\test.venv\lib\site-packages\PySide6\QtAsyncio\events.py", line 571, in <lambda>
    self._schedule_event(self._timeout, lambda: self._cb())
    File "E:\test.venv\lib\site-packages\PySide6\QtAsyncio\events.py", line 579, in _cb
    self._callback(*self._args)
    File "E:\test.venv\lib\site-packages\PySide6\QtAsyncio\tasks.py", line 98, in _step
    asyncio._leave_task(self._loop, self) # type: ignore[arg-type]
    RuntimeError: Leaving task Task 'QtTask' with state: Done with exception (RuntimeError("Cannot enter into task Task 'QtTask' with state: Pending while another task Task 'QtTask' with state: Pending is being executed.")) does not match the current task Task 'QtTask' with state: Pending.

    1 Reply Last reply
    1
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      I haven't used that module yet however I think that manually triggering a secondary event loop like that is wrong.

      I just found this example which confirms my intuition.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved