问题
is there a way to detect if user trying to close window? For example, in Tkinter we can do something like this:
def exit_dialog():
#do stuff
pass
root = Tk()
root.protocol("WM_DELETE_WINDOW", exit_dialog)
root.mainloop()
Thanks.
回答1:
Override the closeEvent method of QWidget in your main window.
For example:
class MainWindow(QWidget): # or QMainWindow
...
def closeEvent(self, event):
# do stuff
if can_exit:
event.accept() # let the window close
else:
event.ignore()
Another possibility is to use the QApplication's aboutToQuit signal like this:
app = QApplication(sys.argv)
app.aboutToQuit.connect(myExitHandler) # myExitHandler is a callable
来源:https://stackoverflow.com/questions/9249500/pyside-pyqt-detect-if-user-trying-to-close-window