Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Override CloseEvent for qml file in Qt Creator and python
Forum Update on Monday, May 27th 2025

Override CloseEvent for qml file in Qt Creator and python

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 590 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JustAbhi
    wrote on 13 Jun 2022, 03:46 last edited by
    #1

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

    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
    alt text
    This is with the Show method on the class which causes two windows to appear
    alt text
    Closing the second window that popped up causes the expected behaviour
    alt text
    Any help is greatly appreciated.

    1 Reply Last reply
    0
    • N Offline
      N Offline
      ndias
      wrote on 13 Jun 2022, 10:11 last edited by
      #2

      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

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JustAbhi
        wrote on 13 Jun 2022, 12:30 last edited by JustAbhi
        #3

        @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.

        N 1 Reply Last reply 13 Jun 2022, 14:44
        0
        • J JustAbhi
          13 Jun 2022, 12:30

          @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.

          N Offline
          N Offline
          ndias
          wrote on 13 Jun 2022, 14:44 last edited by
          #4

          @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

          1 Reply Last reply
          0

          1/4

          13 Jun 2022, 03:46

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved