Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QMessageBox hangs but doesn't show with exec()



  • I have a dialog popup defined like so:

    class ClientWindow(QMainWindow):
        def __init__(self):
            super(ClientWindow, self).__init__()
            loadUi(os.path.join(os.path.dirname(__file__), "window.ui"), self)
    
        def display_text_box(self, text):
            text_box = QMessageBox()
            text_box.setWindowTitle("Alert!")
            text_box.setText(text)
            text_box.setIcon(QMessageBox.Warning)
            text_box.setStandardButtons(QMessageBox.Ok)
            print("before show")
            text_box.exec()
            print("after show")
            self.close()
    

    display_text_box() is called by another window, and when I attempt to show the window, it just makes the ClientWindow stop responding.
    I know exec() is supposed to hang, since it doesn't return until the window is closed unlike show(), but the window doesn't show up either. Weirdly, in another window that's defined in the same way but inherits QDialog instead, a popup dialog window appears using the exact same code.

    Is the window not showing related to the "parent" class inheriting QMainWindow? If so, what's the difference between QDialog and QMainWindow and will it have any drastic side effects if I switch?



  • @DoubleFelix
    This should indeed work as-is. I don't know what "called by another window" means, given that display_text_box() is defined in ClientWindow/the main window. I would start from a standalone example to check it works, and then build towards whatever situation you are in here.



  • I don't know what "called by another window" means

    I have this window class defined in another, and display_text_box is called by that class.



  • @DoubleFelix
    I don't know what you're doing defining a method in a class derived from QMainWindow, where the method displays a modal dialog and you're calling it from outside the main window. Whether that has any bearing on your issue I don't know.



  • How should I do it then?



  • Is it possible that the 2 already open windows are causing some error?



  • @DoubleFelix
    I don't know what your code is. But (IMHO anyway) no other class should call any method in the main window class, nor have any access to it, nor import it. And I cannot see why the main window would want to export a method whose job is to show a message box. Further, that method ends by closing the main window, and it did not even open it.

    Start by redesigning some clean & logical flow. Why is some ClientWindow derived from QMainWindow? If you need this message box to be show, why can't the other place create and show it? Or, if for some reason it's necessary to have the main window show it, have the outside world emit a signal requesting it and have the main window slot onto that to show the dialog.


Log in to reply