Important: Please read the Qt Code of Conduct -

Intercetp system key (Alt+F4, Ctrl+Alt+Canc, ...) in a qtquick 2 app

  • Hello,
    I'm new to Qt and Qt Quick, so I apologize if my question is silly and already has an answer, but after a lot of research, I have not found a solution that works.

    I'm developing an app for a info point totem. The app have 2 interfaces: a normal qt gui (a mainwindow), that run under all and is a service gui used for maintenance, and a QtQuick Gui, the real interface for the user that use the totem, loaded in fullscreen on the Qt Gui (I've used a QQmlApplicationEngine to load it).
    For security reason (it's a request that I have), I need to inhibit the key sequences that permit to close the QtQuick Gui, like Alt+F4, Ctrl+Alt+Canc, ... It's possible to close the QtQuick Gui only with a special key sequence (and this run correctly), and return to the Qt Gui service interface.
    I've tried to intercept this sequence from qml, using the Keys.onPressed with the focus set to true, but nothing: here I run the special sequence that intercept the Ctrl+w combination, but not the Alt+F4 or the Ctrl+Alt+Canc.
    I've tried overriding the eventFilter(QObject *obj, QEvent *event) function (applied to the main window or to the QQmlApplicationEngine), but nothing: the filter intercept the Ctrl + w, but nothing other.

    For now, I'm developing on a Windows 10 system, but probably will pass to a Debian or Kubuntu system.

    Do you have some idea? It's possible to intercept the system calls like the Ctrl+F4 and Ctrl+Alt+Canc?

    Thanks a lot

  • From looking at the documentation, you can use the close.accepted property in the callback of the closing signal in the Window type to prevent the window from closing.

    I haven't tried it, but maybe this could work.

    Window {
        id: win
        property bool closeKeysWereTriggered: false
        onClosed: {
            if (!closeKeysWereTriggered)  // If user defined close keys weren't triggered 
                close.accepted = false    // force the window to stay open 
        Shortcut {
            sequence: "Ctrl+Q"
            onActivated: {
                closeKeysWereTriggered = true;

  • Hi JordanHarris,
    wonderful, it works! In this way I can block sequence like Alt+F4. Thanks a lot.
    I had to change from Window to ApplicationWindow, otherwise I could not use the onClosing method.

    Do you have any idea on how to block sequences like Alt+Tab or Ctrl+Alt+Canc?

    Thanks in advance

  • I don't think you can do that, but I don't really know. I'm not sure if that should even be possible. I don't know what Ctrl+Alt+Canc is, but I'm assuming it's like Ctrl+Alt+Del, and that should be available no matter where you are. Same with switching active application.

  • @JordanHarris
    Hi JordanHarris,
    ok, that's what I was afraid, I will try to solve in any other way.
    In the Italian keyboard layout, the Del button is called Canc, and in the rush to write I forgot it ...

    Thanks anyway!

Log in to reply