简体   繁体   中英

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?

nb 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.

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_()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM