调用QDialog::reject ()
隐藏对话框而不是关闭它。要在拒绝QDialog
刚刚重新实现reject
插槽之前显示提示:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from PyQt4 import QtCore, QtGui
class myDialog(QtGui.QDialog):
def __init__(self, parent=None):
super(myDialog, self).__init__(parent)
self.buttonBox = QtGui.QDialogButtonBox(self)
self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
self.msgBox = QtGui.QMessageBox(self)
self.msgBox.setText("Are you sure you want to exit the program?")
self.msgBox.setStandardButtons(QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
self.msgBox.setDefaultButton(QtGui.QMessageBox.Yes)
@QtCore.pyqtSlot()
def reject(self):
msgBoxResult = self.msgBox.exec_()
if msgBoxResult == QtGui.QMessageBox.Yes:
return super(myDialog, self).reject()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('myDialog')
main = myDialog()
main.resize(400, 300)
main.show()
sys.exit(app.exec_())