Hide a QMessageBox in PyQt5 without triggering its exec() to return?

I have a rather complex code that utilizes QMessageBox through several states, showing results of processing of a separate thread, mostly showing different results/statuses of the process, but every once in a while (and also at the end of the process) offering the user some choices via the buttons – which actually works good enough for me.

The whole thing relies on the QMessageBox.exec_ blocking while this processing thread runs, and only exiting on when the user (is allowed to) clicks a button (and a couple of times, forcibly from the other thread using the .done(0) method).

I now have the problem of wanting to run the same process at application startup, but not show the message box for the most part, – and only show it for a couple of specific (but not all) results of the process.

Since it would be quite complex to refactor this into a “no-gui” and “gui” part, I though I’d try what I thought was an easy solution: simply run the whole process including the QMessageBox instantiation, but hide the QMessageBox right before its blocking .exec_() is called. This would have been a great idea, unfortunately it turns out that calling .hide() causes .exec_() to return and stop its blocking, which is demonstrated by the following example:

import sys

from PyQt5.QtWidgets import QApplication, QDialog, QMainWindow, QMessageBox, QPushButton
from PyQt5.QtCore import (QTimer)

class MainWindow(QMainWindow):
  def __init__(self):
    super().__init__()

    self.setWindowTitle("My App")
    button = QPushButton("Press me for a dialog!")
    button.clicked.connect(self.button_clicked)
    self.setCentralWidget(button)
    # one second after startup, run raise dialog quietly
    QTimer.singleShot(1000, lambda : self.raise_dialog(quiet=True))

  def button_clicked(self, s):
    self.raise_dialog()

  def raise_dialog(self, quiet=False):
    print("some process runs here ...")
    dlg = QMessageBox(self)
    dlg.setWindowTitle("I have a question!")
    dlg.setText("This is a simple dialog")
    if quiet:
      QTimer.singleShot(1000, dlg.hide) # if I just call dlg.hide() here, ,_exec shows it again; in actual code, using this with 1 ms delay, but here 1000 for test
    button = dlg.exec()
    print("exec() returned - proceeding with code after exec() ...")
    if button == QMessageBox.Ok:
      print("OK!")

app = QApplication(sys.argv)
window = MainWindow()
window.show()

app.exec()

I’ve deliberately set the hiding to occur 1 second after the QMessageBox is shown, so it is easily visible that as soon as the QMessageBox is hidden/disappears, the text “exec() returned - proceeding with code after exec() ...” is printed on the terminal.

So, I wanted to ask – is there a way to hide a QMessageBox (which would hopefully also hide its modality, so the main window can be interacted with in the meantime), without causing it to exit/return from its .exec_ blocking call (like QMessageBox.hide() does)?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật