隐藏pyqt主窗口会影响儿童对话的行为
在PYQT5中,我想知道为什么隐藏QMainWindow会影响儿童对话的行为?
例如,在下面的代码中,如果我单击“显示对话框” - > “确定”,显示QmessageBox,然后程序终止。但是,如果我在showchilddialog()中评论self.hide()和self.show(),则该程序返回显示在显示qmessagebox之后显示子对话框。
理想情况下,我希望该程序在显示Qmessagebox后返回“子对话”,但是在显示“子”对话框时,我也想隐藏主窗口。
如何同时实现这两种行为?
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_())
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_())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您更改此行:
到此行:
它可以正常工作。
if you change this line:
to this line:
it works properly.