Reputation: 21
I would like to give a choice if the user is sure to close the app, when the Windows close button is being pressed? So far I've coded this, which prompting that the App is about to quit, but I couldn't catch the event or make the App wait until the user confirms. I'm pretty new to this, can anyone help PLEASE. Here is my code,
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
##
#......some more codes
##
QtCore.QMetaObject.connectSlotsByName(MainWindow)
app.aboutToQuit.connect(self.closeEvent)
def retranslateUi(self, MainWindow):
## codes
def closeEvent(self):
print("User has clicked the red x on the main window")
## HERE I WANT TO GIVE AN OPTION TO THE USER IF THEY REALLY WANT TO QUIT
## AND ON THIS TIME THE MAIN APP/ MAIN WINDOW NEEDS TO BE ON AS IT IS
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Upvotes: 1
Views: 15359
Reputation: 53
For PyQt5: There is 2 options in my solution, first one is for the "x" button, second one is for menu button, decide which better for you. Based on Natan's answer exitEvent
import sys
from PyQt5.QtWidgets import QApplication, QMessageBox, QMainWindow, QAction
class window(QMainWindow):
def __init__(self):
super().__init__()
def createUI(self):
self.setGeometry(500, 300, 700, 700)
self.setWindowTitle("window")
quit = QAction("Quit", self)
quit.triggered.connect(self.exit_window)
menubar = self.menuBar()
fmenu = menubar.addMenu("File")
fmenu.addAction(quit)
def closeEvent(self, event):
close = QMessageBox()
close.setText("You sure?")
close.setStandardButtons(QMessageBox.Yes | QMessageBox.Cancel)
close = close.exec()
if close == QMessageBox.Yes:
event.accept()
else:
event.ignore()
def exit_window(self, event):
close = QtWidgets.QMessageBox.question(self,
"QUIT?",
"Are you sure want to STOP and EXIT?",
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
if close == QtWidgets.QMessageBox.Yes:
# event.accept()
sys.exit()
else:
pass
main = QApplication(sys.argv)
window = window()
window.createUI()
window.show()
sys.exit(main.exec_())
Upvotes: 1
Reputation: 3648
This post describes how it should be done.
In short here's the answer from furas
(I used PyQt4.QtGui, but I think this is the exact same as PyQt5.QtWidgets for this application)
import sys
from PyQt4.QtGui import QApplication, QMessageBox, QMainWindow, QAction
class Ui_MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
def setupUI(self):
self.setGeometry(500, 300, 700, 700)
self.setWindowTitle("window")
finish = QAction("Quit", self)
finish.triggered.connect(self.closeEvent)
menubar = self.menuBar()
fmenu = menubar.addMenu("File")
fmenu.addAction(finish)
def retranslateUi(self):
## codes
codes = "___"
def closeEvent(self, event):
close = QMessageBox.question(self,
"QUIT",
"Sure?",
QMessageBox.Yes | QMessageBox.No)
if close == QMessageBox.Yes:
event.accept()
else:
event.ignore()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Ui_MainWindow()
window.setupUI()
window.show()
sys.exit(app.exec_())
Upvotes: 3