Show Messagebox when Windows X close button is pressed (PyQt4 Python) -


class ui_mainwindow(qtgui.qmainwindow):     def __init__(self):         qtgui.qmainwindow.__init__(self)         app.atexit.connect(self.exitapp)      def exitapp(self):         atexit.register(self.exitapp(app))         ret = qtgui.qmessagebox.question(none, 'close request', 'are sure want quit?',                                          qtgui.qmessagebox.yes | qtgui.qmessagebox.no,                                          qtgui.qmessagebox.yes)         if ret == qtgui.qmessagebox.yes:             sys.exit(app.exec())         elif ret == qtgui.qmessagebox.no:             pass      def setupui(self, mainwindow):         mainwindow.setobjectname(_fromutf8("mainwindow"))         mainwindow.resize(1280, 760)         mainwindow.setmaximumsize(qtcore.qsize(16777215, 16777215))  def main():     mainwindow = qtgui.qmainwindow()     ui = ui_mainwindow()     ui.setupui(mainwindow)     mainwindow.showmaximized()     ui_mainwindow.loaddata(ui)     sys.exit(app.exec_())  if __name__ == "__main__":     app = qtgui.qapplication(sys.argv)     main() 

i want show message box showing "are sure want quit?" when user clicks on windows close button (x button on top).

how signal when user clicks on button?

solution similar detect when "x" or close button pressed using pyqt4.

you have overwrite closeevent method , ignore event if not want window close. have modified code little because had problems reproducing it.

class mainwindow(qtgui.qmainwindow):     def __init__(self):         qtgui.qmainwindow.__init__(self)      def closeevent(self, event):         ret = qtgui.qmessagebox.question(none, 'close request', 'are sure want quit?',                                          qtgui.qmessagebox.yes | qtgui.qmessagebox.no,                                          qtgui.qmessagebox.yes)         if ret == qtgui.qmessagebox.yes:             qtgui.qmainwindow.closeevent(self, event)         else:             event.ignore()   if __name__ == "__main__":     app = qtgui.qapplication(sys.argv)     ui = mainwindow()     ui.show()     sys.exit(app.exec_()) 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -