Closed omelia-iliffe closed 9 months ago
Remove the qasync.asyncSlot()
decorator from the timer callback, as well as the async
keyword and it will work as you've described.
class TestWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.show()
self.timer = QTimer(self, timeout=self.open_message_box, singleShot=True, interval=1)
self.timer.start()
def open_message_box(self):
msg = QMessageBox().warning(self, "Warning", "This is a warning message", QMessageBox.StandardButton.Ok, QMessageBox.StandardButton.Cancel)
if msg == QMessageBox.StandardButton.Ok:
self.close()
If you need to have an async operation in the open_message_box
, use the asyncSlot as you do currently:
class TestWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.show()
self.timer = QTimer(self, timeout=self.open_message_box, singleShot=True, interval=1)
self.timer.start()
@qasync.asyncSlot()
async def open_message_box(self):
await asyncio.sleep(1)
msg = QMessageBox().warning(self, "Warning", "This is a warning message", QMessageBox.StandardButton.Ok, QMessageBox.StandardButton.Cancel)
if msg == QMessageBox.StandardButton.Ok:
self.close()
Although I appreciate your reply, your second suggestion will also produce the same error if the sleep is less than the sleep in test() I have learnt a lot more since posting this issue. This is how I would approach it now. Instead of relying on the QMessageBox().warning which is blocks the eventloop, I would create a QMessageBox in the init and attach signals to handle its result. The function that handles the results can be decorated with @qasync.asyncSlot(int).
class TestWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.show()
self.message_box = QMessageBox()
self.message_box.setText("This is a warning message")
self.message_box.setWindowTitle("Warning")
self.message_box.setIcon(QMessageBox.Icon.Warning)
self.message_box.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel)
self.message_box.finished.connect(self.message_box_results)
self.message_box.show()
@qasync.asyncSlot(int)
async def message_box_results(self, result):
print(QMessageBox.StandardButton(result))
if result == QMessageBox.StandardButton.Ok:
await another_async_function()
self.close()
else:
self.close()
I am trying to use QMessageBox to prompt the user but it is cause other tasks I want to happen in the background to not start.
I've created a small code snippet that reproduces the error I am using a singleshot timer with a small interval to trigger the open_message_box task which than prevents the test task from starting.
How should I modify my code to function as expected. Message box opens and in background the test function is run
The error is: