简体   繁体   English

PyQt5获取QErrorMessage来阻止Mainwindow

[英]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... 在PyQt5中,我至少可以追溯到4,如果QMessageBox以MainWindow为父级初始化,它将阻塞对MainWindow的任何输入,直到MessageBox被接受,关闭等。

However, if you initialize a QErrorMessage using MainWindow as parent this behavior does not occur. 但是,如果使用MainWindow作为父级初始化QErrorMessage,则不会发生此行为。 Is there a way to pass some sort of window parameter to QErrorMessage such that it blocks the parent window until it is closed? 有没有一种方法可以将某种窗口参数传递给QErrorMessage,使其阻塞父窗口直到关闭它?

nb I realize that I can make a QMessageBox behave and appear like a QErrorMessage. 我意识到我可以使QMessageBox表现得像QErrorMessage一样。 I would just like to know if the above described behavior is possible. 我只想知道上述行为是否可能。

Set your QErrorMessage as modal using setWindowModality(QtCore.Qt.WindowModal) 使用setWindowModality(QtCore.Qt.WindowModal)QErrorMessage设置为模式

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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