Patrick
Patrick

Reputation: 2544

Don’t close Dialog on pressing OK button of QMessageBox

I have called QMessageBox() like this:

class Main(QDialog):
    def __init__(self):
        self.view = QUiLoader().load("app.ui", self)
        self.view.show()
        self.functionA()
    ....
    functionA():
        try:
            ....
        except Exception:
            QMessageBox.critical(self, "Error", "System Failure")

def main():
    app = QApplication(sys.argv)
    a = Main()
    sys.exit(app.exec_())

if __name__ == "__main__"
    main()

When i click OK button of Message box it also closes my Dialog. How to avoid this ?

Upvotes: 2

Views: 2279

Answers (2)

Patrick
Patrick

Reputation: 2544

Use QMessageBox like this:

QMessageBox.critical(self.view, "Error", "System Failure")

Upvotes: 1

NoDataDumpNoContribution
NoDataDumpNoContribution

Reputation: 10860

Your code example (slightly altered to make it run) works for me:

from PySide.QtGui import *

class Main(QDialog):
    def __init__(self):
        super().__init__()
        self.show()
        self.functionA()

    def functionA(self):
        try:
            raise Exception()
        except Exception:
            QMessageBox.critical(self, "Error", "System Failure")

app = QApplication([])
a = Main()
app.exec_()

You can press OK on the message box and the dialog will not be closed. You are probably doing something else too that causes the closing of the dialog.

Upvotes: 1

Related Questions