隐藏pyqt主窗口会影响儿童对话的行为

发布于 2025-02-08 05:40:26 字数 1842 浏览 1 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在夏天 2025-02-15 05:40:27

如果您更改此行:

self.dialog = childDialog(self)

到此行:

self.dialog = childDialog()

它可以正常工作。

if you change this line:

self.dialog = childDialog(self)

to this line:

self.dialog = childDialog()

it works properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文