Assigning Keyboard Shortcuts to QML Components

Vikas Bhargava picture Vikas Bhargava · Aug 30, 2012 · Viewed 8k times · Source

I am deep into building a Desktop Application with QML and Qt Creator and I am currently researching keyboard handling and how it works with QML elements. I am already aware of the lack of proper QML replacements for Desktop Widgets.

My current problem is that I wish to assign some global keyboard shortcuts to some particular QML components (like assigning keyboard shortcuts to buttons on the GUI) which should activate them. The best I could manage is to use FocusScopes and Key Navigation to be able to just navigate the GUI via keyboards, but this isn't the same thing.

Can anyone suggest what to do in this scenario? Is there any such feature coming in with Qt 5? I couldn't find any information on this on the Internet.

Answer

Vikas Bhargava picture Vikas Bhargava · Nov 26, 2013

Answering my own question as the Shortcuts are now possible to implement in Qt 5.1.1. Shortcuts can be easily bound to QtQuick controls like Button, ToolButtons and MenuItem using the QML Action item. e.g. :

ApplicationWindow {
    ...
    ToolButton { action: openAction } // Add a tool button in a ToolBar
    ...
    Action {
        id: openAction
        text: "&Open"
        shortcut: "Ctrl+O"
        onTriggered: // Do some action
        tooltip: "Open an image"
    }
}

Pressing Ctrl+O will execute the action specified in the onTriggered section.

Refer to Qt Quick Controls Gallery example