Search code examples
pythonqmlpyside6

closeEvent for Window Python


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
    }
}

Main.py

    # 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 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 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 Closing the second window that popped up causes the expected behaviour

Any help is greatly appreciated.


Solution

  • Well the thing is that the closeEvent() does not work so a workaround is that you can add a property to the Window Component of the qml that is onClosing like:

    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()
    }
    */
    

    This was Answered by @ndias on the Qt Forum

    Hope this helps.