Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

ApplicationWindow not calling onClosing on cmd+q on macOS



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



  • 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()
                }
            }
        }
    }
    


  • 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).



  • 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()
                }
            }
        }
    }
    

Log in to reply