通过模态对话框关闭PyQt应用程序

时间:2016-01-01 17:44:04

标签: python qt modal-dialog pyqt4

我目前正在使用Qt4 python绑定编写应用程序,该绑定要求用户提供登录凭据。在启动我的应用程序时,我会显示一个模态对话框,用户可以在其中输入数据。由于当用户未登录时应用程序无法提供有用的服务,因此我想在用户单击取消按钮时关闭应用程序。

因此,如果对话框返回否定结果,我只需调用close()的{​​{1}}方法。通常这会导致应用程序退出,因为不再有可交互的窗口。

但是,如果之前显示过模态对话框,应用程序只会继续运行,我必须手动终止它。

以下是最小示例的代码

main.py:

QMainWindow

MyMainWindow.py:

import sys
from PyQt4.QtGui import QApplication
from MyMainWindow import MyMainWindow

app = QApplication(sys.argv)

window = MyMainWindow()
window.show()

app.exec_()

print "Terminated"

MyDialog.py:

from PyQt4.QtGui import QMainWindow
from PyQt4 import QtGui
from MyDialog import MyDialog

class MyMainWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)

        self.setWindowTitle("Close App Test")

        self.centralwidget = QtGui.QWidget(self)
        self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)

        self.closeButton = QtGui.QPushButton(self.centralwidget)
        self.closeButton.setText("Close")

        self.closeButton.clicked.connect(self.exit)

    def show(self):
        QMainWindow.show(self)

        myDialog = MyDialog(self) 
        res = myDialog.exec_()  

        if res == 0:
            self.exit()
        else:
            print "Continue"

    def exit(self):
        self.close()

如果单击模态对话框上的“关闭”按钮,即使关闭所有窗口,应用程序也会继续运行。如果单击“Do Nothing”按钮,并且主窗口上的关闭按钮关闭了应用程序,则一切都按预期工作,应用程序终止。

我无法真正看到两种情况之间的区别,因为每次我只是在主窗口上调用from PyQt4.QtGui import QDialog from PyQt4 import QtGui class MyDialog(QDialog): def __init__(self, parent = None): QDialog.__init__(self, parent) self.setWindowTitle("Modal Dialog") self.resize(200, 50) self.closeButton = QtGui.QPushButton(self) self.closeButton.setText("Close") self.closeButton.move(10, 10) self.otherButton = QtGui.QPushButton(self) self.otherButton.setText("Do Nothing") self.otherButton.move(100, 10) self.closeButton.clicked.connect(self.reject) self.otherButton.clicked.connect(self.accept) 。我可以假设我在处理模态对话框时遇到错误。我甚至尝试使用closemyDialog.close()以及myDialog.destroy()quitOnClose窗口属性进行修补,没有任何正面影响。

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

如果单击模式对话框上的“关闭”按钮,则在app.exec_()之前调用self.exit(),以便应用程序继续运行。应该在主事件循环运行时调用self.exit()。

以下是该案例的可能解决方案:

from PyQt4.QtGui import QMainWindow
from PyQt4 import QtGui
from pyQt4 import QtCore

from MyDialog import MyDialog

class MyMainWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)

        self.setWindowTitle("Close App Test")

        self.centralwidget = QtGui.QWidget(self)
        self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)

        self.closeButton = QtGui.QPushButton(self.centralwidget)
        self.closeButton.setText("Close")

        self.closeButton.clicked.connect(self.exit)

    def login(self):
        myDialog = MyDialog(self) 
        res = myDialog.exec_()  

        if res == 0:
            self.exit()
        else:
            print "Continue"

    def show(self):
        QMainWindow.show(self)
        QtCore.QTimer.singleShot(0, self.login)        

    def exit(self):
        self.close()