Skip to content

Instantly share code, notes, and snippets.

Created September 12, 2017 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2f1498183267e6776efa5cf6cf0f97c6 to your computer and use it in GitHub Desktop.
Save anonymous/2f1498183267e6776efa5cf6cf0f97c6 to your computer and use it in GitHub Desktop.
Qt catch closeEvent example
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui
class MyWindow(QtGui.QMainWindow):
def closeEvent(self, event):
if self.showDialog():
event.accept()
else:
event.ignore()
def showDialog(self):
msg = QtGui.QMessageBox()
msg.setText('Do you really want to Exit?')
msg.setStandardButtons(QtGui.QMessageBox.Ok | QtGui.QMessageBox.Cancel)
retval = msg.exec_()
if retval == QtGui.QMessageBox.Ok:
return True
else:
return False
def main():
app = QtGui.QApplication(sys.argv)
w = MyWindow()
w.resize(250, 150)
w.move(300, 300)
w.setWindowTitle('Exit Test')
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment