In PyQt5, I'm wondering why hiding a QMainWindow affects the behaviour of child dialogs?
For example, in the code below, if I click "Show dialog" -> "OK", the QMessageBox is displayed then the program terminates. However, if I comment out the self.hide() and self.show() in showChildDialog(), the program returns to showing the child dialog after the QMessageBox is displayed.
Ideally, I would like the program to return to the child dialog after displaying the QMessageBox, but I also want to hide the main window while the child dialog is displayed.
How can I achieve both behaviours simultaneously?
from PyQt5 import QtWidgets
class mainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(mainWindow, self).__init__(parent)
self.dialog = childDialog(self)
self.setWindowTitle('Main Window')
self.buttonShow = QtWidgets.QPushButton(self)
self.buttonShow.setText("Show Dialog")
self.buttonShow.clicked.connect(self.showChildDialog)
def showChildDialog(self):
self.hide() # works as intended if this commented out...
self.dialog.exec()
self.show() # and this.
class childDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super(childDialog, self).__init__(parent)
self.buttonOk = QtWidgets.QPushButton(self)
self.buttonOk.setText("OK")
self.buttonOk.clicked.connect(self.ok_clicked)
self.buttonHide = QtWidgets.QPushButton(self)
self.buttonHide.setText("Close")
self.buttonHide.clicked.connect(self.hide_clicked)
self.layout = QtWidgets.QVBoxLayout(self)
self.layout.addWidget(self.buttonOk)
self.layout.addWidget(self.buttonHide)
def hide_clicked(self):
self.accept()
def ok_clicked(self):
# do somehting here that causes an error
dialog = QtWidgets.QMessageBox(text='an error occurred')
dialog.exec()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setApplicationName('myWindow')
main = mainWindow()
main.show()
sys.exit(app.exec_())
if you change this line:
self.dialog = childDialog(self)
to this line:
self.dialog = childDialog()
it works properly.