Getting "Alt+F4" shortcut not working



  • Hi,

    I can't get "Alt + F4" shortcut to display a confirmation dialog. I can only prevent it from closing the window as shown below:

    ApplicationWindow {
        ...
        property bool closeKeysWereTriggered: false
        ...
        onClosing: {
            if (!closeKeysWereTriggered) {
                close.accepted = false;
        }
    
        Shortcut {
                sequence: "Escape"
                onActivated:  {
                    console.log("Escape shortcut");
                    exitDialog.open();
                }
    
                context: Qt.ApplicationShortcut
        }
        
        Shortcut {
                sequence: "Alt+F4"
                onActivated:  {
                    console.log("Alt+F4 shortcut");
                    exitDialog.open();
                }
    
                context: Qt.ApplicationShortcut
        }
        ...
        Dialog {
            id: exitDialog
        ...
            Button {
                id: yesButton
        ...
                onClicked: {
                    closeKeysWereTriggered = true;
                    window.close();
                }
        ...
    

    However, "Alt+F3" shortcut works.

    Any idea ?

    Best regards.


  • Lifetime Qt Champion

    Hi,

    AFAIK, ALT+F4 is not assignable as a shortcut (a bit like ALT+Tab) so you have to put the related logic in the onClosing method.



  • I found a solution in the code above:

    onClosing: {
            if (!closeKeysWereTriggered) {
                close.accepted = false;
                exitDialog.open();
            }
        }
    

    Now, the exit dialog is always displayed.

    Best regards.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.