Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Qt close event with Python issue
Cobra91151 last edited by Cobra91151
Hi! I want to intercept close event in my program. The problem when I press the
Xbutton it closes omitting close event. Nothing is printed to the console. Thanks in advance for your help.
class Ui_mainWindow(object): def closeEvent(self, event): event.ignore() print("Test...")
Note a mini fully working bit of example code is extremely helpful as it means someone trying to help you does not have to do it for you. As such here is a fully working bit of code that does what you say your code is not doing. I am using Python 3.7 / pyqt5 / Win10 and it works just fine
from sys import exit as sysExit from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class CenterPanel(QWidget): def __init__(self, MainWin): QWidget.__init__(self) CenterPane = QHBoxLayout(self) CenterPane.addWidget(QTextEdit()) self.setLayout(CenterPane) class UI_MainWindow(QMainWindow): def __init__(self): super(UI_MainWindow, self).__init__() self.setCentralWidget(CenterPanel(self)) def closeEvent(self, event): print("Close Test 1") if __name__ == '__main__': MainApp = QApplication() MainGui = UI_MainWindow() MainGui.show() sysExit(MainApp.exec_())
OK. Here is the code:
rom PyQt5 import QtCore, QtGui, QtWidgets, Qt import sys from test1 import Ui_mainWindow if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True); QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True); QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_UseOpenGLES, True); MainWindow = QtWidgets.QMainWindow() mainUI = Ui_mainWindow() mainUI.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
class Ui_mainWindow(object): def closeEvent(self, event): event.ignore() print("Test...") def setupUi(self, mainWindow): mainWindow.setObjectName("mainWindow") mainWindow.resize(1895, 899) mainWindow.setStyleSheet("") mainWindow.setIconSize(QtCore.QSize(58, 48)) mainWindow.setDockNestingEnabled(True)
@Cobra91151 I had added to my previous post a bit of example code that does work perhaps that will help you ascertain where you problem resides
I had added to my previous post a bit of example code that does work perhaps that will help you ascertain where you problem resides
Ok. I will check it. Thanks.
Okay here is a working version of your program I think you can see where you might need to make adjustments to your code
import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class UI_MainWindow(QMainWindow): def __init__(self): super(UI_MainWindow, self).__init__() #class Ui_mainWindow(object): # def setupUi(self, mainWindow): # mainWindow.setObjectName("mainWindow") # self.resize(1895, 899) self.setStyleSheet("") self.setIconSize(QSize(58, 48)) self.setDockNestingEnabled(True) def closeEvent(self, event): # event.ignore() print("Test...") if __name__ == "__main__": app = QApplication(sys.argv) QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True); QCoreApplication.setAttribute(Qt.AA_UseHighDpiPixmaps, True); QCoreApplication.setAttribute(Qt.AA_UseOpenGLES, True); # MainWindow = QMainWindow() # mainUI = Ui_mainWindow() # mainUI.setupUi(MainWindow) # MainWindow.show() MainGui = UI_MainWindow() MainGui.show() sys.exit(app.exec_())
Note I removed the event.ignore because otherwise you have to basically crash the program to exit it and all you need to know is that you entered that function
Cobra91151 last edited by Cobra91151
C++developer, but this project should be done only with
Also, the reason I want the
closeEvent, because I want to stop loading page/free resources or do something to fix this
QWaitCondition: Destroyed while threads are still waiting
I noticed this issue only occurs when page loading is in progress while I close the program.
The issue is resolved.
First @Cobra91151 your welcome and glad I could help. I would make a quick statement try to always K.I.S.S. your code (Keep It Simple and Smart) I have seen a lot of python and pyqt code recently that is overly complex (which is easy to do with python) due to not compartmentalizing the code as it should be.
For instance like in C++ code the main function should be reserved for what is needed within the main and only that -- aka for pyqt that is only the stuff that needs to address the Application object of the program and then only minimally so.
The GUI or whatever the Application object is running should be where you create your first Class this then isolates functionality and makes modifications and error handling/tracking so much easier. Happy coding in your project.