Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning Keyboard Shortcuts to QML Components

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.

like image 532
Vikas Bhargava Avatar asked Aug 30 '12 08:08

Vikas Bhargava


People also ask

How do I assign keyboard shortcuts?

To assign a keyboard shortcut do the following: Begin keyboard shortcuts with CTRL or a function key. Press the TAB key repeatedly until the cursor is in the Press new shortcut key box. Press the combination of keys that you want to assign.

How do I add a shortcut to Qt?

The simplest way to create a shortcut for a particular widget is to construct the shortcut with a key sequence. For example: shortcut = new QShortcut(QKeySequence(tr("Ctrl+O", "File|Open")), parent); When the user types the key sequence for a given shortcut, the shortcut's activated() signal is emitted.

How do you use Qt QML?

Creating and Running QML Projects For simple UI files such as this one, select File > New File or Project > Application (Qt Quick) > Qt Quick Application - Empty from within Qt Creator. Pressing the green Run button runs the application. You should see the text Hello, World! in the center of a red rectangle.

What is QML GUI?

QML (Qt Modeling Language) is a user interface markup language. It is a declarative language (similar to CSS and JSON) for designing user interface–centric applications. Inline JavaScript code handles imperative aspects.


1 Answers

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

like image 160
Vikas Bhargava Avatar answered Sep 19 '22 23:09

Vikas Bhargava