Grr
Grr

Reputation: 16099

PyQt5 get QErrorMessage to block Mainwindow

In PyQt5, and I believe retroactively at least to 4, if a QMessageBox is initialized with MainWindow as the parent it will block any input to MainWindow until the MessageBox is accepted, closed, etc...

However, if you initialize a QErrorMessage using MainWindow as parent this behavior does not occur. Is there a way to pass some sort of window parameter to QErrorMessage such that it blocks the parent window until it is closed?

n.b. I realize that I can make a QMessageBox behave and appear like a QErrorMessage. I would just like to know if the above described behavior is possible.

Upvotes: 1

Views: 4450

Answers (1)

user3419537
user3419537

Reputation: 5000

Set your QErrorMessage as modal using setWindowModality(QtCore.Qt.WindowModal)

from PyQt5 import QtCore, QtWidgets

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

        emsg = QtWidgets.QErrorMessage(self)
        emsg.setWindowModality(QtCore.Qt.WindowModal)

        cwidget = QtWidgets.QWidget()
        layout = QtWidgets.QVBoxLayout(cwidget)
        lineedit = QtWidgets.QLineEdit()
        button = QtWidgets.QPushButton('Show message')
        button.clicked.connect(lambda: emsg.showMessage('Message: ' + lineedit.text()))
        layout.addWidget(lineedit)
        layout.addWidget(button)
        self.setCentralWidget(cwidget)


app = QtWidgets.QApplication([])
win = MainWindow()
win.show()
app.exec_()

Upvotes: 4

Related Questions