Reputation: 825
I have this code for close event
def closeEvent(self, event):
print "cloce"
quit_msg = "Are you sure you want to exit the program?"
reply = QtGui.QMessageBox.question(self, 'Message',
quit_msg, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
event.accept()
else:
event.ignore()
But, when i klik on x on the top of window, this is not called. Should I connect that klik with this function or something else do so it will work
Upvotes: 1
Views: 2286
Reputation: 1802
You can implement your own event filter
class custom(QWidget):
def __init__(self):
super(custom, self).__init__()
self.installEventFilter(self)
def eventFilter(self, qobject, qevent):
qtype = qevent.type()
if qtype == QEvent.Close:
qobject.hide()
return True
# parents event handler for all other events
return super(custom,self).eventFilter(qobject, qevent)
Upvotes: 0
Reputation:
Calling QDialog::reject ()
hides the dialog instead closing it. To present a prompt before rejecting a QDialog
just reimplement the reject
slot:
#!/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_())
Upvotes: 1