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. ApplicationWindow not calling onClosing on cmd+q on macOS
Forum Updated to NodeBB v4.3 + New Features

ApplicationWindow not calling onClosing on cmd+q on macOS

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 1 Posters 527 Views 1 Watching
  • 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.
  • E Offline
    E Offline
    EddieC
    wrote on last edited by EddieC
    #1

    I am getting the same symptoms of QTBUG-33235: when I quit my QApplication() with cmd+q, the onClosing() event of the QML ApplicationWindow is not triggered.

    It is triggered normally, and the application closed, if the window is closed using the close button on the window frame. It is also triggered if the method .close() is called programatically from within the QML code.

    I'm using Qt 6.22 on macOS 11.6. Any idea on what might be the problem?

    P.S.: I ran some tests and the issue seems to be linked to QApplication. It doesn't happen for QGuiApplication, for example. I also should mention that I'm using Qt For Python 6.

    1 Reply Last reply
    0
    • E Offline
      E Offline
      EddieC
      wrote on last edited by EddieC
      #3

      I've found a much simpler (and maybe obvious?) workaround! Just capture the Quit menu on macOS:

      // (...)
      import Qt.labs.platform
      
      ApplicationWindow {
          id: mainwindow
          // (...)
          MenuBar {
              id: menubar
              // (...)
              Menu {
                  title: ""
                  MenuItem {
                      id: quitaction
                      text: qsTr("&Quit")
                      shortcut: StandardKey.Quit
                      onTriggered: mainwindow.close()
                  }
              }
          }
      }
      
      1 Reply Last reply
      0
      • E Offline
        E Offline
        EddieC
        wrote on last edited by
        #2

        I've found an ugly workaround, filtering the Quit events on QApplication:

        app = QApplication(sys.argv)
        
        class QuitFilter(QObject):
            def __init__(self, app, top_windows, *args, **kwargs):
                super().__init__(*args, **kwargs)
                self.app = app
                self.top_windows = top_windows
        
            def eventFilter(self, obj, event):
                if obj is self.app and event.type() == QEvent.Quit:
                    for w in self.top_windows:
                        if w.isVisible() and not w.close():
                            return True
                return super().eventFilter(obj, event)
        
        
        engine = QQmlApplicationEngine()
        qml_file = 'application.qml')
        engine.load(qml_file)
        root_objects = engine.rootObjects()
        if not root_objects:
            sys.exit(-1)
        
        main_windows = list(root_objects)
        quit_filter = QuitFilter(app, main_windows)
        app.installEventFilter(quit_filter)
        
        sys.exit(app.exec())
        

        As an additional complicator, I've found that as of Qt 6.22 / PySide6, the filter has to be instantiated in a persistent variable from Python side. Keeping the reference only in Qt (by, e.g., passing the object directly as a parameter in installFilter(), results in it being garbage-collected).

        1 Reply Last reply
        0
        • E Offline
          E Offline
          EddieC
          wrote on last edited by EddieC
          #3

          I've found a much simpler (and maybe obvious?) workaround! Just capture the Quit menu on macOS:

          // (...)
          import Qt.labs.platform
          
          ApplicationWindow {
              id: mainwindow
              // (...)
              MenuBar {
                  id: menubar
                  // (...)
                  Menu {
                      title: ""
                      MenuItem {
                          id: quitaction
                          text: qsTr("&Quit")
                          shortcut: StandardKey.Quit
                          onTriggered: mainwindow.close()
                      }
                  }
              }
          }
          
          1 Reply Last reply
          0

          • Login

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