Override CloseEvent for qml file in Qt Creator and python
-
So I am creating an application on Qt Creator with python as my backend and I have a problem that i can't seem to pin point.
So what i want to do is have a function called when the user exits the application but the function closeEvent(self, event:QCloseEvent) is never called.
The Constructor that the class inheriting the QMainWindow has is called but the over ridden methods aren't called.
Main.qml
import QtQuick import QtQuick.Window Window { id: mainwindow width: 640 height: 480 visible: true color: "#000000" flags: Qt.Window title: qsTr("Hello World") Connections{ target: backend } }
# This Python file uses the following encoding: utf-8 import os from pathlib import Path import sys from PySide6.QtGui import QCloseEvent from PySide6.QtQml import QQmlApplicationEngine from PySide6.QtWidgets import QApplication,QMainWindow class DetectQuit(QMainWindow): def __init__(self): print("From Constructor") super().__init__() print("End Constructor") def hideEvent(self, event): print("Minimizing Application") def closeEvent(self, event:QCloseEvent) : return super().closeEvent(event) def closeEvent(self, event): print("Exiting Application") if __name__ == "__main__": app = QApplication(sys.argv) engine = QQmlApplicationEngine() mainwindow=DetectQuit() engine.rootContext().setContextProperty("backend",mainwindow) engine.load(os.fspath(Path(__file__).resolve().parent / "main.qml")) if not engine.rootObjects(): sys.exit(-1) sys.exit(app.exec())
So what happens is that the DetectQuit class' constructor is called but the over-ridden functions are not called. If I add a self.show() in the constructor, there appears a second window titled "python" that calls the events accordingly.
Images for Visual assistance :
Running the Application Prints the Constructor part but not the Closing and hiding Events
This is with the Show method on the class which causes two windows to appear
Closing the second window that popped up causes the expected behaviour
Any help is greatly appreciated. -
Hi @JustAbhi,
You can use closing signal on QML side to call a function you define on your python class:
# This Python file uses the following encoding: utf-8 import os from pathlib import Path import sys from PySide6.QtQml import QQmlApplicationEngine from PySide6.QtWidgets import QApplication,QMainWindow from PySide6.QtCore import QUrl, Slot class DetectQuit(QMainWindow): def __init__(self): print("From Constructor") super().__init__() print("End Constructor") def hideEvent(): print("Minimizing Application") @Slot() def closeFunction(self): print("Exiting Application (2)") #app.quit() # if you use close.accepted = false on qml if __name__ == "__main__": app = QApplication(sys.argv) engine = QQmlApplicationEngine() mainwindow=DetectQuit() engine.rootContext().setContextProperty("backend",mainwindow) engine.load(QUrl.fromLocalFile(os.path.abspath("Examples/Qt Quick (QML)/TTTTTEST(2).qml"))) if not engine.rootObjects(): sys.exit(-1) sys.exit(app.exec())
import QtQuick import QtQuick.Window Window { id: mainwindow width: 640 height: 480 visible: true color: "#000000" flags: Qt.Window title: qsTr("Hello World") onClosing: { print("Exiting Application (1)") backend.closeFunction() } /* // close.accepted = false -- if you need to do something else before the window can be closed onClosing: function(close) { print("Exiting Application (1)") close.accepted = false backend.closeFunction() } */ }
Regards
-
@ndias said in Override CloseEvent for qml file in Qt Creator and python:
onClosing: {
print("Exiting Application (1)")
backend.closeFunction()
}Thanks for the Answer.
So now I am facing a new bug (I think its a bug). Your Answer is correct it does what I want but then the Qt Creator has a problem with the above mentioned line. The error pops up in Qt Creator when editing the qml file and prevents the design tab to open. The error goes something like:
Invalid property name "onClosing". (M16)
I think I read somewhere that its a bug but I might be wrong so I wanted to clarify this too.
-
@JustAbhi said in Override CloseEvent for qml file in Qt Creator and python:
So now I am facing a new bug (I think its a bug). Your Answer is correct it does what I want but then the Qt Creator has a problem with the above mentioned line. The error pops up in Qt Creator when editing the qml file and prevents the design tab to open. The error goes something like:
Invalid property name "onClosing". (M16)Yes it is a QT Creator issue. This issue has already been reported in: https://bugreports.qt.io/browse/QTCREATORBUG-13347