How do I assign a shortcut to a QPushButton?

Kvass picture Kvass · Sep 22, 2014 · Viewed 7.7k times · Source

The documentation on assigning a shortcut to a QPushButton is as follows:


A shortcut key can be specified by preceding the preferred character with an ampersand in the text. For example:

QPushButton *button = new QPushButton("&Download", this);

In this example the shortcut is Alt+D.


What do I do if I don't want an Alt+[A-Z] shortcut? For example, in my case I want my button to be fired when the TAB button is pressed. How can I achieve this effect?

Answer

Rafal Mielniczuk picture Rafal Mielniczuk · Sep 22, 2014

You can use setShortcut method, eg:

pushButton->setShortcut(QKeySequence(Qt::Key_Tab));

It will fire then slots assigned to the clicked() signal