python - PyQt4 ---- I want to use closeEvent() in my code -
i python newbie. developing pr-room management program. think if user presses x button , want give warning message . so, want use closeevent() in first code. in other words, first code + second code please
# -*- coding: utf-8 -*- # form implementation generated reading ui file 'c:\users\pjh11\desktop\dia3.ui' # # created: sun aug 17 16:23:08 2014 # by: pyqt4 ui code generator 4.11.1 # # warning! changes made in file lost! pyqt4 import qtcore, qtgui try: _fromutf8 = qtcore.qstring.fromutf8 except attributeerror: def _fromutf8(s): return s try: _encoding = qtgui.qapplication.unicodeutf8 def _translate(context, text, disambig): return qtgui.qapplication.translate(context, text, disambig, _encoding) except attributeerror: def _translate(context, text, disambig): return qtgui.qapplication.translate(context, text, disambig) class ui_dialog(object): def setupui(self, dialog): dialog.setobjectname(_fromutf8("dialog")) dialog.resize(190, 98) self.pushbutton = qtgui.qpushbutton(dialog) self.pushbutton.setgeometry(qtcore.qrect(0, 0, 191, 101)) font = qtgui.qfont() font.setpointsize(13) self.pushbutton.setfont(font) self.pushbutton.setobjectname(_fromutf8("pushbutton")) self.retranslateui(dialog) qtcore.qmetaobject.connectslotsbyname(dialog) def retranslateui(self, dialog): dialog.setwindowtitle(_translate("dialog", "dialog", none)) self.pushbutton.settext(_translate("dialog", "hi", none)) if __name__ == "__main__": import sys app = qtgui.qapplication(sys.argv) dialog = qtgui.qdialog() ui = ui_dialog() ui.setupui(dialog) dialog.show() sys.exit(app.exec_())
def closeevent(self, event): reply = qtgui.qmessagebox.question(self, 'message', "are sure quit?", qtgui.qmessagebox.yes | qtgui.qmessagebox.no, qtgui.qmessagebox.no) if reply == qtgui.qmessagebox.yes: event.accept() else: event.ignore()
your can implement class create closeevent
(as code close event), put them , create object in main. , can call close event using bool qwidget.close (self)
.
full example;
from pyqt4 import qtcore, qtgui try: _fromutf8 = qtcore.qstring.fromutf8 except attributeerror: def _fromutf8(s): return s try: _encoding = qtgui.qapplication.unicodeutf8 def _translate(context, text, disambig): return qtgui.qapplication.translate(context, text, disambig, _encoding) except attributeerror: def _translate(context, text, disambig): return qtgui.qapplication.translate(context, text, disambig) class ui_dialog(object): def setupui(self, dialog): self.dialog = dialog self.dialog.setobjectname(_fromutf8("self.dialog")) self.dialog.resize(190, 98) self.pushbutton = qtgui.qpushbutton(self.dialog) self.pushbutton.setgeometry(qtcore.qrect(0, 0, 191, 101)) font = qtgui.qfont() font.setpointsize(13) self.pushbutton.setfont(font) self.pushbutton.setobjectname(_fromutf8("pushbutton")) self.retranslateui(self.dialog) qtcore.qmetaobject.connectslotsbyname(self.dialog) qtcore.qobject.connect(self.pushbutton, qtcore.signal('released()'), self.dialog.close) # <- put signal close when clicked. def retranslateui(self, dialog): self.dialog.setwindowtitle(_translate("self.dialog", "self.dialog", none)) self.pushbutton.settext(_translate("self.dialog", "hi", none)) class qcustomdialog (qtgui.qdialog): # <- implement own def closeevent(self, event): reply = qtgui.qmessagebox.question(self, 'message', "are sure quit?", qtgui.qmessagebox.yes | qtgui.qmessagebox.no, qtgui.qmessagebox.no) if reply == qtgui.qmessagebox.yes: event.accept() else: event.ignore() if __name__ == "__main__": import sys app = qtgui.qapplication(sys.argv) dialog = qcustomdialog() ui = ui_dialog() ui.setupui(dialog) dialog.show() sys.exit(app.exec_())
regards,
Comments
Post a Comment