X button does not fire the closeEvent function
-
Hello
I'm trying to use the closeEvent function when the user press X on main window but Python never fired closeEvent function.
I read many posts about this issue but eventually I was not succeeded to resolve this.
Any ideas why?
Here is the code:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtWidgets import QAction, QMessageBox, QMenu, QWidgetclass Ui_MainWindow(object):
def init(self):
super(Ui_MainWindow, self).init()
print("Initialization succeeded")def closeEvent(self, event): print("closeEvent has been called") userResult = QMessageBox.question(None, "Quit", "Do you want to close the program?", QMessageBox.Yes, QMessageBox.No) if userResult == QMessageBox.Yes: event.accept() else: event.ignore() def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(1920, 1080) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("Milvus UI", "Milvus UI"))if name == "main":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_()) -
Hello
I'm trying to use the closeEvent function when the user press X on main window but Python never fired closeEvent function.
I read many posts about this issue but eventually I was not succeeded to resolve this.
Any ideas why?
Here is the code:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtWidgets import QAction, QMessageBox, QMenu, QWidgetclass Ui_MainWindow(object):
def init(self):
super(Ui_MainWindow, self).init()
print("Initialization succeeded")def closeEvent(self, event): print("closeEvent has been called") userResult = QMessageBox.question(None, "Quit", "Do you want to close the program?", QMessageBox.Yes, QMessageBox.No) if userResult == QMessageBox.Yes: event.accept() else: event.ignore() def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(1920, 1080) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("Milvus UI", "Milvus UI"))if name == "main":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())@EyalT
Please use the forum Code tags around blocks of code you paste.It looks like your
def closeEvent()is a member of yourUi_MainWindowclass. That is no good. It is an event method ofQMainWindowand needs overriding. Put it in yourMainWindowclass instead.EDIT
Oh, yourMainWindowis a variable, not a class, and it's just directly aQMainWindow(). You will either need to create a derived class for it, or you may be able to achieve it via QObject.eventFilter(). -
@EyalT
Please use the forum Code tags around blocks of code you paste.It looks like your
def closeEvent()is a member of yourUi_MainWindowclass. That is no good. It is an event method ofQMainWindowand needs overriding. Put it in yourMainWindowclass instead.EDIT
Oh, yourMainWindowis a variable, not a class, and it's just directly aQMainWindow(). You will either need to create a derived class for it, or you may be able to achieve it via QObject.eventFilter(). -
@JonB said in X button does not fire the closeEvent function:
QObject.eventFilter().
Thank you very much, I'll try and update.
-
@EyalT I didn't succeeded and I would greatly appreciate it if you could make the necessary changes to the code I uploaded